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/diagnostics/init.lua | 74 ++++++ script/test/diagnostics/normal.lua | 503 +++++++++++++++++++++++++++++++++++++ script/test/diagnostics/syntax.lua | 8 + 3 files changed, 585 insertions(+) create mode 100644 script/test/diagnostics/init.lua create mode 100644 script/test/diagnostics/normal.lua create mode 100644 script/test/diagnostics/syntax.lua (limited to 'script/test/diagnostics') diff --git a/script/test/diagnostics/init.lua b/script/test/diagnostics/init.lua new file mode 100644 index 00000000..eee03d2e --- /dev/null +++ b/script/test/diagnostics/init.lua @@ -0,0 +1,74 @@ +local core = require 'core' +local buildVM = require 'vm' +local parser = require 'parser' +local service = require 'service' +local config = require 'config' + +rawset(_G, 'TEST', true) + +local function catch_target(script, ...) + local list = {} + local function catch(buf) + local cur = 1 + local cut = 0 + while true do + local start, finish = buf:find('', cur) + if not start then + break + end + list[#list+1] = { start - cut, finish - 4 - cut } + cur = finish + 1 + cut = cut + 4 + end + end + catch(script) + if ... then + for _, buf in ipairs {...} do + catch(buf) + end + end + local new_script = script:gsub('', '%1') + return new_script, 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 new_script, target = catch_target(script, ...) + local lsp = service() + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') + assert(ast) + local lines = parser:lines(new_script) + local vm = buildVM(ast, lsp, 'test') + assert(vm) + local datas = core.diagnostics(vm, lines, 'test') + local results = {} + for i, data in ipairs(datas) do + results[i] = { data.start, data.finish } + end + + if results[1] then + if not founded(target, results) then + error(('%s\n%s'):format(table.dump(target), table.dump(results))) + end + else + assert(#target == 0) + end +end + +require 'diagnostics.normal' +require 'diagnostics.syntax' diff --git a/script/test/diagnostics/normal.lua b/script/test/diagnostics/normal.lua new file mode 100644 index 00000000..5fdc3ba8 --- /dev/null +++ b/script/test/diagnostics/normal.lua @@ -0,0 +1,503 @@ +local config = require 'config' + +TEST [[ +local +]] + +TEST([[ + +]], +[[ +local function () +end +]] +) + +TEST [[ +local = +]] + +TEST [[ +local +x = +]] + + +TEST [[ +print() +print() +print() +print() +print(_VERSION) +print() +print(Z) +Z = 1 +]] + +TEST [[ +:::: +]] + +TEST [[ + +]] + +TEST [[ +X = 1 +]] + +TEST [[ +X = [=[ + ]=] +]] + +TEST [[ +local x +print(x) +local +print(x) +]] + +TEST [[ +local x +print(x) +local +print(x) +local +print(x) +]] + +TEST [[ +local _ +print(_) +local _ +print(_) +local _ENV +(_ENV) -- 由于重定义了_ENV,因此print变为了未定义全局变量 +]] + +TEST [[ +print(1) +_ENV = nil +]] + +config.config.diagnostics.disable['undefined-env-child'] = true +TEST [[ +_ENV = nil + = 1 --> _ENV.GLOBAL = 1 +]] + +TEST [[ +_ENV = nil +local _ = --> local _ = _ENV.GLOBAL +]] + +TEST [[ +_ENV = {} +GLOBAL = 1 --> _ENV.GLOBAL = 1 +]] + +TEST [[ +_ENV = {} +local _ = GLOBAL --> local _ = _ENV.GLOBAL +]] + +config.config.diagnostics.disable['undefined-env-child'] = nil +TEST [[ +print() +:sub(1, 1) +]] + +TEST [[ +print() +('string') +]] + +TEST [[ +return { + +} +]] + +TEST [[ +return { + +} +]] + +TEST [[ +print() +'string' +]] + +TEST [[ +print +{ + x = 1, +} +]] + +TEST [[ +local function x(a, b) + return a, b +end +x(1, 2, ) +]] + +TEST [[ +InstanceName = 1 +Instance = _G[InstanceName] +]] + +TEST [[ +(''):sub(1, 2) +]] + +TEST [=[ +return [[ + +]] +]=] + +TEST [[ +local mt, x +function mt:m() + function x:m() + end +end +]] + +TEST [[ +local mt = {} +function mt:f() +end +]] + +TEST [[ +local function f() +end +f() +]] + +TEST [[ +local function f() +end +f() +]] + +TEST [[ +local function f(var) + print(var) +end +local var +f(var) +]] + +TEST [[ +local function f(a, b) + return a, b +end +f(1, 2, , ) +]] + +TEST [[ +local mt = {} +function mt:f(a, b) + return a, b +end +mt.f(1, 2, 3, ) +]] + + +TEST [[ +local mt = {} +function mt.f(a, b) + return a, b +end +mt:f(1, , , ) +]] + +TEST [[ +local mt = {} +function mt:f(a, b) + return a, b +end +mt:f(1, 2, , ) +]] + +TEST [[ +local function f(a, b, ...) + return a, b, ... +end +f(1, 2, 3, 4) +]] + +TEST [[ +next({}, 1, ) +print(1, 2, 3, 4, 5) +]] + +TEST [[ +local function f(callback) + callback(1, 2, 3) +end +f(function () end) +]] + +--TEST [[ +--local realTostring = tostring +--tostring = function () end +--tostring() +--tostring = realTostring +--tostring(1) +--]] + +TEST [[ + = 1 +tostring = 1 +ROOT = 1 +_G.bb = 1 +]] + +TEST [[ +local f = load('') +f(1, 2, 3) +]] + +require 'config' .config.runtime.version = 'Lua 5.3' +TEST [[ +(1) +]] + +TEST [[ +X = table[] +]] + +TEST [[ +return { + , + y = 2, + = 3, +} +]] + +TEST [[ +local m = {} +function m.open() +end + +m:open() +]] + +TEST [[ + +]] + +TEST [[ + +]] + +TEST [[ +if true then +else + return +end +]] + +TEST [[ +while true do +end +]] + +TEST [[ + +]] + +TEST [[ + +]] + +TEST [[ +local _ = 1, +]] + +TEST [[ +_ = 1, +]] + +TEST [[ +local function x() + do + local k + print(k) + x() + end + local k = 1 + print(k) +end +]] + +TEST [[ +local function x() + local loc + x() + print(loc) +end +]] + +TEST [[ +---@class +---@class +]] + +TEST [[ +---@class A : +]] + +TEST [[ +---@class +---@class +---@class +---@class +]] + +TEST [[ +---@class A : B +---@class B : C +---@class C : D +---@class D +]] + +TEST [[ +---@type +]] + +TEST [[ +---@class A +---@type A|| +]] + +TEST [[ +---@class AAA +---@alias B AAA + +---@type B +]] + +TEST [[ +---@alias B +]] + +TEST [[ +---@class +---@class B +---@alias +]] + +TEST [[ +---@param x +]] + +TEST [[ +---@class Class +---@param Class +local function f(x) + return x +end +f() +]] + +TEST [[ +---@class Class +---@param Class +function F(x) + return x +end +F() +]] + +TEST [[ +---@class Class +---@param Class +---@param y Class +---@param Class +local function f(x, y) + return x, y +end +f() +]] + +TEST [[ +---@field +---@class Class +]] + +TEST [[ +---@class Class +---@field Class +---@field Class +]] + +TEST [[ +---@class Class : any +]] + +TEST [[ +---@type fun(a: integer) +local f +f() +]] + +TEST [[ +local x +x = +]] + +TEST [[ +local x, y +x = +]] + +TEST [[ +local x, y, z +x = x and y or '' .. z +]] + +TEST [[ +local x +x = x or -1 +]] + +TEST [[ +local x +x = x or (0 + 1) +]] + +TEST [[ +local x, y +x = (x + y) or 0 +]] + +--TEST [[ +--local t = {} +--function t:() +--end +--function t:() +--end +--]] + +TEST [[ +local t = {} +t.a = 1 +t.a = 2 +]] diff --git a/script/test/diagnostics/syntax.lua b/script/test/diagnostics/syntax.lua new file mode 100644 index 00000000..887d05f0 --- /dev/null +++ b/script/test/diagnostics/syntax.lua @@ -0,0 +1,8 @@ +local config = require 'config' + +TEST [[ +local x = 1 + = 2 +return x +]] + -- cgit v1.2.3