summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/basic/init.lua221
-rw-r--r--test/basic/linker.lua146
-rw-r--r--test/basic/linker.txt141
-rw-r--r--test/basic/textmerger.lua219
-rw-r--r--test/definition/init.lua2
-rw-r--r--test/definition/luadoc.lua65
-rw-r--r--test/references/init.lua35
7 files changed, 603 insertions, 226 deletions
diff --git a/test/basic/init.lua b/test/basic/init.lua
index a3a11f62..d3a84152 100644
--- a/test/basic/init.lua
+++ b/test/basic/init.lua
@@ -1,219 +1,2 @@
-local files = require 'files'
-local tm = require 'text-merger'
-
-local function TEST(source)
- return function (expect)
- return function (changes)
- files.removeAll()
- files.setText('', source)
- local text = tm('', changes)
- assert(text == expect)
- end
- end
-end
-
-TEST [[
-
-
-function Test(self)
-
-end
-]][[
-
-
-function Test(self)
-
-end
-
-asser]]{
- [1] = {
- range = {
- ["end"] = {
- character = 0,
- line = 5,
- },
- start = {
- character = 0,
- line = 5,
- },
- },
- rangeLength = 0,
- text = "\
-",
- },
- [2] = {
- range = {
- ["end"] = {
- character = 0,
- line = 6,
- },
- start = {
- character = 0,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "a",
- },
- [3] = {
- range = {
- ["end"] = {
- character = 1,
- line = 6,
- },
- start = {
- character = 1,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "s",
- },
- [4] = {
- range = {
- ["end"] = {
- character = 2,
- line = 6,
- },
- start = {
- character = 2,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "s",
- },
- [5] = {
- range = {
- ["end"] = {
- character = 3,
- line = 6,
- },
- start = {
- character = 3,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "e",
- },
- [6] = {
- range = {
- ["end"] = {
- character = 4,
- line = 6,
- },
- start = {
- character = 4,
- line = 6,
- },
- },
- rangeLength = 0,
- text = "r",
- },
-}
-
-TEST [[
-local mt = {}
-
-function mt['xxx']()
-
-
-
-end
-]] [[
-local mt = {}
-
-function mt['xxx']()
-
-end
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 4,
- line = 5,
- },
- start = {
- character = 4,
- line = 3,
- },
- },
- rangeLength = 8,
- text = "",
- },
-}
-
-TEST [[
-local mt = {}
-
-function mt['xxx']()
-
-end
-]] [[
-local mt = {}
-
-function mt['xxx']()
- p
-end
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 4,
- line = 3,
- },
- start = {
- character = 4,
- line = 3,
- },
- },
- rangeLength = 0,
- text = "p",
- },
-}
-
-TEST [[
-print(12345)
-]] [[
-print(123
-45)
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 9,
- line = 0,
- },
- start = {
- character = 9,
- line = 0,
- },
- },
- rangeLength = 0,
- text = "\
-",
- },
-}
-
-TEST [[
-print(123
-45)
-]] [[
-print(12345)
-]] {
- [1] = {
- range = {
- ["end"] = {
- character = 0,
- line = 1,
- },
- start = {
- character = 9,
- line = 0,
- },
- },
- rangeLength = 2,
- text = "",
- },
-}
+require 'basic.textmerger'
+require 'basic.linker'
diff --git a/test/basic/linker.lua b/test/basic/linker.lua
new file mode 100644
index 00000000..d5525c40
--- /dev/null
+++ b/test/basic/linker.lua
@@ -0,0 +1,146 @@
+local linker = require 'core.linker'
+local files = require 'files'
+local util = require 'utility'
+local guide = require 'parser.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)
+ linker.compileLinks(source)
+ local result = {
+ id = linker.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: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 = 't:1|"x"',
+}
+
+TEST [[
+return <?X?>
+]] {
+ id = 'g:"X"',
+}
+
+TEST [[
+function f()
+ return <?X?>
+end
+]] {
+ id = 'g:"X"',
+}
+
+TEST [[
+::<?label?>::
+goto label
+]] {
+ id = 'l:5',
+}
+
+TEST [[
+::label::
+goto <?label?>
+]] {
+ id = 'l:3',
+}
diff --git a/test/basic/linker.txt b/test/basic/linker.txt
new file mode 100644
index 00000000..ea3ba180
--- /dev/null
+++ b/test/basic/linker.txt
@@ -0,0 +1,141 @@
+ast -> linkers = {
+ ['g|"X"|"Y"|"Z"'] = {src1, src2, src3},
+ ['g|"X"|"Y"'] = {src4, src5, src6},
+ ['g|"X"'] = {src7, src8, src9},
+ ['l|7'] = {src10},
+ ['l|7|"x"'] = {src11},
+ ['l|11|"k"'] = {src12},
+}
+
+```lua
+x.y.<?z?> = <!f!>
+
+<?g?> = x.y.z
+
+t.<!z!> = 1
+x.y = t
+
+x = {
+ y = {
+ <!z!> = 1
+ }
+}
+```
+
+expect: 'l|x|y|z'
+forward: 'l|x|y|z' -> f
+backward: 'l|x|y|z' -> g
+last: 'l|x|y' + 'z'
+
+expect: 'l|x|y' + '|z'
+forward: 'l|t' + '|z' -> 'l|t|z' -> t.z
+backward: nil
+last: 'l|x' + '|y|z'
+
+expect: 'l|x' + '|y|z'
+forward: 'l|0' + '|y|z' -> 'l|0|y|z'
+backward: nil
+last: nil
+
+expect: 'l|0|y|z'
+forward: nil
+backward: nil
+last: 'l|0|y' + '|z'
+
+expect: 'l|0|y' + '|z'
+forward: 'l|1'+ '|z' -> 'l|1|z' -> field z
+backward: nil
+last: 'l|0' + '|y|z'
+
+
+```lua
+a = {
+ b = {
+ <?c?> = 1,
+ }
+}
+
+print(a.b.<!c!>)
+```
+
+expect: 't|3|c'
+forward: nil
+backward: nil
+last: 't|3' + '|c'
+
+expect: 't|3' + '|c'
+forward: nil
+backward: 't|2|b' + '|c'
+last: nil
+
+expect: 't|2|b|c'
+forward: nil
+backward: 't|2|b' + '|c'
+last: nil
+
+```lua
+---@return <?A?>
+local function f()
+end
+
+local <!x!> = f()
+```
+
+'d|A'
+'f|1|#1'
+'f|1' + '|#1'
+'l|1' + '|#1'
+'s|1' + '|#1'
+
+```lua
+---@generic T
+---@param a T
+---@return T
+local function f(a) end
+
+local <?c?>
+
+local <!v!> = f(c)
+```
+
+'l1'
+'l2|@1'
+'f|1|@1'
+'f|1|#1'
+
+```
+---@generic T
+---@param p T
+---@return T
+local function f(p) end
+
+local <?r?> = f(<!k!>)
+```
+
+l:r
+s:1#1 call
+l:f#1 call
+f:1#1 call -> f:1&T = l:k
+l:f@1 --> 从保存的call信息里找到 f:1&T = l:k
+l:k
+
+
+
+```
+---@generic T, V
+---@param p T
+---@return fun(V):T, V
+local function f(p) end
+
+local f2 = f(<!k!>)
+local <?r?> = f2()
+```
+
+l:r
+s:2|#1 call1
+l:f2|#1 call1
+f:2|#1 call1
+s:1#1|#1 call2
+f:1#1|#1 call2 -> f:1&T = l:k
+dfun:1|#1
+dn:V -> f:1&T = l:k
diff --git a/test/basic/textmerger.lua b/test/basic/textmerger.lua
new file mode 100644
index 00000000..a3a11f62
--- /dev/null
+++ b/test/basic/textmerger.lua
@@ -0,0 +1,219 @@
+local files = require 'files'
+local tm = require 'text-merger'
+
+local function TEST(source)
+ return function (expect)
+ return function (changes)
+ files.removeAll()
+ files.setText('', source)
+ local text = tm('', changes)
+ assert(text == expect)
+ end
+ end
+end
+
+TEST [[
+
+
+function Test(self)
+
+end
+]][[
+
+
+function Test(self)
+
+end
+
+asser]]{
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 0,
+ line = 5,
+ },
+ start = {
+ character = 0,
+ line = 5,
+ },
+ },
+ rangeLength = 0,
+ text = "\
+",
+ },
+ [2] = {
+ range = {
+ ["end"] = {
+ character = 0,
+ line = 6,
+ },
+ start = {
+ character = 0,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "a",
+ },
+ [3] = {
+ range = {
+ ["end"] = {
+ character = 1,
+ line = 6,
+ },
+ start = {
+ character = 1,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "s",
+ },
+ [4] = {
+ range = {
+ ["end"] = {
+ character = 2,
+ line = 6,
+ },
+ start = {
+ character = 2,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "s",
+ },
+ [5] = {
+ range = {
+ ["end"] = {
+ character = 3,
+ line = 6,
+ },
+ start = {
+ character = 3,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "e",
+ },
+ [6] = {
+ range = {
+ ["end"] = {
+ character = 4,
+ line = 6,
+ },
+ start = {
+ character = 4,
+ line = 6,
+ },
+ },
+ rangeLength = 0,
+ text = "r",
+ },
+}
+
+TEST [[
+local mt = {}
+
+function mt['xxx']()
+
+
+
+end
+]] [[
+local mt = {}
+
+function mt['xxx']()
+
+end
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 4,
+ line = 5,
+ },
+ start = {
+ character = 4,
+ line = 3,
+ },
+ },
+ rangeLength = 8,
+ text = "",
+ },
+}
+
+TEST [[
+local mt = {}
+
+function mt['xxx']()
+
+end
+]] [[
+local mt = {}
+
+function mt['xxx']()
+ p
+end
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 4,
+ line = 3,
+ },
+ start = {
+ character = 4,
+ line = 3,
+ },
+ },
+ rangeLength = 0,
+ text = "p",
+ },
+}
+
+TEST [[
+print(12345)
+]] [[
+print(123
+45)
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 9,
+ line = 0,
+ },
+ start = {
+ character = 9,
+ line = 0,
+ },
+ },
+ rangeLength = 0,
+ text = "\
+",
+ },
+}
+
+TEST [[
+print(123
+45)
+]] [[
+print(12345)
+]] {
+ [1] = {
+ range = {
+ ["end"] = {
+ character = 0,
+ line = 1,
+ },
+ start = {
+ character = 9,
+ line = 0,
+ },
+ },
+ rangeLength = 2,
+ text = "",
+ },
+}
diff --git a/test/definition/init.lua b/test/definition/init.lua
index 6e6d0a9a..af302669 100644
--- a/test/definition/init.lua
+++ b/test/definition/init.lua
@@ -65,6 +65,6 @@ require 'definition.table'
require 'definition.method'
require 'definition.label'
require 'definition.call'
-require 'definition.bug'
require 'definition.special'
+require 'definition.bug'
require 'definition.luadoc'
diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua
index ff54546b..74cc5c81 100644
--- a/test/definition/luadoc.lua
+++ b/test/definition/luadoc.lua
@@ -87,6 +87,11 @@ TEST [[
]]
TEST [[
+---@type <!fun():void!>
+local <?<!f!>?>
+]]
+
+TEST [[
---@param f <!fun():void!>
function t(<?<!f!>?>) end
]]
@@ -204,6 +209,23 @@ TEST [[
]]
TEST [[
+---@return <!fun()!>
+local function f() end
+
+local <?<!r!>?> = f()
+]]
+
+TEST [[
+---@generic T
+---@param p T
+---@return T
+local function f(p) end
+
+local <!k!>
+local <?<!r!>?> = f(k)
+]]
+
+TEST [[
---@class Foo
local Foo = {}
function Foo:<!bar1!>() end
@@ -299,13 +321,46 @@ print(v1[1].<?bar1?>)
--]]
TEST [[
+---@type fun():<!fun()!>
+local f
+
+local <?<!f2!>?> = f()
+]]
+
+TEST [[
+---@generic T
+---@param x T
+---@return fun():T
+local function f(x) end
+
+local v1 = f(<!{}!>)
+local <?<!v2!>?> = v1()
+]]
+
+TEST [[
+---@generic V
+---@return fun(x: V):V
+local function f(x) end
+
+local v1 = f()
+local <?<!v2!>?> = v1(<!{}!>)
+]]
+
+TEST [[
---@class Foo
local Foo = {}
function Foo:<!bar1!>() end
---@type table<number, Foo>
local v1
-local ipairs = ipairs
+
+---@generic T: table, V
+---@param t T
+---@return fun(table: V[], i?: integer):integer, V
+---@return T
+---@return integer i
+local function ipairs(t) end
+
for i, v in ipairs(v1) do
print(v.<?bar1?>)
end
@@ -318,6 +373,14 @@ function Foo:<!bar1!>() end
---@type table<Foo, Foo>
local v1
+
+---@generic T: table, K, V
+---@param t T
+---@return fun(table: table<K, V>, index: K):K, V
+---@return T
+---@return nil
+local function pairs(t) end
+
for k, v in pairs(v1) do
print(k.<?bar1?>)
print(v.bar1)
diff --git a/test/references/init.lua b/test/references/init.lua
index c4e5018a..f6785984 100644
--- a/test/references/init.lua
+++ b/test/references/init.lua
@@ -1,4 +1,4 @@
-local core = require 'core.reference'
+local core = require 'core.reference'
local files = require 'files'
local function catch_target(script)
@@ -33,7 +33,7 @@ end
function TEST(script)
files.removeAll()
- local target = catch_target(script)
+ local expect = catch_target(script)
local start = script:find('<[?~]')
local finish = script:find('[?~]>')
local pos = (start + finish) // 2 + 1
@@ -46,9 +46,9 @@ function TEST(script)
for i, result in ipairs(results) do
positions[i] = { result.target.start, result.target.finish }
end
- assert(founded(target, positions))
+ assert(founded(expect, positions))
else
- assert(#target == 0)
+ assert(#expect == 0)
end
end
@@ -96,6 +96,16 @@ local <?a?> = 1
]]
TEST [[
+local <!a!>
+local <?b?> = <!a!>
+]]
+
+TEST [[
+local <?a?>
+local <!b!> = <!a!>
+]]
+
+TEST [[
local t = {
<!a!> = 1
}
@@ -252,7 +262,7 @@ a.<!t!> = <?f?>
]]
TEST [[
-<!t!>.f = <?t?>
+<!t!>.<!f!> = <?t?>
]]
TEST [[
@@ -349,6 +359,21 @@ local a, b = f()
return a.x, b.<!x!>
]]
+-- TODO 支持泛型
+do return end
+TEST [[
+---@class Dog
+local <?Dog?> = {}
+
+---@generic T
+---@param type1 T
+---@return T
+function foobar(type1)
+end
+
+local <!v1!> = foobar(<!Dog!>)
+]]
+
TEST [[
---@class Dog
local Dog = {}