aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/treesitter_spec.lua
blob: 8e21faca12d6bf7b66fb18ef4c9283ed19863826 (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
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)

local clear = helpers.clear
local eq = helpers.eq
local insert = helpers.insert
local exec_lua = helpers.exec_lua
local iswin = helpers.iswin
local feed = helpers.feed

before_each(clear)

describe('tree-sitter API', function()
  -- error tests not requiring a parser library
  it('handles basic errors', function()
    --eq({false, 'Error executing lua: vim.schedule: expected function'},
    --   meth_pcall(meths.execute_lua, "parser = vim.treesitter.create_parser(0, 'nosuchlang')", {}))



  end)

  local ts_path = os.getenv("TREE_SITTER_DIR")

  describe('with C parser', function()
    if ts_path == nil then
      it("works", function() pending("TREE_SITTER_PATH not set, skipping tree-sitter parser tests") end)
      return
    end

    before_each(function()
      -- TODO the .so/.dylib/.dll thingie
      local path = ts_path .. '/bin/c'..(iswin() and '.dll' or '.so')
      exec_lua([[
        local path = ...
        vim.treesitter.add_language(path,'c')

      ]], path)
    end)

    it('parses buffer', function()
      insert([[
        int main() {
          int x = 3;
        }]])

      exec_lua([[
        parser = vim.treesitter.get_parser(0, "c")
        tree = parser:parse()
        root = tree:root()
      ]])

      eq("<tree>", exec_lua("return tostring(tree)"))
      eq("<node translation_unit>", exec_lua("return tostring(root)"))
      eq({0,0,3,0}, exec_lua("return {root:range()}"))

      eq(1, exec_lua("return root:child_count()"))
      exec_lua("child = root:child(0)")
      eq("<node function_definition>", exec_lua("return tostring(child)"))
      eq({0,0,2,1}, exec_lua("return {child:range()}"))

      exec_lua("descendant = root:descendant_for_range(1,2,1,12)")
      eq("<node declaration>", exec_lua("return tostring(descendant)"))
      eq({1,2,1,12}, exec_lua("return {descendant:range()}"))
      eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()"))

      eq(true, exec_lua("return child == child"))
      -- separate lua object, but represents same node
      eq(true, exec_lua("return child == root:child(0)"))
      eq(false, exec_lua("return child == descendant2"))
      eq(false, exec_lua("return child == nil"))
      eq(false, exec_lua("return child == tree"))

      feed("2G7|ay")
      exec_lua([[
        tree2 = parser:parse()
        root2 = tree2:root()
        descendant2 = root2:descendant_for_range(1,2,1,13)
      ]])
      eq(false, exec_lua("return tree2 == tree1"))
      eq(false, exec_lua("return root2 == root"))
      eq("<node declaration>", exec_lua("return tostring(descendant2)"))
      eq({1,2,1,13}, exec_lua("return {descendant2:range()}"))

      -- orginal tree did not change
      eq({1,2,1,12}, exec_lua("return {descendant:range()}"))

      -- unchanged buffer: return the same tree
      eq(true, exec_lua("return parser:parse() == tree2"))
    end)

  end)
end)