UNPKG

4.95 kBPlain TextView Raw
1/**
2 * @jest-environment jsdom
3 */
4import * as fc from 'fast-check';
5import * as pathing from './path'
6
7
8// CREATION
9
10
11test("Creating a directory path", () => {
12 fc.assert(
13 fc.property(fc.array(fc.hexaString()), data => {
14 expect(pathing.directory(...data)).toEqual({
15 directory: data
16 })
17 })
18 )
19
20 expect(() =>
21 pathing.directory("/")
22 ).toThrow()
23})
24
25test("Creating a file path", () => {
26 fc.assert(
27 fc.property(fc.array(fc.hexaString()), data => {
28 expect(pathing.file(...data)).toEqual({
29 file: data
30 })
31 })
32 )
33
34 expect(() =>
35 pathing.file("/")
36 ).toThrow()
37})
38
39
40
41// POSIX
42
43
44test("Creating a path from a POSIX formatted string", () => {
45 expect(
46 pathing.fromPosix("foo/bar/")
47 ).toEqual(
48 { directory: [ "foo", "bar" ] }
49 )
50
51 expect(
52 pathing.fromPosix("/foo/bar/")
53 ).toEqual(
54 { directory: [ "foo", "bar" ] }
55 )
56
57 expect(
58 pathing.fromPosix("/")
59 ).toEqual(
60 { directory: [] }
61 )
62
63 expect(
64 pathing.fromPosix("foo/bar")
65 ).toEqual(
66 { file: [ "foo", "bar" ] }
67 )
68
69 expect(
70 pathing.fromPosix("/foo/bar")
71 ).toEqual(
72 { file: [ "foo", "bar" ] }
73 )
74})
75
76
77test("Converting a path to the POSIX format", () => {
78 expect(
79 pathing.toPosix({ directory: [ "foo", "bar" ] })
80 ).toEqual(
81 "foo/bar/"
82 )
83
84 expect(
85 pathing.toPosix({ directory: [] })
86 ).toEqual(
87 ""
88 )
89
90 expect(
91 pathing.toPosix({ file: [ "foo", "bar" ] })
92 ).toEqual(
93 "foo/bar"
94 )
95})
96
97
98
99// 🛠
100
101
102test("combine", () => {
103 expect(
104 pathing.combine(
105 pathing.directory("a"),
106 pathing.directory("b")
107 )
108 ).toEqual(
109 { directory: [ "a", "b" ] }
110 )
111
112 expect(
113 pathing.combine(
114 pathing.directory("a"),
115 pathing.file("b")
116 )
117 ).toEqual(
118 { file: [ "a", "b" ] }
119 )
120})
121
122
123test("isBranch", () => {
124 expect(
125 pathing.isBranch(
126 pathing.Branch.Private,
127 pathing.directory(pathing.Branch.Private, "a")
128 )
129 ).toBe(true)
130
131 expect(
132 pathing.isBranch(
133 pathing.Branch.Public,
134 pathing.directory(pathing.Branch.Private, "a")
135 )
136 ).toBe(false)
137})
138
139
140test("isDirectory", () => {
141 expect(
142 pathing.isDirectory(
143 pathing.directory(pathing.Branch.Private)
144 )
145 ).toBe(true)
146
147 expect(
148 pathing.isDirectory(
149 pathing.file("foo")
150 )
151 ).toBe(false)
152})
153
154
155test("isFile", () => {
156 expect(
157 pathing.isFile(
158 pathing.file("foo")
159 )
160 ).toBe(true)
161
162 expect(
163 pathing.isFile(
164 pathing.directory(pathing.Branch.Private)
165 )
166 ).toBe(false)
167})
168
169
170test("isRootDirectory", () => {
171 expect(
172 pathing.isRootDirectory(
173 pathing.root()
174 )
175 ).toBe(true)
176
177 expect(
178 pathing.isRootDirectory(
179 pathing.directory()
180 )
181 ).toBe(true)
182
183 expect(
184 pathing.isRootDirectory(
185 pathing.directory(pathing.Branch.Private)
186 )
187 ).toBe(false)
188})
189
190
191test("isSameBranch", () => {
192 expect(
193 pathing.isSameBranch(
194 pathing.directory(pathing.Branch.Private),
195 pathing.directory(pathing.Branch.Private)
196 )
197 ).toBe(true)
198
199 expect(
200 pathing.isSameBranch(
201 pathing.directory(pathing.Branch.Private),
202 pathing.directory(pathing.Branch.Public)
203 )
204 ).toBe(false)
205})
206
207
208test("isSameKind", () => {
209 expect(
210 pathing.isSameKind(
211 pathing.directory(),
212 pathing.file()
213 )
214 ).toBe(false)
215
216 expect(
217 pathing.isSameKind(
218 pathing.file(),
219 pathing.directory()
220 )
221 ).toBe(false)
222
223 expect(
224 pathing.isSameKind(
225 pathing.directory(),
226 pathing.directory()
227 )
228 ).toBe(true)
229
230 expect(
231 pathing.isSameKind(
232 pathing.file(),
233 pathing.file()
234 )
235 ).toBe(true)
236})
237
238
239test("kind", () => {
240 expect(
241 pathing.kind(pathing.directory())
242 ).toEqual(
243 pathing.Kind.Directory
244 )
245
246 expect(
247 pathing.kind(pathing.file())
248 ).toEqual(
249 pathing.Kind.File
250 )
251})
252
253
254test("map", () => {
255 expect(
256 pathing.map(
257 p => [ ...p, "bar" ],
258 pathing.directory("foo")
259 )
260 ).toEqual(
261 { directory: [ "foo", "bar" ] }
262 )
263
264 expect(
265 pathing.map(
266 p => [ ...p, "bar" ],
267 pathing.file("foo")
268 )
269 ).toEqual(
270 { file: [ "foo", "bar" ] }
271 )
272})
273
274
275test("parent", () => {
276 expect(
277 pathing.parent(
278 pathing.directory("foo")
279 )
280 ).toEqual(
281 pathing.root()
282 )
283
284 expect(
285 pathing.parent(
286 pathing.file("foo")
287 )
288 ).toEqual(
289 pathing.root()
290 )
291
292 expect(
293 pathing.parent(
294 pathing.root()
295 )
296 ).toEqual(
297 null
298 )
299})
300
301
302test("removeBranch", () => {
303 expect(
304 pathing.removeBranch(
305 pathing.directory("foo")
306 )
307 ).toEqual(
308 { directory: [] }
309 )
310
311 expect(
312 pathing.removeBranch(
313 pathing.directory("foo", "bar")
314 )
315 ).toEqual(
316 pathing.directory("bar")
317 )
318})
319
320
321test("unwrap", () => {
322 expect(
323 pathing.unwrap(
324 pathing.directory("foo")
325 )
326 ).toEqual(
327 [ "foo" ]
328 )
329
330 expect(
331 pathing.unwrap(
332 pathing.file("foo")
333 )
334 ).toEqual(
335 [ "foo" ]
336 )
337})