@@ -58,6 +58,7 @@ describe("Path2", function()
58
58
{ " foo/bar/" , " foo/bar" },
59
59
{ { readme_path }, readme_path },
60
60
{ { readme_path , license_path }, license_path }, -- takes only the last abs path
61
+ { " ." , " ." },
61
62
}
62
63
63
64
return paths
@@ -334,6 +335,65 @@ describe("Path2", function()
334
335
end )
335
336
end )
336
337
338
+ describe (" :shorten" , function ()
339
+ it_cross_plat (" can shorten a path" , function ()
340
+ local long_path = " this/is/a/long/path"
341
+ local short_path = Path :new (long_path ):shorten ()
342
+ assert .are .same (short_path , plat_path " t/i/a/l/path" )
343
+ end )
344
+
345
+ it_cross_plat (" can shorten a path's components to a given length" , function ()
346
+ local long_path = " this/is/a/long/path"
347
+ local short_path = Path :new (long_path ):shorten (2 )
348
+ assert .are .same (short_path , plat_path " th/is/a/lo/path" )
349
+
350
+ -- without the leading /
351
+ long_path = " this/is/a/long/path"
352
+ short_path = Path :new (long_path ):shorten (3 )
353
+ assert .are .same (short_path , plat_path " thi/is/a/lon/path" )
354
+
355
+ -- where len is greater than the length of the final component
356
+ long_path = " this/is/an/extremely/long/path"
357
+ short_path = Path :new (long_path ):shorten (5 )
358
+ assert .are .same (short_path , plat_path " this/is/an/extre/long/path" )
359
+ end )
360
+
361
+ it_cross_plat (" can shorten a path's components when excluding parts" , function ()
362
+ local long_path = " this/is/a/long/path"
363
+ local short_path = Path :new (long_path ):shorten (nil , { 1 , - 1 })
364
+ assert .are .same (short_path , plat_path " this/i/a/l/path" )
365
+
366
+ -- without the leading /
367
+ long_path = " this/is/a/long/path"
368
+ short_path = Path :new (long_path ):shorten (nil , { 1 , - 1 })
369
+ assert .are .same (short_path , plat_path " this/i/a/l/path" )
370
+
371
+ -- where excluding positions greater than the number of parts
372
+ long_path = " this/is/an/extremely/long/path"
373
+ short_path = Path :new (long_path ):shorten (nil , { 2 , 4 , 6 , 8 })
374
+ assert .are .same (short_path , plat_path " t/is/a/extremely/l/path" )
375
+
376
+ -- where excluding positions less than the negation of the number of parts
377
+ long_path = " this/is/an/extremely/long/path"
378
+ short_path = Path :new (long_path ):shorten (nil , { - 2 , - 4 , - 6 , - 8 })
379
+ assert .are .same (short_path , plat_path " this/i/an/e/long/p" )
380
+ end )
381
+
382
+ it_cross_plat (" can shorten a path's components to a given length and exclude positions" , function ()
383
+ local long_path = " this/is/a/long/path"
384
+ local short_path = Path :new (long_path ):shorten (2 , { 1 , - 1 })
385
+ assert .are .same (short_path , plat_path " this/is/a/lo/path" )
386
+
387
+ long_path = " this/is/a/long/path"
388
+ short_path = Path :new (long_path ):shorten (3 , { 2 , - 2 })
389
+ assert .are .same (short_path , plat_path " thi/is/a/long/pat" )
390
+
391
+ long_path = " this/is/an/extremely/long/path"
392
+ short_path = Path :new (long_path ):shorten (5 , { 3 , - 3 })
393
+ assert .are .same (short_path , plat_path " this/is/an/extremely/long/path" )
394
+ end )
395
+ end )
396
+
337
397
describe (" parents" , function ()
338
398
it_cross_plat (" should extract the ancestors of the path" , function ()
339
399
local p = Path :new (vim .fn .getcwd ())
0 commit comments