-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaths.idr
319 lines (259 loc) · 13.7 KB
/
Paths.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
-- --------------------------------------------------------------- [ Paths.idr ]
-- Module : Test.System.Posix.Paths
-- Copyright : (c) 2017 Josh Filstrup
-- License : see LICENSE
-- --------------------------------------------------------------------- [ EOH ]
module Paths
import System.Posix.Paths
assertEq : (Show a, Show b, Eq b) => (id : String) -> (fn : a -> b)
-> (input : a) -> (expected : b) -> IO Bool
assertEq id f input expected = let res = f input in
if res == expected
then do putStrLn ("Test " ++ id ++ " passed!")
pure True
else do putStrLn ("Test " ++ id ++ " failed!")
putStrLn ("\t Expected: " ++ (show expected))
putStrLn ("\t Actual: " ++ (show res))
putStrLn ("\t Input: " ++ (show input))
pure False
concatTestImpl : String -> (Maybe (Path t Directory))
-> (Maybe (Path Relative k)) -> String -> IO Bool
concatTestImpl id (Just left) (Just right) expected = let concatFn = Paths.concat left in
assertEq id (show . concatFn) right expected
concatTestImpl id _ _ _ = do putStrLn ("Test " ++ id ++ " failed! (Invalid args)")
pure False
fileExtTestImpl : String -> (Maybe (Path a File)) -> String -> IO Bool
fileExtTestImpl id (Just path) expected = assertEq id (show . Paths.getFileExtension) path expected
fileExtTestImpl id _ _ = do putStrLn ("Test " ++ id ++ " failed! (Invalid args)")
pure False
dirNameTestImpl : String -> (Maybe (Path a File)) -> String -> IO Bool
dirNameTestImpl id (Just path) expected = assertEq id (show . Paths.getDirectoryName) path expected
dirNameTestImpl id _ _ = do putStrLn ("Test " ++ id ++ " failed! (Invalid args)")
pure False
export
test_DirectoryName : IO ()
test_DirectoryName = do putStrLn "--------------------------------------------"
putStrLn "Running test_DirectoryName"
putStrLn "--------------------------------------------"
(dirNameTestImpl
"test_DirectoryName/absoluteFile"
(mkAbsoluteFile "/foo/bar/baz.txt")
"Just \"/foo/bar\"")
(dirNameTestImpl
"test_DirectoryName/relativeFile"
(mkRelativeFile "./foo/bar/baz.txt")
"Just \"foo/bar\"")
(dirNameTestImpl
"test_DirectoryName/absoluteFileRoot"
(mkAbsoluteFile "/")
"Nothing")
putStrLn ""
export
test_FileExtension : IO ()
test_FileExtension = do putStrLn "--------------------------------------------"
putStrLn "Running test_FileExtension"
putStrLn "--------------------------------------------"
(fileExtTestImpl
"test_FileExtension/absolute"
(mkAbsoluteFile "/foo/bar/baz.txt")
"Just \"txt\"")
(fileExtTestImpl
"test_FileExtension/relative"
(mkRelativeFile "./foo/bar/baz.txt")
"Just \"txt\"")
(fileExtTestImpl
"test_FileExtension/relativeNoExtension"
(mkRelativeFile "./foo/bar/baz")
"Nothing")
(fileExtTestImpl
"test_FileExtension/absoluteNoExtension"
(mkAbsoluteFile "/foo/bar/baz")
"Nothing")
putStrLn ""
export
test_Concatenation : IO ()
test_Concatenation = do putStrLn "--------------------------------------------"
putStrLn "Running test_Concatenation"
putStrLn "--------------------------------------------"
(concatTestImpl
"test_Concatenation/absRelFile"
(mkAbsoluteDirectory "/foo/bar")
(mkRelativeFile "temp.txt")
"Absolute File: /foo/bar/temp.txt")
(concatTestImpl
"test_Concatenation/absRelDir"
(mkAbsoluteDirectory "/foo/bar")
(mkRelativeDirectory "temp")
"Absolute Directory: /foo/bar/temp")
(concatTestImpl
"test_Concatenation/relRelFile"
(mkRelativeDirectory "./foo/bar")
(mkRelativeFile "temp.txt")
"Relative File: foo/bar/temp.txt")
(concatTestImpl
"test_Concatenation/relRelDir"
(mkRelativeDirectory "./foo/bar")
(mkRelativeDirectory "temp")
"Relative Directory: foo/bar/temp")
(concatTestImpl
"test_Concatenation/normalize"
(mkRelativeDirectory "./foo///bar//baz")
(mkRelativeDirectory "temp/")
"Relative Directory: foo/bar/baz/temp")
putStrLn ""
export
test_MkRelativeFile : IO ()
test_MkRelativeFile = do putStrLn "--------------------------------------------"
putStrLn "Running test_MkRelativeFile"
putStrLn "--------------------------------------------"
(assertEq
"test_MkRelativeFile/basic"
(show . Paths.mkRelativeFile)
"./foo/bar/temp.txt"
"Just Relative File: foo/bar/temp.txt")
(assertEq
"test_MkRelativeFile/absolute"
(show . Paths.mkRelativeFile)
"/foo/bar/temp.txt"
"Nothing")
(assertEq
"test_MkRelativeFile/normalized"
(show . Paths.mkRelativeFile)
"foo/bar///baz//temp.txt"
"Just Relative File: foo/bar/baz/temp.txt")
(assertEq
"test_MkRelativeFile/relativeSpecifiers"
(show . Paths.mkRelativeFile)
"foo/bill/../bar/../baz/temp.txt"
"Just Relative File: foo/baz/temp.txt")
putStrLn ""
export
test_MkRelativeDirectory : IO ()
test_MkRelativeDirectory = do putStrLn "--------------------------------------------"
putStrLn "Running test_MkRelativeDirectory"
putStrLn "--------------------------------------------"
(assertEq
"test_MkRelativeDirectory/basic"
(show . Paths.mkRelativeDirectory)
"./foo/bar/"
"Just Relative Directory: foo/bar")
(assertEq
"test_MkRelativeDirectory/absolute"
(show . Paths.mkRelativeDirectory)
"/foo/bar/"
"Nothing")
(assertEq
"test_MkRelativeDirectory/normalized"
(show . Paths.mkRelativeDirectory)
"foo/bar///baz//"
"Just Relative Directory: foo/bar/baz")
(assertEq
"test_MkRelativeDirectory/relativeSpecifiers"
(show . Paths.mkRelativeDirectory)
"foo/bill/../bar/../baz/"
"Just Relative Directory: foo/baz")
putStrLn ""
export
test_MkAbsoluteFile : IO ()
test_MkAbsoluteFile = do putStrLn "--------------------------------------------"
putStrLn "Running test_MkAbsoluteFile"
putStrLn "--------------------------------------------"
(assertEq
"test_MkAbsoluteDirectory/basic"
(show . Paths.mkAbsoluteFile)
"/foo/bar/baz.txt"
"Just Absolute File: /foo/bar/baz.txt")
(assertEq
"test_MkAbsoluteDirectory/normalized"
(show . Paths.mkAbsoluteFile)
"/foo//bar/////baz.txt"
"Just Absolute File: /foo/bar/baz.txt")
(assertEq
"test_MkAbsoluteDirectory/relative"
(show . Paths.mkAbsoluteFile)
"./foo/bar/baz.txt"
"Nothing")
(assertEq
"test_MkAbsoluteDirectory/relativeSpecifiers"
(show . Paths.mkAbsoluteFile)
"/foo/../bar/../baz.txt"
"Nothing")
putStrLn ""
export
test_MkAbsoluteDirectory : IO ()
test_MkAbsoluteDirectory = do putStrLn "--------------------------------------------"
putStrLn "Running test_MkAbsoluteDirectory"
putStrLn "--------------------------------------------"
(assertEq
"test_MkAbsoluteDirectory/basic"
(show . Paths.mkAbsoluteDirectory)
"/foo/bar/"
"Just Absolute Directory: /foo/bar")
(assertEq
"test_MkAbsoluteDirectory/normalized"
(show . Paths.mkAbsoluteDirectory)
"/foo/bar///baz/"
"Just Absolute Directory: /foo/bar/baz")
(assertEq
"test_MkAbsoluteDirectory/normalized2"
(show . Paths.mkAbsoluteDirectory)
"/foo/../bar///baz/"
"Nothing")
(assertEq
"test_MkAbsoluteDirectory/missingFrontSlash"
(show . Paths.mkAbsoluteDirectory)
"foo/bar/"
"Nothing")
(assertEq
"test_MkAbsoluteDirectory/tildeExists"
(show . Paths.mkAbsoluteDirectory)
"~/foo/bar/"
"Nothing")
putStrLn ""
export
test_Normalization : IO ()
test_Normalization = do putStrLn "--------------------------------------------"
putStrLn "Running test_Normalization"
putStrLn "--------------------------------------------"
(assertEq
"test_Normalization/tooManySlashes"
(Paths.normalize Absolute)
"//foo/bar//baz////billy/"
"/foo/bar/baz/billy")
(assertEq
"test_Normalization/dotSlashPrefix"
(Paths.normalize Relative)
"./hello/there"
"hello/there")
(assertEq
"test_Normalization/doubleDots"
(Paths.normalize Relative)
"hello/../there"
"there")
(assertEq
"test_Normalization/manyDoubleDots"
(Paths.normalize Relative)
"hello/../there/billy/../"
"there")
(assertEq
"test_Normalization/rootPath"
(Paths.normalize Absolute)
"/"
"/")
putStrLn ""
export
test_Eq : IO ()
test_Eq = do putStrLn "--------------------------------------------"
putStrLn "Running test_Eq"
putStrLn "--------------------------------------------"
(assertEq
"test_Eq/basic"
((==) (mkAbsoluteDirectory "/foo/bar"))
(mkAbsoluteDirectory "/foo/bar")
True)
(assertEq
"test_Eq/basic2"
((==) (mkAbsoluteDirectory "/foo/bar"))
(mkAbsoluteDirectory "/foo/bar/baz")
False)
putStrLn ""