• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2013 Daniel James
3 
4     Use, modification and distribution is subject to the Boost Software
5     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 
9 #include <boost/detail/lightweight_test.hpp>
10 #include "glob.hpp"
11 
glob_tests()12 void glob_tests()
13 {
14     BOOST_TEST(quickbook::glob("", ""));
15 
16     BOOST_TEST(!quickbook::glob("*", ""));
17     BOOST_TEST(quickbook::glob("*", "a"));
18     BOOST_TEST(quickbook::glob("*b", "b"));
19     BOOST_TEST(quickbook::glob("*b", "ab"));
20     BOOST_TEST(quickbook::glob("*b", "bab"));
21     BOOST_TEST(quickbook::glob("*b*", "b"));
22     BOOST_TEST(quickbook::glob("*b*", "ab"));
23     BOOST_TEST(quickbook::glob("*b*", "bc"));
24     BOOST_TEST(quickbook::glob("*b*", "abc"));
25     BOOST_TEST(!quickbook::glob("*b*", ""));
26     BOOST_TEST(!quickbook::glob("*b*", "a"));
27     BOOST_TEST(!quickbook::glob("*b*", "ac"));
28 
29     BOOST_TEST(quickbook::glob("hello.txt", "hello.txt"));
30     BOOST_TEST(!quickbook::glob("world.txt", "helloworld.txt"));
31     BOOST_TEST(quickbook::glob("*world.txt", "helloworld.txt"));
32     BOOST_TEST(!quickbook::glob("world.txt*", "helloworld.txt"));
33     BOOST_TEST(!quickbook::glob("hello", "helloworld.txt"));
34     BOOST_TEST(!quickbook::glob("*hello", "helloworld.txt"));
35     BOOST_TEST(quickbook::glob("hello*", "helloworld.txt"));
36     BOOST_TEST(quickbook::glob("*world*", "helloworld.txt"));
37 
38     BOOST_TEST(quickbook::glob("?", "a"));
39     BOOST_TEST(!quickbook::glob("?", ""));
40     BOOST_TEST(!quickbook::glob("?", "ab"));
41     BOOST_TEST(quickbook::glob("a?", "ab"));
42     BOOST_TEST(quickbook::glob("?b", "ab"));
43     BOOST_TEST(quickbook::glob("?bc", "abc"));
44     BOOST_TEST(quickbook::glob("a?c", "abc"));
45     BOOST_TEST(quickbook::glob("ab?", "abc"));
46     BOOST_TEST(!quickbook::glob("?bc", "aac"));
47     BOOST_TEST(!quickbook::glob("a?c", "bbc"));
48     BOOST_TEST(!quickbook::glob("ab?", "abcd"));
49 
50     BOOST_TEST(quickbook::glob("[a]", "a"));
51     BOOST_TEST(!quickbook::glob("[^a]", "a"));
52     BOOST_TEST(!quickbook::glob("[b]", "a"));
53     BOOST_TEST(quickbook::glob("[^b]", "a"));
54     BOOST_TEST(quickbook::glob("[a-z]", "a"));
55     BOOST_TEST(!quickbook::glob("[^a-z]", "a"));
56     BOOST_TEST(!quickbook::glob("[b-z]", "a"));
57     BOOST_TEST(quickbook::glob("[^b-z]", "a"));
58     BOOST_TEST(quickbook::glob("[-a]", "a"));
59     BOOST_TEST(quickbook::glob("[-a]", "-"));
60     BOOST_TEST(!quickbook::glob("[-a]", "b"));
61     BOOST_TEST(!quickbook::glob("[^-a]", "a"));
62     BOOST_TEST(!quickbook::glob("[^-a]", "-"));
63     BOOST_TEST(quickbook::glob("[^-a]", "b"));
64     BOOST_TEST(quickbook::glob("[a-]", "a"));
65     BOOST_TEST(quickbook::glob("[a-]", "-"));
66     BOOST_TEST(!quickbook::glob("[a-]", "b"));
67     BOOST_TEST(!quickbook::glob("[^a-]", "a"));
68     BOOST_TEST(!quickbook::glob("[^a-]", "-"));
69     BOOST_TEST(quickbook::glob("[^a-]", "b"));
70     BOOST_TEST(quickbook::glob("[a-ce-f]", "a"));
71     BOOST_TEST(!quickbook::glob("[a-ce-f]", "d"));
72     BOOST_TEST(quickbook::glob("[a-ce-f]", "f"));
73     BOOST_TEST(!quickbook::glob("[a-ce-f]", "g"));
74     BOOST_TEST(!quickbook::glob("[^a-ce-f]", "a"));
75     BOOST_TEST(quickbook::glob("[^a-ce-f]", "d"));
76     BOOST_TEST(!quickbook::glob("[^a-ce-f]", "f"));
77     BOOST_TEST(quickbook::glob("[^a-ce-f]", "g"));
78     BOOST_TEST(!quickbook::glob("[b]", "a"));
79     BOOST_TEST(quickbook::glob("[a]bc", "abc"));
80     BOOST_TEST(quickbook::glob("a[b]c", "abc"));
81     BOOST_TEST(quickbook::glob("ab[c]", "abc"));
82     BOOST_TEST(quickbook::glob("a[a-c]c", "abc"));
83     BOOST_TEST(quickbook::glob("*[b]*", "abc"));
84     BOOST_TEST(quickbook::glob("[\\]]", "]"));
85     BOOST_TEST(!quickbook::glob("[^\\]]", "]"));
86 
87     BOOST_TEST(quickbook::glob("b*ana", "banana"));
88     BOOST_TEST(quickbook::glob("1234*1234*1234", "123412341234"));
89     BOOST_TEST(!quickbook::glob("1234*1234*1234", "1234123341234"));
90     BOOST_TEST(quickbook::glob("1234*1234*1234", "123412312312341231231234"));
91     BOOST_TEST(!quickbook::glob("1234*1234*1234", "12341231231234123123123"));
92 }
93 
invalid_glob_tests()94 void invalid_glob_tests()
95 {
96     // Note that glob only throws an exception when the pattern matches up to
97     // the point where the error occurs.
98     BOOST_TEST_THROWS(quickbook::glob("[", "a"), quickbook::glob_error);
99     BOOST_TEST_THROWS(quickbook::glob("[^", "a"), quickbook::glob_error);
100     BOOST_TEST_THROWS(quickbook::glob("[xyz", "a"), quickbook::glob_error);
101     BOOST_TEST_THROWS(quickbook::glob("[xyz\\", "a"), quickbook::glob_error);
102     BOOST_TEST_THROWS(quickbook::glob("[x\\y", "a"), quickbook::glob_error);
103     BOOST_TEST_THROWS(quickbook::glob("[a-", "a"), quickbook::glob_error);
104     BOOST_TEST_THROWS(quickbook::glob("[a-z", "a"), quickbook::glob_error);
105     BOOST_TEST_THROWS(quickbook::glob("[a-\\", "a"), quickbook::glob_error);
106     BOOST_TEST_THROWS(quickbook::glob("[a-\\a", "a"), quickbook::glob_error);
107     BOOST_TEST_THROWS(quickbook::glob("xyx[", "xyxa"), quickbook::glob_error);
108     BOOST_TEST_THROWS(quickbook::glob("]", "a"), quickbook::glob_error);
109     BOOST_TEST_THROWS(quickbook::glob("abc]", "abcd"), quickbook::glob_error);
110     BOOST_TEST_THROWS(quickbook::glob("]def", "a"), quickbook::glob_error);
111     BOOST_TEST_THROWS(quickbook::glob("[]", "a"), quickbook::glob_error);
112     BOOST_TEST_THROWS(quickbook::glob("[[]", "a"), quickbook::glob_error);
113     BOOST_TEST_THROWS(quickbook::glob("[]]", "a"), quickbook::glob_error);
114     BOOST_TEST_THROWS(quickbook::glob("**", "a"), quickbook::glob_error);
115     BOOST_TEST_THROWS(quickbook::glob("[/]", "a"), quickbook::glob_error);
116     BOOST_TEST_THROWS(quickbook::glob("[\\/]", "a"), quickbook::glob_error);
117     BOOST_TEST_THROWS(quickbook::glob("[ -/]", "a"), quickbook::glob_error);
118     BOOST_TEST_THROWS(quickbook::glob("[ -\\/]", "a"), quickbook::glob_error);
119     BOOST_TEST_THROWS(quickbook::glob("\\", "a"), quickbook::glob_error);
120     BOOST_TEST_THROWS(quickbook::glob("\\\\", "a"), quickbook::glob_error);
121 }
122 
check_glob_tests()123 void check_glob_tests()
124 {
125     BOOST_TEST(!quickbook::check_glob(""));
126     BOOST_TEST(!quickbook::check_glob("file"));
127     BOOST_TEST(!quickbook::check_glob("file\\[\\]"));
128     BOOST_TEST(quickbook::check_glob("[x]"));
129     BOOST_TEST(quickbook::check_glob("abc[x]"));
130     BOOST_TEST(quickbook::check_glob("[x]abd"));
131     BOOST_TEST_THROWS(quickbook::check_glob("["), quickbook::glob_error);
132     BOOST_TEST_THROWS(quickbook::check_glob("[^"), quickbook::glob_error);
133     BOOST_TEST_THROWS(quickbook::check_glob("[xyz"), quickbook::glob_error);
134     BOOST_TEST_THROWS(quickbook::check_glob("[xyz\\"), quickbook::glob_error);
135     BOOST_TEST_THROWS(quickbook::check_glob("[x\\y"), quickbook::glob_error);
136     BOOST_TEST_THROWS(quickbook::check_glob("[a-"), quickbook::glob_error);
137     BOOST_TEST_THROWS(quickbook::check_glob("[a-z"), quickbook::glob_error);
138     BOOST_TEST_THROWS(quickbook::check_glob("[a-\\"), quickbook::glob_error);
139     BOOST_TEST_THROWS(quickbook::check_glob("[a-\\a"), quickbook::glob_error);
140     BOOST_TEST_THROWS(quickbook::check_glob("xyx["), quickbook::glob_error);
141     BOOST_TEST_THROWS(quickbook::check_glob("]"), quickbook::glob_error);
142     BOOST_TEST_THROWS(quickbook::check_glob("abc]"), quickbook::glob_error);
143     BOOST_TEST_THROWS(quickbook::check_glob("]def"), quickbook::glob_error);
144     BOOST_TEST_THROWS(quickbook::check_glob("[]"), quickbook::glob_error);
145     BOOST_TEST_THROWS(quickbook::check_glob("[[]"), quickbook::glob_error);
146     BOOST_TEST_THROWS(quickbook::check_glob("[]]"), quickbook::glob_error);
147     BOOST_TEST_THROWS(quickbook::check_glob("**"), quickbook::glob_error);
148     BOOST_TEST_THROWS(quickbook::check_glob("[/]"), quickbook::glob_error);
149     BOOST_TEST_THROWS(quickbook::check_glob("[\\/]"), quickbook::glob_error);
150     BOOST_TEST_THROWS(quickbook::check_glob("[ -/]"), quickbook::glob_error);
151     BOOST_TEST_THROWS(quickbook::check_glob("[ -\\/]"), quickbook::glob_error);
152     BOOST_TEST_THROWS(quickbook::check_glob("\\"), quickbook::glob_error);
153     BOOST_TEST_THROWS(quickbook::check_glob("\\\\"), quickbook::glob_error);
154 }
155 
main()156 int main()
157 {
158     glob_tests();
159     invalid_glob_tests();
160     check_glob_tests();
161 
162     return boost::report_errors();
163 }
164