robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
8.3 KiB264 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local exec_lua = n.exec_lua
7local eq = t.eq
8local is_os = t.is_os
9local skip = t.skip
10local write_file = t.write_file
11
12describe('URI methods', function()
13  before_each(function()
14    clear()
15  end)
16
17  describe('file path to uri', function()
18    describe('encode Unix file path', function()
19      it('file path includes only ascii characters', function()
20        exec_lua("filepath = '/Foo/Bar/Baz.txt'")
21
22        eq('file:///Foo/Bar/Baz.txt', exec_lua('return vim.uri_from_fname(filepath)'))
23      end)
24
25      it('file path including white space', function()
26        exec_lua("filepath = '/Foo /Bar/Baz.txt'")
27
28        eq('file:///Foo%20/Bar/Baz.txt', exec_lua('return vim.uri_from_fname(filepath)'))
29      end)
30
31      it('file path including Unicode characters', function()
32        exec_lua("filepath = '/xy/åäö/ɧ/汉语/↥/🤦/🦄/å/بِيَّ.txt'")
33
34        -- The URI encoding should be case-insensitive
35        eq(
36          'file:///xy/%c3%a5%c3%a4%c3%b6/%c9%a7/%e6%b1%89%e8%af%ad/%e2%86%a5/%f0%9f%a4%a6/%f0%9f%a6%84/a%cc%8a/%d8%a8%d9%90%d9%8a%d9%8e%d9%91.txt',
37          exec_lua('return vim.uri_from_fname(filepath)')
38        )
39      end)
40    end)
41
42    describe('encode Windows filepath', function()
43      it('file path includes only ascii characters', function()
44        exec_lua([[filepath = 'C:\\Foo\\Bar\\Baz.txt']])
45
46        eq('file:///C:/Foo/Bar/Baz.txt', exec_lua('return vim.uri_from_fname(filepath)'))
47      end)
48
49      it('file path including white space', function()
50        exec_lua([[filepath = 'C:\\Foo \\Bar\\Baz.txt']])
51
52        eq('file:///C:/Foo%20/Bar/Baz.txt', exec_lua('return vim.uri_from_fname(filepath)'))
53      end)
54
55      it('file path including Unicode characters', function()
56        exec_lua([[filepath = 'C:\\xy\\åäö\\ɧ\\汉语\\↥\\🤦\\🦄\\å\\بِيَّ.txt']])
57
58        eq(
59          'file:///C:/xy/%c3%a5%c3%a4%c3%b6/%c9%a7/%e6%b1%89%e8%af%ad/%e2%86%a5/%f0%9f%a4%a6/%f0%9f%a6%84/a%cc%8a/%d8%a8%d9%90%d9%8a%d9%8e%d9%91.txt',
60          exec_lua('return vim.uri_from_fname(filepath)')
61        )
62      end)
63    end)
64  end)
65
66  describe('uri to filepath', function()
67    describe('decode Unix file path', function()
68      it('file path includes only ascii characters', function()
69        exec_lua("uri = 'file:///Foo/Bar/Baz.txt'")
70
71        eq('/Foo/Bar/Baz.txt', exec_lua('return vim.uri_to_fname(uri)'))
72      end)
73
74      it('local file path without hostname', function()
75        exec_lua("uri = 'file:/Foo/Bar/Baz.txt'")
76
77        eq('/Foo/Bar/Baz.txt', exec_lua('return vim.uri_to_fname(uri)'))
78      end)
79
80      it('file path including white space', function()
81        exec_lua("uri = 'file:///Foo%20/Bar/Baz.txt'")
82
83        eq('/Foo /Bar/Baz.txt', exec_lua('return vim.uri_to_fname(uri)'))
84      end)
85
86      it('file path including Unicode characters', function()
87        local test_case = [[
88        local uri = 'file:///xy/%C3%A5%C3%A4%C3%B6/%C9%A7/%E6%B1%89%E8%AF%AD/%E2%86%A5/%F0%9F%A4%A6/%F0%9F%A6%84/a%CC%8A/%D8%A8%D9%90%D9%8A%D9%8E%D9%91.txt'
89        return vim.uri_to_fname(uri)
90        ]]
91
92        eq('/xy/åäö/ɧ/汉语/↥/🤦/🦄/å/بِيَّ.txt', exec_lua(test_case))
93      end)
94
95      it('file path with uri fragment', function()
96        exec_lua("uri = 'file:///Foo/Bar/Baz.txt#fragment'")
97
98        eq('/Foo/Bar/Baz.txt', exec_lua('return vim.uri_to_fname(uri)'))
99      end)
100    end)
101
102    describe('decode Windows filepath', function()
103      it('file path includes only ascii characters', function()
104        local test_case = [[
105        local uri = 'file:///C:/Foo/Bar/Baz.txt'
106        return vim.uri_to_fname(uri)
107        ]]
108
109        eq('C:\\Foo\\Bar\\Baz.txt', exec_lua(test_case))
110      end)
111
112      it('local file path without hostname', function()
113        local test_case = [[
114        local uri = 'file:/C:/Foo/Bar/Baz.txt'
115        return vim.uri_to_fname(uri)
116        ]]
117
118        eq('C:\\Foo\\Bar\\Baz.txt', exec_lua(test_case))
119      end)
120
121      it('file path includes only ascii characters with encoded colon character', function()
122        local test_case = [[
123        local uri = 'file:///C%3A/Foo/Bar/Baz.txt'
124        return vim.uri_to_fname(uri)
125        ]]
126
127        eq('C:\\Foo\\Bar\\Baz.txt', exec_lua(test_case))
128      end)
129
130      it('file path including white space', function()
131        local test_case = [[
132        local uri = 'file:///C:/Foo%20/Bar/Baz.txt'
133        return vim.uri_to_fname(uri)
134        ]]
135
136        eq('C:\\Foo \\Bar\\Baz.txt', exec_lua(test_case))
137      end)
138
139      it('file path including Unicode characters', function()
140        local test_case = [[
141        local uri = 'file:///C:/xy/%C3%A5%C3%A4%C3%B6/%C9%A7/%E6%B1%89%E8%AF%AD/%E2%86%A5/%F0%9F%A4%A6/%F0%9F%A6%84/a%CC%8A/%D8%A8%D9%90%D9%8A%D9%8E%D9%91.txt'
142        return vim.uri_to_fname(uri)
143        ]]
144
145        eq('C:\\xy\\åäö\\ɧ\\汉语\\↥\\🤦\\🦄\\å\\بِيَّ.txt', exec_lua(test_case))
146      end)
147    end)
148
149    describe('decode non-file URI', function()
150      it('uri_to_fname returns non-file URI unchanged', function()
151        eq(
152          'jdt1.23+x-z://content/%5C/',
153          exec_lua [[
154          return vim.uri_to_fname('jdt1.23+x-z://content/%5C/')
155        ]]
156        )
157      end)
158
159      it('uri_to_fname returns non-file upper-case scheme URI unchanged', function()
160        eq(
161          'JDT://content/%5C/',
162          exec_lua [[
163          return vim.uri_to_fname('JDT://content/%5C/')
164        ]]
165        )
166      end)
167
168      it('uri_to_fname returns non-file scheme URI without authority unchanged', function()
169        eq(
170          'zipfile:///path/to/archive.zip%3A%3Afilename.txt',
171          exec_lua [[
172          return vim.uri_to_fname('zipfile:///path/to/archive.zip%3A%3Afilename.txt')
173        ]]
174        )
175      end)
176    end)
177
178    describe('decode URI without scheme', function()
179      it('fails because URI must have a scheme', function()
180        eq(
181          false,
182          exec_lua [[
183          return pcall(vim.uri_to_fname, 'not_an_uri.txt')
184        ]]
185        )
186      end)
187
188      it('uri_to_fname should not treat comma as a scheme character', function()
189        eq(
190          false,
191          exec_lua [[
192          return pcall(vim.uri_to_fname, 'foo,://bar')
193        ]]
194        )
195      end)
196
197      it('uri_to_fname returns non-file schema URI with fragment unchanged', function()
198        eq(
199          'scheme://path#fragment',
200          exec_lua [[
201          return vim.uri_to_fname('scheme://path#fragment')
202        ]]
203        )
204      end)
205    end)
206  end)
207
208  describe('uri from bufnr', function()
209    it('Windows paths should not be treated as uris', function()
210      skip(not is_os('win'), 'N/A on non-Windows')
211
212      local file = t.tmpname()
213      write_file(file, 'Test content')
214      local test_case = string.format(
215        [[
216          local file = '%s'
217          return vim.uri_from_bufnr(vim.fn.bufadd(file))
218        ]],
219        file
220      )
221      local expected_uri = ('file:///%s'):format(file)
222      eq(expected_uri, exec_lua(test_case))
223      os.remove(file)
224    end)
225  end)
226
227  describe('uri to bufnr', function()
228    it('uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris', function()
229      local uri =
230        'jdt://contents/java.base/java.util/List.class?=sql/%5C/home%5C/user%5C/.jabba%5C/jdk%5C/openjdk%5C@1.14.0%5C/lib%5C/jrt-fs.jar%60java.base=/javadoc_location=/https:%5C/%5C/docs.oracle.com%5C/en%5C/java%5C/javase%5C/14%5C/docs%5C/api%5C/=/%3Cjava.util(List.class'
231      local test_case = string.format(
232        [[
233        local uri = '%s'
234        return vim.uri_from_bufnr(vim.uri_to_bufnr(uri))
235      ]],
236        uri
237      )
238      eq(uri, exec_lua(test_case))
239    end)
240
241    it(
242      'uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris without authority',
243      function()
244        local uri = 'zipfile:///path/to/archive.zip%3A%3Afilename.txt'
245        local test_case = string.format(
246          [[
247        local uri = '%s'
248        return vim.uri_from_bufnr(vim.uri_to_bufnr(uri))
249      ]],
250          uri
251        )
252        eq(uri, exec_lua(test_case))
253      end
254    )
255  end)
256
257  describe('encode to uri', function()
258    it('rfc2732 including brackets', function()
259      exec_lua("str = '[:]'")
260      exec_lua("rfc = 'rfc2732'")
261      eq('[%3a]', exec_lua('return vim.uri_encode(str, rfc)'))
262    end)
263  end)
264end)