summaryrefslogtreecommitdiff
path: root/test/basic/noder.lua
blob: 87b34e12c762c072e77a8721de35654b3463019c (plain)
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
local noder  = require 'core.noder'
local files  = require 'files'
local util   = require 'utility'
local guide  = require 'parser.guide'
local catch  = require 'catch'

local function getSource(pos)
    local ast = files.getState('')
    return guide.eachSourceContain(ast.ast, pos, function (source)
        if source.type == 'local'
        or source.type == 'getlocal'
        or source.type == 'setlocal'
        or source.type == 'setglobal'
        or source.type == 'getglobal'
        or source.type == 'setfield'
        or source.type == 'getfield'
        or source.type == 'setmethod'
        or source.type == 'getmethod'
        or source.type == 'tablefield'
        or source.type == 'setindex'
        or source.type == 'getindex'
        or source.type == 'tableindex'
        or source.type == 'label'
        or source.type == 'goto' then
            return source
        end
    end)
end

local CARE = {}
local function TEST(script)
    return function (expect)
        files.removeAll()
        local newScript, catched = catch(script, '?')
        files.setText('', newScript)
        local source = getSource(catched['?'][1][1])
        assert(source)
        local result = {
            id = noder.getID(source),
        }

        expect['id'] = expect['id']:gsub('|', '\x1F')

        for key in pairs(CARE) do
            assert(result[key] == expect[key])
        end
    end
end

CARE['id'] = true
TEST [[
local <?x?>
]] {
    id   = 'l:6',
}

TEST [[
local x
print(<?x?>)
]] {
    id   = 'l:6',
}

TEST [[
local x
<?x?> = 1
]] {
    id   = 'l:6',
}

TEST [[
print(<?X?>)
]] {
    id   = 'g:.X',
}

TEST [[
print(<?X?>)
]] {
    id   = 'g:.X',
}

TEST [[
local x
print(x.y.<?z?>)
]] {
    id   = 'l:6|.y|.z',
}

TEST [[
local x
function x:<?f?>() end
]] {
    id   = 'l:6|.f',
}

TEST [[
print(X.Y.<?Z?>)
]] {
    id   = 'g:.X|.Y|.Z',
}

TEST [[
function x:<?f?>() end
]] {
    id   = 'g:.x|.f',
}

TEST [[
{
    <?x?> = 1,
}
]] {
    id   = 't:0|.x',
}

TEST [[
return <?X?>
]] {
    id      = 'g:.X',
}

TEST [[
function f()
    return <?X?>
end
]] {
    id      = 'g:.X',
}

TEST [[
::<?label?>::
goto label
]] {
    id      = 'l:2',
}

TEST [[
::label::
goto <?label?>
]] {
    id      = 'l:2',
}