aboutsummaryrefslogtreecommitdiff
path: root/test/functional/treesitter/language_spec.lua
blob: e1e34fcecc55bacc1a8b693f426602d7ae497488 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local clear = n.clear
local eq = t.eq
local command = n.command
local exec_lua = n.exec_lua
local pcall_err = t.pcall_err
local matches = t.matches
local insert = n.insert
local NIL = vim.NIL

before_each(clear)

describe('treesitter language API', function()
  -- error tests not requiring a parser library
  it('handles missing language', function()
    eq(
      '.../treesitter.lua:0: Parser could not be created for buffer 1 and language "borklang"',
      pcall_err(exec_lua, "parser = vim.treesitter.get_parser(0, 'borklang')")
    )

    eq(NIL, exec_lua("return vim.treesitter.get_parser(0, 'borklang', { error = false })"))

    -- actual message depends on platform
    matches(
      "Failed to load parser for language 'borklang': uv_dlopen: .+",
      pcall_err(
        exec_lua,
        "parser = vim.treesitter.language.add('borklang', { path = 'borkbork.so' })"
      )
    )

    eq(NIL, exec_lua("return vim.treesitter.language.add('borklang')"))

    eq(
      false,
      exec_lua("return pcall(vim.treesitter.language.add, 'borklang', { path = 'borkbork.so' })")
    )

    matches(
      'Failed to load parser: uv_dlsym: .+',
      pcall_err(exec_lua, 'vim.treesitter.language.add("c", { symbol_name = "borklang" })')
    )
  end)

  it('does not load parser for invalid language name', function()
    eq(NIL, exec_lua('vim.treesitter.language.add("/foo/")'))
  end)

  it('inspects language', function()
    local keys, fields, symbols = unpack(exec_lua(function()
      local lang = vim.treesitter.language.inspect('c')
      local keys, symbols = {}, {}
      for k, v in pairs(lang) do
        if type(v) == 'boolean' then
          keys[k] = v
        else
          keys[k] = true
        end
      end

      -- symbols array can have "holes" and is thus not a valid msgpack array
      -- but we don't care about the numbers here (checked in the parser test)
      for _, v in pairs(lang.symbols) do
        table.insert(symbols, v)
      end
      return { keys, lang.fields, symbols }
    end))

    eq({ fields = true, symbols = true, _abi_version = true, _wasm = false }, keys)

    local fset = {}
    for _, f in pairs(fields) do
      eq('string', type(f))
      fset[f] = true
    end
    eq(true, fset['directive'])
    eq(true, fset['initializer'])

    local has_named, has_anonymous
    for _, s in pairs(symbols) do
      eq('string', type(s[1]))
      eq('boolean', type(s[2]))
      if s[1] == 'for_statement' and s[2] == true then
        has_named = true
      elseif s[1] == '|=' and s[2] == false then
        has_anonymous = true
      end
    end
    eq({ true, true }, { has_named, has_anonymous })
  end)

  it(
    'checks if vim.treesitter.get_parser tries to create a new parser on filetype change',
    function()
      command('set filetype=c')
      -- Should not throw an error when filetype is c
      eq('c', exec_lua('return vim.treesitter.get_parser(0):lang()'))
      command('set filetype=borklang')
      -- Should throw an error when filetype changes to borklang
      eq(
        '.../treesitter.lua:0: Parser could not be created for buffer 1 and language "borklang"',
        pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0, 'borklang')")
      )
      eq(NIL, exec_lua("return vim.treesitter.get_parser(0, 'borklang', { error = false })"))
    end
  )

  it('retrieve the tree given a range', function()
    insert([[
      int main() {
        int x = 3;
      }]])

    eq(
      '<node translation_unit>',
      exec_lua(function()
        local langtree = vim.treesitter.get_parser(0, 'c')
        local tree = langtree:tree_for_range({ 1, 3, 1, 3 })
        return tostring(tree:root())
      end)
    )
  end)

  it('retrieve the tree given a range when range is out of bounds relative to buffer', function()
    insert([[
      int main() {
        int x = 3;
      }]])

    eq(
      '<node translation_unit>',
      exec_lua(function()
        local langtree = vim.treesitter.get_parser(0, 'c')
        local tree = langtree:tree_for_range({ 10, 10, 10, 10 })
        return tostring(tree:root())
      end)
    )
  end)

  it('retrieve the node given a range', function()
    insert([[
      int main() {
        int x = 3;
      }]])

    eq(
      '<node primitive_type>',
      exec_lua(function()
        local langtree = vim.treesitter.get_parser(0, 'c')
        local node = langtree:named_node_for_range({ 1, 3, 1, 3 })
        return tostring(node)
      end)
    )
  end)

  it('retrieve an anonymous node given a range', function()
    insert([[vim.fn.input()]])

    exec_lua(function()
      _G.langtree = vim.treesitter.get_parser(0, 'lua')
      _G.node = _G.langtree:node_for_range({ 0, 3, 0, 3 })
    end)

    eq('.', exec_lua('return node:type()'))
  end)
end)