1
2module Test.Cases(testCases) where
3
4import Test.Util
5import System.FilePath((</>))
6import System.Info.Extra
7
8
9testCases :: IO ()
10testCases = testMatch >> testArity >> testSubstitute >> testStepNext >> testDirectory
11
12
13testArity :: IO ()
14testArity = do
15    arity "" 0
16    arity "a*b" 1
17    arity "a//b" 0
18    arity "a/**/b" 1
19    arity "/a/b/cccc_" 0
20    arity "a///b" 0
21    arity "foo/**/*" 2
22    arity "//*a.txt" 1
23    arity "foo//a*.txt" 1
24    arity "**/*a.txt" 2
25    arity "foo/**/a*.txt" 2
26    arity "//*a.txt" 1
27    arity "foo//a*.*txt" 2
28    arity "foo/**/a*.*txt" 3
29
30
31testSubstitute :: IO ()
32testSubstitute = do
33    substitute "**/*a*.txt" ["","test","da"] "testada.txt"
34    substitute "**/*a.txt" ["foo/bar","test"] "foo/bar/testa.txt"
35    -- error if the number of replacements is wrong
36    substituteErr "nothing" ["test"] ["substitute","nothing","expects 0","got 1","test"]
37    substituteErr "*/*" ["test"] ["substitute","*/*","expects 2","got 1","test"]
38
39
40testMatch :: IO ()
41testMatch = do
42    matchN "//*.c" "foo/bar/baz.c"
43    matchY "**/*.c" "foo/bar/baz.c" ["foo/bar","baz"]
44    matchY ("**" </> "*.c") ("foo/bar" </> "baz.c") ["foo/bar","baz"]
45    matchY "*.c" "baz.c" ["baz"]
46    matchN "//*.c" "baz.c"
47    matchY "**/*.c" "baz.c" ["","baz"]
48    matchY "**/*a.txt" "foo/bar/testa.txt" ["foo/bar","test"]
49    matchN "**/*.c" "baz.txt"
50    matchY "**/*a.txt" "testa.txt" ["","test"]
51    matchY "**/a.txt" "a.txt" [""]
52    matchY "a/**/b" "a/b" [""]
53    matchY "a/**/b" "a/x/b" ["x"]
54    matchY "a/**/b" "a/x/y/b" ["x/y"]
55    matchY "a/**/**/b" "a/x/y/b" ["","x/y"]
56    matchY "**/*a*.txt" "testada.txt" ["","test","da"]
57    matchY "test.c" "test.c" []
58    matchN "*.c" "foor/bar.c"
59    matchN "*/*.c" "foo/bar/baz.c"
60    matchN "foo//bar" "foobar"
61    matchN "foo/**/bar" "foobar"
62    matchN "foo//bar" "foobar/bar"
63    matchN "foo/**/bar" "foobar/bar"
64    matchN "foo//bar" "foo/foobar"
65    matchN "foo/**/bar" "foo/foobar"
66    matchN "foo//bar" "foo/bar"
67    matchY "foo/**/bar" "foo/bar" [""]
68    matchY "foo/bar" ("foo" </> "bar") []
69    matchY ("foo" </> "bar") "foo/bar" []
70    matchY ("foo" </> "bar") ("foo" </> "bar") []
71    matchY "**/*.c" ("bar" </> "baz" </> "foo.c") ["bar/baz","foo"]
72    matchY "**/*" "/bar" ["/","bar"]
73    matchN "/bob//foo" "/bob/this/test/foo"
74    matchY "/bob/**/foo" "/bob/this/test/foo" ["this/test"]
75    matchN "/bob//foo" "bob/this/test/foo"
76    matchN "/bob/**/foo" "bob/this/test/foo"
77    matchN "bob//foo/" "bob/this/test/foo/"
78    matchY "bob/**/foo/" "bob/this/test/foo/" ["this/test"]
79    matchY "bob/**/foo/" "bob/foo/" [""]
80    matchY "bob/**/foo/" "bob//foo/" ["/"]
81    matchN "bob//foo/" "bob/this/test/foo"
82    matchN "bob/**/foo/" "bob/this/test/foo"
83    matchY ("**" </> "*a*.txt") "testada.txt" ["","test","da"]
84    matchN "a//" "a"
85    matchY "a/**" "a" [""]
86    matchY "a/**" "a/" ["/"]
87    matchN "/a//" "/a"
88    matchY "a/**" "a" [""]
89    matchY "/a/**" "/a" [""]
90    matchN "///a//" "/a"
91    matchY "**/a/**" "/a" ["/",""]
92    matchN "///" ""
93    matchY "/**" "/" ["/"]
94    matchY "**/" "a/" ["a"]
95    matchY "**/**" "" ["","/"]
96    matchY "x/**/y" "x/y" [""]
97    matchY "x/**/" "x/" [""]
98    matchY "x/**/" "x/foo/" ["foo"]
99    matchN "x///" "x"
100    matchN "x/**/" "x"
101    matchY "x/**/" "x/foo/bar/" ["foo/bar"]
102    matchN "x///" "x/foo/bar"
103    matchN "x/**/" "x/foo/bar"
104    matchY "x/**/*/y" "x/z/y" ["","z"]
105    matchY "" "" []
106    matchN "" "y"
107    matchN "" "/"
108
109    matchY "*/*" "x/y" ["x","y"]
110    matchN "*/*" "x"
111    matchY "**/*" "x" ["","x"]
112    matchY "**/*" "" ["",""]
113    matchY "*/**" "x" ["x",""]
114    matchY "*/**/*" "x/y" ["x","","y"]
115    matchN "*//*" ""
116    matchN "*/**/*" ""
117    matchN "*//*" "x"
118    matchN "*/**/*" "x"
119    matchN "*//*//*" "x/y"
120    matchN "*/**/*/**/*" "x/y"
121    matchY "**/*/" "/" ["",""]
122    matchY "*/**/**/" "/" ["","",""]
123    matchN "b*b*b*//" "bb"
124    matchN "b*b*b*/**" "bb"
125
126    matchY "**" "/" ["//"] -- UGLY corner case
127    matchY "**/x" "/x" ["/"]
128    matchY "**" "x/" ["x/"]
129    let s = if isWindows then '/' else '\\'
130    matchY "**" "\\\\drive" [s:s:"drive"]
131    matchY "**" "C:\\drive" ["C:"++s:"drive"]
132    matchY "**" "C:drive" ["C:drive"]
133
134    matchN "./file" "file"
135    matchN "/file" "file"
136    matchN "foo/./bar" "foo/bar"
137    matchY "foo/./bar" "foo/./bar" []
138    matchN "foo/./bar" "foo/bar"
139    matchY "**z" "xyz" ["","xy"]
140    matchY "**/a/b*" "a/a/a/a/bc" ["a/a/a","c"]
141    matchY "**/a/b/**" "a/a/a/a/b/c" ["a/a/a","c"]
142
143
144testStepNext :: IO ()
145testStepNext = do
146    stepNext ["*.xml"] [] StepUnknown
147    stepNext ["*.xml"] ["foo"] $ StepOnly []
148    stepNext ["**/*.xml"] [] StepUnknown
149    stepNext ["**/*.xml"] ["foo"] StepUnknown
150    stepNext ["foo/bar/*.xml"] [] $ StepOnly ["foo"]
151    stepNext ["foo/bar/*.xml"] ["oof"] $ StepOnly []
152    stepNext ["foo/bar/*.xml"] ["foo"] $ StepOnly ["bar"]
153    stepNext ["a","b/c"] [] $ StepOnly ["a","b"]
154    stepNext ["a","b/c"] ["b"] $ StepOnly ["c"]
155    stepNext ["*/x"] [] StepUnknown
156    stepNext ["*/x"] ["foo"] $ StepOnly ["x"]
157    stepNext ["*/**"] ["bar"] StepEverything
158
159
160testDirectory :: IO ()
161testDirectory = do
162    getDirectory ["**/*.c"] [] ["baz/test.c","baz/zoo.c","foo/bar.c","foo/foo/foo.c","zoo.c"] ["extra/test.h","foo.c/bob.h"]
163    -- Currently no way to test what it can access, sadly
164    -- should only look inside: foo
165    getDirectory ["foo/*.c"] [] ["foo/bar.c","foo/baz.c"] ["foo.c","foo/bar/baz.c","test/foo.c"]
166    -- should only look inside: . foo zoo
167    getDirectory ["foo/*.c","**/*.h"] [".git/**","**/no.*"] ["foo/bar.c","foo/baz.h","zoo/test.h"] ["foo/no.c",".git/foo.h","zoo/test.c"]
168