summaryrefslogtreecommitdiff
path: root/test/basic/linker.lua
blob: d563fa4a3561c54c06dae3246e8afd0085a15600 (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 linker = require 'core.linker'
local files  = require 'files'
local util   = require 'utility'
local guide  = require 'core.guide'

local function getSource(pos)
    local ast = files.getAst('')
    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 start  = script:find('<?', 1, true)
        local finish = script:find('?>', 1, true)
        local pos = (start + finish) // 2 + 1
        local newScript = script:gsub('<[!?]', '  '):gsub('[!?]>', '  ')
        files.setText('', newScript)
        local source = getSource(pos)
        assert(source)
        local result = linker.getLink(source)
        for key in pairs(CARE) do
            assert(result[key] == expect[key])
        end
    end
end

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

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

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

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

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

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

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

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

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

TEST [[
{
    <?x?> = 1,
}
]] {
    id   = 'l|1|"x"',
}

CARE['freturn'] = true
TEST [[
return <?X?>
]] {
    id      = 'g|"X"',
    freturn = 0,
}

TEST [[
function f()
    return <?X?>
end
]] {
    id      = 'g|"X"',
    freturn = 1,
}

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

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