From d0ff66c9abe9d6abbca12fd811e0c3cb69c1033a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 22 Nov 2019 23:26:32 +0800 Subject: =?UTF-8?q?=E6=95=B4=E7=90=86=E4=B8=80=E4=B8=8B=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/test/definition/arg.lua | 19 ++++ script/test/definition/bug.lua | 90 +++++++++++++++++ script/test/definition/emmy.lua | 91 +++++++++++++++++ script/test/definition/function.lua | 24 +++++ script/test/definition/init.lua | 64 ++++++++++++ script/test/definition/label.lua | 29 ++++++ script/test/definition/local.lua | 195 ++++++++++++++++++++++++++++++++++++ script/test/definition/method.lua | 140 ++++++++++++++++++++++++++ script/test/definition/set.lua | 50 +++++++++ script/test/definition/table.lua | 114 +++++++++++++++++++++ 10 files changed, 816 insertions(+) create mode 100644 script/test/definition/arg.lua create mode 100644 script/test/definition/bug.lua create mode 100644 script/test/definition/emmy.lua create mode 100644 script/test/definition/function.lua create mode 100644 script/test/definition/init.lua create mode 100644 script/test/definition/label.lua create mode 100644 script/test/definition/local.lua create mode 100644 script/test/definition/method.lua create mode 100644 script/test/definition/set.lua create mode 100644 script/test/definition/table.lua (limited to 'script/test/definition') diff --git a/script/test/definition/arg.lua b/script/test/definition/arg.lua new file mode 100644 index 00000000..9e88b2bf --- /dev/null +++ b/script/test/definition/arg.lua @@ -0,0 +1,19 @@ +TEST [[ +local function xx () + () +end +]] + +TEST [[ +local +function mt:x() + () +end +mt:x() +]] + +TEST [[ +function mt:x() + () +end +]] diff --git a/script/test/definition/bug.lua b/script/test/definition/bug.lua new file mode 100644 index 00000000..1d3ab02c --- /dev/null +++ b/script/test/definition/bug.lua @@ -0,0 +1,90 @@ +TEST [[ +local +function _(x) +end +function _() + () +end +]] + +TEST [[ +function _() + do return end + () +end +]] + +TEST [[ +local +function a:b() + a:b() + () +end +]] + +TEST [[ +function _(...) + function _() + print() + end +end +]] + +TEST [[ +local +( / b)() +]] + +TEST [[ +local +io.load(root / .source / 'API' / path) +]] + +TEST [[ +obj[#+1] = {} +]] + +TEST [[ +self = { + results = { + = {}, + } +} +self[self.results.] = lbl +]] + +TEST [[ +local mt = {} +function mt:() +end +mt:x() +mt:() +]] + +TEST [[ +local function func() + x = { + xx(), + , + } +end +]] + +TEST [[ +local +local t = { + ..., + , +} +]] + +TEST [[ +local a +local +return f(), +]] + +TEST [[ +local a = os.clock() +local ?> = os.clock() +]] diff --git a/script/test/definition/emmy.lua b/script/test/definition/emmy.lua new file mode 100644 index 00000000..ee8fa6a7 --- /dev/null +++ b/script/test/definition/emmy.lua @@ -0,0 +1,91 @@ +TEST [[ +---@class +---@class B : +]] + +TEST [[ +---@class +---@type B| +]] + +TEST [[ +---@class A +local mt = {} +function mt:() +end + +---@type A +local obj +obj:() +]] + +TEST [[ +---@class A +local = {} +function mt:cast() +end + +---@type A +local +:cast() +]] + +TEST [[ +---@type A +local ?> + +---@class A +local +]] + +TEST [[ +---@type A +local obj +obj:() + +---@class A +local mt +function mt:() +end +]] + +TEST [[ +---@type A +local obj +obj:() + +local mt = {} +mt.__index = mt +function mt:() +end +---@class A +local obj = setmetatable({}, mt) +]] + +TEST [[ +---@alias +---@type +]] + +TEST [[ +---@class +---@param a +]] + +TEST [[ +---@class Class +---@field +---@field id integer +local mt = {} +mt. +]] + +TEST [[ +---@class loli +local unit + +function unit:() +end + +---@see loli# +]] diff --git a/script/test/definition/function.lua b/script/test/definition/function.lua new file mode 100644 index 00000000..ce20f50b --- /dev/null +++ b/script/test/definition/function.lua @@ -0,0 +1,24 @@ + +TEST [[ +function () end +() +]] + +TEST [[ +local function () end +() +]] + +TEST [[ +local x +local function () + () +end +]] + +TEST [[ +local +function () +end +() +]] diff --git a/script/test/definition/init.lua b/script/test/definition/init.lua new file mode 100644 index 00000000..022b1d3b --- /dev/null +++ b/script/test/definition/init.lua @@ -0,0 +1,64 @@ +local core = require 'core' +local parser = require 'parser' +local buildVM = require 'vm' + +rawset(_G, 'TEST', true) + +local function catch_target(script) + local list = {} + local cur = 1 + while true do + local start, finish = script:find('', cur) + if not start then + break + end + list[#list+1] = { start + 2, finish - 2 } + cur = finish + 1 + end + return list +end + +local function founded(targets, results) + if #targets ~= #results then + return false + end + for _, target in ipairs(targets) do + for _, result in ipairs(results) do + if target[1] == result[1] and target[2] == result[2] then + goto NEXT + end + end + do return false end + ::NEXT:: + end + return true +end + +function TEST(script) + local target = catch_target(script) + local start = script:find('', 1, true) + local pos = (start + finish) // 2 + 1 + local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') + assert(ast) + local vm = buildVM(ast) + assert(vm) + + local positions = core.definition(vm, pos, 'definition') + if positions then + assert(founded(target, positions)) + else + assert(#target == 0) + end +end + +require 'definition.set' +require 'definition.local' +require 'definition.arg' +require 'definition.function' +require 'definition.table' +require 'definition.method' +require 'definition.label' +require 'definition.bug' +require 'definition.emmy' diff --git a/script/test/definition/label.lua b/script/test/definition/label.lua new file mode 100644 index 00000000..4859d70e --- /dev/null +++ b/script/test/definition/label.lua @@ -0,0 +1,29 @@ +TEST [[ +:::: +goto +]] + +TEST [[ +goto +:::: +]] + +TEST [[ +::LABEL:: +function _() + goto +end +]] + +TEST [[ +do + goto +end +:::: +]] + +TEST [[ +:::: +goto +:::: +]] diff --git a/script/test/definition/local.lua b/script/test/definition/local.lua new file mode 100644 index 00000000..4153bfa0 --- /dev/null +++ b/script/test/definition/local.lua @@ -0,0 +1,195 @@ +TEST [[ +local +() +]] + +TEST [[ +local z, y, +() +]] + +TEST [[ +local = 1 +() +]] + +TEST [[ +local z, y, = 1 +() +]] + +TEST [[ +local x +local +() +]] + +TEST [[ +local +do + () +end +]] + +TEST [[ +local +do + local x +end +() +]] + +TEST [[ +local +if then + local x +end +]] + +TEST [[ +local +if x then + local x +elseif then + local x +end +]] + +TEST [[ +local +if x then + local x +elseif x then + local x +else + local x +end +() +]] + +TEST [[ +local +if x then + () +elseif x then + local x +else + local x +end +]] + +TEST [[ +local +for x = 1, 10 do +end +() +]] + +TEST [[ +local x +for = 1, 10 do + () +end +]] + +TEST [[ +local +for x in x do +end +() +]] + +TEST [[ +local +for x in do +end +]] + +TEST [[ +local x +for in x do + () +end +]] + +TEST [[ +local x +for z, y, in x do + () +end +]] + +TEST [[ +local +while do +end +]] + +TEST [[ +local +while x do + () +end +]] + +TEST [[ +local +while x do + local x +end +() +]] + +TEST [[ +local +repeat + () +until true +]] + +TEST [[ +local +repeat + local x +until true +() +]] + +TEST [[ +local +repeat +until +]] + +TEST [[ +local x +repeat + local +until +]] + +TEST [[ +local +function _() + local x +end +() +]] + +TEST [[ +local +return function () + () +end +]] + +TEST [[ +local +local x = function () + () +end +]] + +TEST [[ +local ?> +]] diff --git a/script/test/definition/method.lua b/script/test/definition/method.lua new file mode 100644 index 00000000..08b56f61 --- /dev/null +++ b/script/test/definition/method.lua @@ -0,0 +1,140 @@ +TEST [[ +function mt:() +end +function mt:b() + mt:() +end +]] + +TEST [[ +function mt:() +end +function mt:m2() + self:() +end +]] + +TEST [[ +function mt:m3() + mt:() +end +function mt:() +end +]] + +TEST [[ +function mt:m3() + self:() +end +function mt:() +end +]] + +TEST [[ +local mt +mt.__index = mt +function mt:() +end + +local obj = setmetatable({}, mt) +obj:() +]] + +TEST [[ +local mt +mt.__index = mt +function mt:() +end + +local obj = setmetatable({}, mt) +obj:() +]] + +TEST [[ +local mt +function mt:() +end + +local obj = setmetatable({}, { __index = mt }) +obj:() +]] + +TEST [[ +local mt +local api +function mt:() +end + +setmetatable(api, { __index = mt }) +api:() +]] + +TEST [[ +local mt +function mt:x() + self.() +end + +local obj = setmetatable({}, { __index = mt }) +obj. = 1 +obj:x() +]] + +TEST [[ +local mt +function mt:x() + self.() +end + +local obj = setmetatable({ = 1 }, { __index = mt }) +obj:x() +]] + +TEST [[ +local mt +function mt:x() + self.a.() +end + +local obj = setmetatable({ + a = { + = 1, + } +}, { __index = mt }) +obj:x() +]] + +TEST [[ +local sm = setmetatable +local mt +mt.__index = mt +function mt:() +end + +local obj = sm({}, mt) +obj:() +]] + +TEST [[ +local mt = {} +function mt:() +end + +local obj = setmetatable({}, {__index = mt}) +function obj:x() +end + +mt:() +]] + +TEST [[ +local mt = {} +function mt:x() +end + +local obj = setmetatable({}, {__index = mt}) +function obj:() +end + +obj:() +]] diff --git a/script/test/definition/set.lua b/script/test/definition/set.lua new file mode 100644 index 00000000..44277d33 --- /dev/null +++ b/script/test/definition/set.lua @@ -0,0 +1,50 @@ +TEST [[ + = 1 +() +]] + +TEST [[ +do + = 1 +end +() +]] + +TEST [[ + = 1 +do + local x = 1 +end +() +]] + +TEST [[ +x = 1 +do + local = 1 + do + = 2 + end + () +end +]] + +TEST [[ + = 1 +if y then + = 2 +else + = 3 +end +print() +]] + +TEST [[ +_G. = 1 +print() +]] + +TEST [[ +rawset +print() +]] diff --git a/script/test/definition/table.lua b/script/test/definition/table.lua new file mode 100644 index 00000000..90e7926f --- /dev/null +++ b/script/test/definition/table.lua @@ -0,0 +1,114 @@ +TEST [[ +local +a = { + +} +]] + +TEST [[ +local t +t. = 1 +t.() +]] + +TEST [[ +t. = 1 +t.() +]] + +TEST [[ +local +t.x = 1 +.x = 1 +]] + +TEST [[ +t. = 1 +t..y = 1 +]] + +TEST [[ +local t +t. = 1 +t.() +]] + +TEST [[ +local t +t[] = 1 +t[]() +]] + +TEST [[ +local t +t[] = 1 +t[]() +]] + +TEST [[ +local t +t[] = 1 +t[]() +]] + +TEST [[ +local t +t[] = 1 +t[]() +]] + +TEST [[ +local t +t. = 1 +t[]() +]] + +TEST [[ +local t +t. = 1 +t..b() +]] + +TEST [[ +local t +local +t[]() +]] + +TEST[[ +local +local _ = { + _ = +} +]] + +TEST[[ +local +t { + _ = .x +} +]] + +TEST[[ +local t = { + = 1, +} +t.() +]] + +--TEST[[ +--local t = { +-- = 1, +--} +--y.() +--]] + +--TEST[[ +--local t = { +-- = 1, +--} +--local y = { +-- insert = 1, +--} +--t.() +--]] -- cgit v1.2.3