robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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