robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
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 () 13before_each ( function () 14clear () 15end ) 16 17describe ( 'file path to uri' , function () 18describe ( 'encode Unix file path' , function () 19it ( 'file path includes only ascii characters' , function () 20exec_lua ( "filepath = '/Foo/Bar/Baz.txt'" ) 21 22eq ( 'file:///Foo/Bar/Baz.txt' , exec_lua ( 'return vim.uri_from_fname(filepath)' )) 23end ) 24 25it ( 'file path including white space' , function () 26exec_lua ( "filepath = '/Foo /Bar/Baz.txt'" ) 27 28eq ( 'file:///Foo%20/Bar/Baz.txt' , exec_lua ( 'return vim.uri_from_fname(filepath)' )) 29end ) 30 31it ( 'file path including Unicode characters' , function () 32exec_lua ( "filepath = '/xy/åäö/ɧ/汉语/↥/🤦/🦄/å/بِيَّ.txt'" ) 33 34-- The URI encoding should be case-insensitive 35eq ( 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' , 37exec_lua ( 'return vim.uri_from_fname(filepath)' ) 38) 39end ) 40end ) 41 42describe ( 'encode Windows filepath' , function () 43it ( 'file path includes only ascii characters' , function () 44exec_lua ( [[filepath = 'C:\\Foo\\Bar\\Baz.txt']] ) 45 46eq ( 'file:///C:/Foo/Bar/Baz.txt' , exec_lua ( 'return vim.uri_from_fname(filepath)' )) 47end ) 48 49it ( 'file path including white space' , function () 50exec_lua ( [[filepath = 'C:\\Foo \\Bar\\Baz.txt']] ) 51 52eq ( 'file:///C:/Foo%20/Bar/Baz.txt' , exec_lua ( 'return vim.uri_from_fname(filepath)' )) 53end ) 54 55it ( 'file path including Unicode characters' , function () 56exec_lua ( [[filepath = 'C:\\xy\\åäö\\ɧ\\汉语\\↥\\🤦\\🦄\\å\\بِيَّ.txt']] ) 57 58eq ( 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' , 60exec_lua ( 'return vim.uri_from_fname(filepath)' ) 61) 62end ) 63end ) 64end ) 65 66describe ( 'uri to filepath' , function () 67describe ( 'decode Unix file path' , function () 68it ( 'file path includes only ascii characters' , function () 69exec_lua ( "uri = 'file:///Foo/Bar/Baz.txt'" ) 70 71eq ( '/Foo/Bar/Baz.txt' , exec_lua ( 'return vim.uri_to_fname(uri)' )) 72end ) 73 74it ( 'local file path without hostname' , function () 75exec_lua ( "uri = 'file:/Foo/Bar/Baz.txt'" ) 76 77eq ( '/Foo/Bar/Baz.txt' , exec_lua ( 'return vim.uri_to_fname(uri)' )) 78end ) 79 80it ( 'file path including white space' , function () 81exec_lua ( "uri = 'file:///Foo%20/Bar/Baz.txt'" ) 82 83eq ( '/Foo /Bar/Baz.txt' , exec_lua ( 'return vim.uri_to_fname(uri)' )) 84end ) 85 86it ( 'file path including Unicode characters' , function () 87local test_case =[[ 88local 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' 89return vim.uri_to_fname(uri) 90]] 91 92eq ( '/xy/åäö/ɧ/汉语/↥/🤦/🦄/å/بِيَّ.txt' , exec_lua ( test_case )) 93end ) 94 95it ( 'file path with uri fragment' , function () 96exec_lua ( "uri = 'file:///Foo/Bar/Baz.txt#fragment'" ) 97 98eq ( '/Foo/Bar/Baz.txt' , exec_lua ( 'return vim.uri_to_fname(uri)' )) 99end ) 100end ) 101 102describe ( 'decode Windows filepath' , function () 103it ( 'file path includes only ascii characters' , function () 104local test_case =[[ 105local uri = 'file:///C:/Foo/Bar/Baz.txt' 106return vim.uri_to_fname(uri) 107]] 108 109eq ( 'C:\\Foo\\Bar\\Baz.txt' , exec_lua ( test_case )) 110end ) 111 112it ( 'local file path without hostname' , function () 113local test_case =[[ 114local uri = 'file:/C:/Foo/Bar/Baz.txt' 115return vim.uri_to_fname(uri) 116]] 117 118eq ( 'C:\\Foo\\Bar\\Baz.txt' , exec_lua ( test_case )) 119end ) 120 121it ( 'file path includes only ascii characters with encoded colon character' , function () 122local test_case =[[ 123local uri = 'file:///C%3A/Foo/Bar/Baz.txt' 124return vim.uri_to_fname(uri) 125]] 126 127eq ( 'C:\\Foo\\Bar\\Baz.txt' , exec_lua ( test_case )) 128end ) 129 130it ( 'file path including white space' , function () 131local test_case =[[ 132local uri = 'file:///C:/Foo%20/Bar/Baz.txt' 133return vim.uri_to_fname(uri) 134]] 135 136eq ( 'C:\\Foo \\Bar\\Baz.txt' , exec_lua ( test_case )) 137end ) 138 139it ( 'file path including Unicode characters' , function () 140local test_case =[[ 141local 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' 142return vim.uri_to_fname(uri) 143]] 144 145eq ( 'C:\\xy\\åäö\\ɧ\\汉语\\↥\\🤦\\🦄\\å\\بِيَّ.txt' , exec_lua ( test_case )) 146end ) 147end ) 148 149describe ( 'decode non-file URI' , function () 150it ( 'uri_to_fname returns non-file URI unchanged' , function () 151eq ( 152'jdt1.23+x-z://content/%5C/' , 153exec_lua [[ 154return vim.uri_to_fname('jdt1.23+x-z://content/%5C/') 155]] 156) 157end ) 158 159it ( 'uri_to_fname returns non-file upper-case scheme URI unchanged' , function () 160eq ( 161'JDT://content/%5C/' , 162exec_lua [[ 163return vim.uri_to_fname('JDT://content/%5C/') 164]] 165) 166end ) 167 168it ( 'uri_to_fname returns non-file scheme URI without authority unchanged' , function () 169eq ( 170'zipfile:///path/to/archive.zip%3A%3Afilename.txt' , 171exec_lua [[ 172return vim.uri_to_fname('zipfile:///path/to/archive.zip%3A%3Afilename.txt') 173]] 174) 175end ) 176end ) 177 178describe ( 'decode URI without scheme' , function () 179it ( 'fails because URI must have a scheme' , function () 180eq ( 181false , 182exec_lua [[ 183return pcall(vim.uri_to_fname, 'not_an_uri.txt') 184]] 185) 186end ) 187 188it ( 'uri_to_fname should not treat comma as a scheme character' , function () 189eq ( 190false , 191exec_lua [[ 192return pcall(vim.uri_to_fname, 'foo,://bar') 193]] 194) 195end ) 196 197it ( 'uri_to_fname returns non-file schema URI with fragment unchanged' , function () 198eq ( 199'scheme://path#fragment' , 200exec_lua [[ 201return vim.uri_to_fname('scheme://path#fragment') 202]] 203) 204end ) 205end ) 206end ) 207 208describe ( 'uri from bufnr' , function () 209it ( 'Windows paths should not be treated as uris' , function () 210skip ( not is_os ( 'win' ), 'N/A on non-Windows' ) 211 212local file =t . tmpname () 213write_file ( file , 'Test content' ) 214local test_case =string . format ( 215[[ 216local file = '%s' 217return vim.uri_from_bufnr(vim.fn.bufadd(file)) 218]] , 219file 220) 221local expected_uri =( 'file:///%s' ): format ( file ) 222eq ( expected_uri , exec_lua ( test_case )) 223os . remove ( file ) 224end ) 225end ) 226 227describe ( 'uri to bufnr' , function () 228it ( 'uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris' , function () 229local 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' 231local test_case =string . format ( 232[[ 233local uri = '%s' 234return vim.uri_from_bufnr(vim.uri_to_bufnr(uri)) 235]] , 236uri 237) 238eq ( uri , exec_lua ( test_case )) 239end ) 240 241it ( 242'uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris without authority' , 243function () 244local uri ='zipfile:///path/to/archive.zip%3A%3Afilename.txt' 245local test_case =string . format ( 246[[ 247local uri = '%s' 248return vim.uri_from_bufnr(vim.uri_to_bufnr(uri)) 249]] , 250uri 251) 252eq ( uri , exec_lua ( test_case )) 253end 254) 255end ) 256 257describe ( 'encode to uri' , function () 258it ( 'rfc2732 including brackets' , function () 259exec_lua ( "str = '[:]'" ) 260exec_lua ( "rfc = 'rfc2732'" ) 261eq ( '[%3a]' , exec_lua ( 'return vim.uri_encode(str, rfc)' )) 262end ) 263end ) 264end )