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 eq =t . eq 7local command =n . command 8local exec_lua =n . exec_lua 9local pcall_err =t . pcall_err 10local matches =t . matches 11local insert =n . insert 12local NIL =vim . NIL 13 14before_each ( clear ) 15 16describe ( 'treesitter language API' , function () 17-- error tests not requiring a parser library 18it ( 'handles missing language' , function () 19local parser , error =exec_lua ( function () 20return vim . treesitter . get_parser ( 0 , 'borklang' ) 21end ) 22eq ( NIL , parser ) 23matches ( 24'^Parser could not be created for buffer 1 and language "borklang": .+: No parser for language "borklang"$' , 25error 26) 27 28-- actual message depends on platform 29matches ( 30"Failed to load parser for language 'borklang': uv_dlopen: .+" , 31pcall_err ( 32exec_lua , 33"parser = vim.treesitter.language.add('borklang', { path = 'borkbork.so' })" 34) 35) 36 37eq ( NIL , exec_lua ( "return vim.treesitter.language.add('borklang')" )) 38 39eq ( 40false , 41exec_lua ( "return pcall(vim.treesitter.language.add, 'borklang', { path = 'borkbork.so' })" ) 42) 43 44matches ( 45'Failed to load parser: uv_dlsym: .+' , 46pcall_err ( exec_lua , 'vim.treesitter.language.add("c", { symbol_name = "borklang" })' ) 47) 48end ) 49 50it ( 'does not load parser for invalid language name' , function () 51eq ( NIL , exec_lua ( 'vim.treesitter.language.add("/foo/")' )) 52end ) 53 54it ( 'inspects language' , function () 55local keys , fields , symbols =unpack ( exec_lua ( function () 56local lang =vim . treesitter . language . inspect ( 'c' ) 57local keys ={} 58for k , v in pairs ( lang ) do 59if type ( v ) == 'boolean' then 60keys [ k ] =v 61else 62keys [ k ] =true 63end 64end 65 66return { keys , lang . fields , lang . symbols } 67end )) 68 69eq ( { 70abi_version =true , 71fields =true , 72metadata =true , 73symbols =true , 74state_count =true , 75supertypes =true , 76_wasm =false , 77} , keys ) 78 79local fset ={} 80for _ , f in pairs ( fields ) do 81eq ( 'string' , type ( f )) 82fset [ f ] =true 83end 84eq ( true , fset [ 'directive' ]) 85eq ( true , fset [ 'initializer' ]) 86 87local has_named , has_anonymous , has_supertype 88for symbol , named in pairs ( symbols ) do 89eq ( 'string' , type ( symbol )) 90eq ( 'boolean' , type ( named )) 91if symbol == 'for_statement' and named == true then 92has_named =true 93elseif symbol == '"|="' and named == false then 94has_anonymous =true 95elseif symbol == 'statement' and named == true then 96has_supertype =true 97end 98end 99eq ( 100{ has_named =true , has_anonymous =true , has_supertype =true } , 101{ has_named =has_named , has_anonymous =has_anonymous , has_supertype =has_supertype } 102) 103end ) 104 105it ( 106'checks if vim.treesitter.get_parser tries to create a new parser on filetype change' , 107function () 108command ( 'set filetype=c' ) 109-- Should not throw an error when filetype is c 110eq ( 'c' , exec_lua ( 'return vim.treesitter.get_parser(0):lang()' )) 111command ( 'set filetype=borklang' ) 112-- Should throw an error when filetype changes to borklang 113local parser , error =exec_lua ( function () 114return vim . treesitter . get_parser ( 0 , 'borklang' ) 115end ) 116eq ( NIL , parser ) 117matches ( 118'^Parser could not be created for buffer 1 and language "borklang": .+: No parser for language "borklang"$' , 119error 120) 121end 122) 123 124it ( 'retrieve the tree given a range' , function () 125insert ( [[ 126int main() { 127int x = 3; 128}]] ) 129 130eq ( 131'<node translation_unit>' , 132exec_lua ( function () 133local langtree =vim . treesitter . get_parser ( 0 , 'c' ) 134langtree : parse () 135local tree =langtree : tree_for_range ( { 1 , 3 , 1 , 3 } ) 136return tostring ( tree : root ()) 137end ) 138) 139end ) 140 141it ( 'retrieve the tree given a range when range is out of bounds relative to buffer' , function () 142insert ( [[ 143int main() { 144int x = 3; 145}]] ) 146 147eq ( 148'<node translation_unit>' , 149exec_lua ( function () 150local langtree =vim . treesitter . get_parser ( 0 , 'c' ) 151langtree : parse () 152local tree =langtree : tree_for_range ( { 10 , 10 , 10 , 10 } ) 153return tostring ( tree : root ()) 154end ) 155) 156end ) 157 158it ( 'retrieve the node given a range' , function () 159insert ( [[ 160int main() { 161int x = 3; 162}]] ) 163 164eq ( 165'<node primitive_type>' , 166exec_lua ( function () 167local langtree =vim . treesitter . get_parser ( 0 , 'c' ) 168langtree : parse () 169local node =langtree : named_node_for_range ( { 1 , 3 , 1 , 3 } ) 170return tostring ( node ) 171end ) 172) 173end ) 174 175it ( 'retrieve an anonymous node given a range' , function () 176insert ( [[vim.fn.input()]] ) 177 178exec_lua ( function () 179_G . langtree =vim . treesitter . get_parser ( 0 , 'lua' ) 180_G . langtree : parse () 181_G . node =_G . langtree : node_for_range ( { 0 , 3 , 0 , 3 } ) 182end ) 183 184eq ( '.' , exec_lua ( 'return node:type()' )) 185end ) 186end )