@@ -23,3 +23,115 @@ describe("luasnip.util.str:dedent", function()
23
23
check (" 2 and 1" , " one\n two" , " one\n two" )
24
24
check (" 2 and 2" , " one\n two" , " one\n two" )
25
25
end )
26
+
27
+ describe (" luasnip.util.Path.parent" , function ()
28
+ local function assert_parents (separator , examples )
29
+ for _ , example in ipairs (examples ) do
30
+ if example .expect then
31
+ it (example .path , function ()
32
+ assert .are .same (
33
+ example .expect ,
34
+ exec_lua (
35
+ " __LUASNIP_TEST_SEP_OVERRIDE = [["
36
+ .. separator
37
+ .. " ]] "
38
+ .. ' return require("luasnip.util.path").parent([['
39
+ .. separator
40
+ .. " ]])([["
41
+ .. example .path
42
+ .. " ]]) == [["
43
+ .. example .expect
44
+ .. " ]]"
45
+ )
46
+ )
47
+ end )
48
+ else
49
+ it (example .path .. " to be nil" , function ()
50
+ assert .is_true (
51
+ exec_lua (
52
+ " __LUASNIP_TEST_SEP_OVERRIDE = [["
53
+ .. separator
54
+ .. " ]] "
55
+ .. ' return require("luasnip.util.path").parent([['
56
+ .. separator
57
+ .. " ]])([["
58
+ .. example .path
59
+ .. " ]]) == nil"
60
+ )
61
+ )
62
+ end )
63
+ end
64
+ end
65
+ end
66
+
67
+ describe (" backslash as the path separator" , function ()
68
+ local examples = {
69
+ {
70
+ path = [[ C:\Users\username\AppData\Local\nvim-data\log]] ,
71
+ expect = [[ C:\Users\username\AppData\Local\nvim-data]] ,
72
+ },
73
+ {
74
+ path = [[ C:/Users/username/AppData/Local/nvim-data/log]] ,
75
+ expect = [[ C:/Users/username/AppData/Local/nvim-data]] ,
76
+ },
77
+ {
78
+ path = [[ D:\Projects\project_folder\source_code.py]] ,
79
+ expect = [[ D:\Projects\project_folder]] ,
80
+ },
81
+ {
82
+ path = [[ D:/Projects/project_folder/source_code.py]] ,
83
+ expect = [[ D:/Projects/project_folder]] ,
84
+ },
85
+ { path = [[ E:\Music\\\\]] , expect = nil },
86
+ { path = [[ E:/Music////]] , expect = nil },
87
+ { path = [[ E:\\Music\\\\]] , expect = nil },
88
+ { path = [[ E://Music////]] , expect = nil },
89
+ { path = [[ F:\]] , expect = nil },
90
+ { path = [[ F:\\]] , expect = nil },
91
+ { path = [[ F:/]] , expect = nil },
92
+ { path = [[ F://]] , expect = nil },
93
+ }
94
+
95
+ assert_parents (" \\ " , examples )
96
+ end )
97
+
98
+ describe (" forward slash as the path separator" , function ()
99
+ local examples = {
100
+ {
101
+ path = [[ /home/usuario/documents/archivo.txt]] ,
102
+ expect = [[ /home/usuario/documents]] ,
103
+ },
104
+ {
105
+ path = [[ /var/www/html////index.html]] ,
106
+ expect = [[ /var/www/html]] ,
107
+ },
108
+ {
109
+ path = [[ /mnt/backup/backup_file.tar.gz]] ,
110
+ expect = [[ /mnt/backup]] ,
111
+ },
112
+ {
113
+ path = [[ /mnt/]] ,
114
+ expect = nil ,
115
+ },
116
+ {
117
+ path = [[ /mnt////]] ,
118
+ expect = nil ,
119
+ },
120
+ {
121
+ path = [[ /project/\backslash\is\legal\in\linux\filename.txt]] ,
122
+ expect = [[ /project]] ,
123
+ },
124
+ {
125
+ path = [[ /\\\\]] ,
126
+ expect = " " ,
127
+ },
128
+ {
129
+ path = [[ /\\\\////]] ,
130
+ expect = nil ,
131
+ },
132
+ { path = [[ /]] , expect = nil },
133
+ }
134
+
135
+ assert_parents (" /" , examples )
136
+ end )
137
+ end )
0 commit comments