diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-08-10 17:44:22 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-08-11 17:57:29 +0800 |
commit | 5a0804e9869382b8d2f1fa1fffd362dc2fea9c52 (patch) | |
tree | a14dbccabb8b82c2cd313a12a80182b37e05a9b5 /test | |
parent | dab3ea38d3b44ad1d144889698b4311a40e7e9dc (diff) | |
download | lua-language-server-5a0804e9869382b8d2f1fa1fffd362dc2fea9c52.zip |
clean up tests
Diffstat (limited to 'test')
-rw-r--r-- | test/diagnostics/common.lua | 296 | ||||
-rw-r--r-- | test/diagnostics/global-element.lua | 34 | ||||
-rw-r--r-- | test/diagnostics/global-in-nil-env.lua | 38 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 34 | ||||
-rw-r--r-- | test/diagnostics/newfield-call.lua | 15 | ||||
-rw-r--r-- | test/diagnostics/newline-call.lua | 34 | ||||
-rw-r--r-- | test/diagnostics/redefined-local.lua | 22 | ||||
-rw-r--r-- | test/diagnostics/redundant-parameter.lua | 43 | ||||
-rw-r--r-- | test/diagnostics/trailing-space.lua | 26 | ||||
-rw-r--r-- | test/diagnostics/type-check.lua | 15 | ||||
-rw-r--r-- | test/diagnostics/undefined-env-child.lua | 36 | ||||
-rw-r--r-- | test/diagnostics/undefined-global.lua | 11 | ||||
-rw-r--r-- | test/diagnostics/unused-function.lua | 21 | ||||
-rw-r--r-- | test/diagnostics/unused-label.lua | 3 | ||||
-rw-r--r-- | test/diagnostics/unused-local.lua | 25 |
15 files changed, 302 insertions, 351 deletions
diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index 28a3a32a..e0a3e342 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -1,298 +1,6 @@ local config = require 'config' local util = require 'utility' -local disables = config.get(nil, 'Lua.diagnostics.disable') - -TEST [[ -local <!x!> -]] - -TEST [[ -local y -local x <close> = y -]] - -TEST [[ -local function x() -end -x() -]] - -TEST [[ -return function (x) - x.a = 1 -end -]] - -TEST [[ -local <!t!> = {} -<!t!>.a = 1 -]] - -TEST [[ -local <!function <!x!>() -end!> -]] - - -TEST [[ -local <!x!> = <!function () end!> -]] - -TEST [[ -local <!x!> -<!x!> = <!function () end!> -]] - -TEST [[ -local <!function x() -end!> -local <!function <!y!>() - x() -end!> -]] - -TEST [[ -local print, _G -print(<!x!>) -print(<!log!>) -print(<!X!>) -print(<!Log!>) -print(<!y!>) -print(Z) -print(_G) -Z = 1 -]] - -TEST [[ -::<!LABEL!>:: -]] - -TEST [[ -<! !> -]] - -TEST [[ - -<! !> -]] - -TEST [[ -X = 1<! !> -]] - -TEST [[ -X = [=[ - ]=] -]] - -TEST [[ --- xxxx -]] - -TEST [[ --- [=[ - ]=] -]] - -TEST [[ -local x -print(x) -local <!x!> -print(x) -]] - -TEST [[ -local x -print(x) -local <!x!> -print(x) -local <!x!> -print(x) -]] - -TEST [[ -local _ -print(_) -local _ -print(_) -local _ENV -<!print!>(_ENV) -- 由于重定义了_ENV,因此print变为了未定义全局变量 -]] - -TEST [[ -local x -return x, function (<!x!>) - return x -end -]] - -TEST [[ -print(1) -_ENV = nil -]] - -TEST [[ ----@diagnostic disable: undefined-global -_ENV = nil -<!print!>(<!A!>) -- `print` and `A` should warning -]] - -TEST [[ ----@diagnostic disable: undefined-global -local _ENV = nil -<!print!>(<!A!>) -- `print` and `A` should warning -]] - -TEST [[ -_ENV = {} -print(A) -- no warning -]] - -TEST [[ -local _ENV = {} -print(A) -- no warning -]] - -TEST [[ ----@type iolib -_ENV = io -<!print!>(stderr) -- `print` is warning but `stderr` is not -]] - -TEST [[ ----@type iolib -local _ENV = io -<!print!>(stderr) -- `print` is warning but `stderr` is not -]] - -TEST [[ -local _ENV = { print = print } -print(1) -]] - -util.arrayInsert(disables, 'undefined-env-child') -TEST [[ -_ENV = nil -<!GLOBAL!> = 1 --> _ENV.GLOBAL = 1 -]] - -TEST [[ -_ENV = nil -local _ = <!print!> --> local _ = _ENV.print -]] - -TEST [[ -_ENV = {} -GLOBAL = 1 --> _ENV.GLOBAL = 1 -]] - -TEST [[ -_ENV = {} -local _ = print --> local _ = _ENV.print -]] - -TEST [[ -GLOBAL = 1 -_ENV = nil -]] - -util.arrayRemove(disables, 'undefined-env-child') -TEST [[ -<!print() -('string')!>:sub(1, 1) -]] - -TEST [[ -print() -('string') -]] - -TEST [[ -print -{} -{} -]] - -TEST [[ -local x -return x - : f(1) - : f(1) -]] - -TEST [[ -return { - <!print - 'string'!> -} -]] - -TEST [[ -return { - <!print - { - x = 1, - }!> -} -]] - -TEST [[ -print() -'string' -]] - -TEST [[ -print -{ - x = 1, -} -]] - -TEST [[ -local function x(a, b) - return a, b -end -x(1, 2, <!3!>) -]] - -TEST [[ -local function x(a, b, ...) - return a, b, ... -end -x(1, 2, 3, 4, 5) -]] - -TEST [[ ----@type fun(a, b, ...) -local x -x(1, 2, 3, 4, 5) -]] - -TEST [[ -local m = {} -function m:x(a, b) - return a, b -end -m:x(1, 2, <!3!>) -]] - -TEST [[ -local m = {} -function m:x(a, b) - return a, b -end -m.x(m, 2, 3, <!4!>) -]] - -TEST [[ -local m = {} -function m.x(a, b) - return a, b -end -m:x(1, <!2!>, <!3!>, <!4!>) -]] - TEST [[ local function x(a, b) return a, b @@ -329,8 +37,6 @@ x(1, 2) ]] TEST [[ ----@diagnostic disable: unused-local - ---@param a integer ---@param b integer local function f(a, b) @@ -340,8 +46,6 @@ f(...) ]] TEST [[ ----@diagnostic disable: unused-local - ---@param a integer ---@param b integer local function f(a, b) diff --git a/test/diagnostics/global-element.lua b/test/diagnostics/global-element.lua index 0e4cdd61..0c31bade 100644 --- a/test/diagnostics/global-element.lua +++ b/test/diagnostics/global-element.lua @@ -1,31 +1,5 @@ local config = require 'config' -local util = require 'utility' --- disable all default groups to make isolated tests -config.set(nil, 'Lua.diagnostics.groupFileStatus', -{ - ['ambiguity'] = 'None', - ['await'] = 'None', - ['codestyle'] = 'None', - ['conventions'] = 'None', - ['duplicate'] = 'None', - ['global'] = 'None', - ['luadoc'] = 'None', - ['redefined'] = 'None', - ['strict'] = 'None', - ['strong'] = 'None', - ['type-check'] = 'None', - ['unbalanced'] = 'None', - ['unused'] = 'None' -}) - --- enable single diagnostic that is to be tested -config.set(nil, 'Lua.diagnostics.neededFileStatus', -{ - ['global-element'] = 'Any!' -- override groupFileStatus -}) - --- check that local elements are not warned about TEST [[ local x = 123 x = 321 @@ -87,11 +61,3 @@ function GLOBAL_CLOSURE() <!elem2!> = 2 end ]] - --- reset configurations -config.set(nil, 'Lua.diagnostics.groupFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.neededFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.globals', -{}) diff --git a/test/diagnostics/global-in-nil-env.lua b/test/diagnostics/global-in-nil-env.lua new file mode 100644 index 00000000..1dc97bd1 --- /dev/null +++ b/test/diagnostics/global-in-nil-env.lua @@ -0,0 +1,38 @@ +TEST [[ +local _ +print(_) +local _ +print(_) +local _ENV +<!print!>(_ENV) -- 由于重定义了_ENV,因此print变为了未定义全局变量 +]] + +TEST [[ +_ENV = nil +<!print!>(<!A!>) -- `print` and `A` should warning +]] + +TEST [[ +local _ENV = nil +<!print!>(<!A!>) -- `print` and `A` should warning +]] + +TEST [[ +_ENV = {} +print(A) -- no warning +]] + +TEST [[ +local _ENV = {} +print(A) -- no warning +]] + +TEST [[ +_ENV = nil +<!GLOBAL!> = 1 --> _ENV.GLOBAL = 1 +]] + +TEST [[ +_ENV = nil +local _ = <!print!> --> local _ = _ENV.print +]] diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index c2fe720d..96720342 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -4,9 +4,11 @@ local config = require 'config' local util = require 'utility' local catch = require 'catch' -config.get(nil, 'Lua.diagnostics.neededFileStatus')['deprecated'] = 'Any' -config.get(nil, 'Lua.diagnostics.neededFileStatus')['type-check'] = 'Any' -config.get(nil, 'Lua.diagnostics.neededFileStatus')['await-in-sync'] = 'Any' +local status = config.get(nil, 'Lua.diagnostics.neededFileStatus') + +for key in pairs(status) do + status[key] = 'Any!' +end rawset(_G, 'TEST', true) @@ -34,7 +36,10 @@ function TEST(script) local origins = {} local results = {} core(TESTURI, false, function (result) - results[#results+1] = { result.start, result.finish } + if DIAG_CARE == result.code + or DIAG_CARE == '*' then + results[#results+1] = { result.start, result.finish } + end origins[#origins+1] = result end) @@ -84,9 +89,26 @@ function TESTWITH(code) end end -require 'diagnostics.common' +local function check(name) + DIAG_CARE = name + require('diagnostics.' .. name) +end + +check 'unused-local' +check 'unused-function' +check 'undefined-global' +check 'unused-label' +check 'trailing-space' +check 'redefined-local' +check 'global-in-nil-env' +check 'undefined-env-child' +check 'newline-call' +check 'newfield-call' +check 'redundant-parameter' require 'diagnostics.type-check' require 'diagnostics.incomplete-signature-doc' require 'diagnostics.missing-global-doc' require 'diagnostics.missing-local-export-doc' -require 'diagnostics.global-element' +check 'global-element' + +require 'diagnostics.common' diff --git a/test/diagnostics/newfield-call.lua b/test/diagnostics/newfield-call.lua new file mode 100644 index 00000000..63de9db9 --- /dev/null +++ b/test/diagnostics/newfield-call.lua @@ -0,0 +1,15 @@ +TEST [[ +return { + <!print + 'string'!> +} +]] + +TEST [[ +return { + <!print + { + x = 1, + }!> +} +]] diff --git a/test/diagnostics/newline-call.lua b/test/diagnostics/newline-call.lua new file mode 100644 index 00000000..ca160aa3 --- /dev/null +++ b/test/diagnostics/newline-call.lua @@ -0,0 +1,34 @@ +TEST [[ +<!print() +('string')!>:sub(1, 1) +]] + +TEST [[ +print() +('string') +]] + +TEST [[ +print +{} +{} +]] + +TEST [[ +local x +return x + : f(1) + : f(1) +]] + +TEST [[ +print() +'string' +]] + +TEST [[ +print +{ + x = 1, +} +]] diff --git a/test/diagnostics/redefined-local.lua b/test/diagnostics/redefined-local.lua new file mode 100644 index 00000000..c594ed2c --- /dev/null +++ b/test/diagnostics/redefined-local.lua @@ -0,0 +1,22 @@ +TEST [[ +local x +print(x) +local <!x!> +print(x) +]] + +TEST [[ +local x +print(x) +local <!x!> +print(x) +local <!x!> +print(x) +]] + +TEST [[ +local x +return x, function (<!x!>) + return x +end +]] diff --git a/test/diagnostics/redundant-parameter.lua b/test/diagnostics/redundant-parameter.lua new file mode 100644 index 00000000..b63d0987 --- /dev/null +++ b/test/diagnostics/redundant-parameter.lua @@ -0,0 +1,43 @@ +TEST [[ +local function x(a, b) + return a, b +end +x(1, 2, <!3!>) +]] + +TEST [[ +local function x(a, b, ...) + return a, b, ... +end +x(1, 2, 3, 4, 5) +]] + +TEST [[ +---@type fun(a, b, ...) +local x +x(1, 2, 3, 4, 5) +]] + +TEST [[ +local m = {} +function m:x(a, b) + return a, b +end +m:x(1, 2, <!3!>) +]] + +TEST [[ +local m = {} +function m:x(a, b) + return a, b +end +m.x(m, 2, 3, <!4!>) +]] + +TEST [[ +local m = {} +function m.x(a, b) + return a, b +end +m:x(1, <!2!>, <!3!>, <!4!>) +]] diff --git a/test/diagnostics/trailing-space.lua b/test/diagnostics/trailing-space.lua new file mode 100644 index 00000000..be1458be --- /dev/null +++ b/test/diagnostics/trailing-space.lua @@ -0,0 +1,26 @@ +TEST [[ +<! !> +]] + +TEST [[ + +<! !> +]] + +TEST [[ +X = 1<! !> +]] + +TEST [[ +X = [=[ + ]=] +]] + +TEST [[ +-- xxxx +]] + +TEST [[ +-- [=[ + ]=] +]] diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua index adfef561..8ef05bac 100644 --- a/test/diagnostics/type-check.lua +++ b/test/diagnostics/type-check.lua @@ -1,11 +1,5 @@ local config = require 'config' -config.add(nil, 'Lua.diagnostics.disable', 'unused-local') -config.add(nil, 'Lua.diagnostics.disable', 'unused-function') -config.add(nil, 'Lua.diagnostics.disable', 'undefined-global') -config.add(nil, 'Lua.diagnostics.disable', 'redundant-return') -config.set(nil, 'Lua.type.castNumberToInteger', false) - TEST [[ local x = 0 @@ -174,7 +168,6 @@ m.ints = {} ]] TEST [[ ----@diagnostic disable: missing-fields ---@class A ---@field x A @@ -329,7 +322,6 @@ local x ]] TEST [[ ----@diagnostic disable:undefined-global ---@type integer local x @@ -337,7 +329,6 @@ x = 1 + G ]] TEST [[ ----@diagnostic disable:undefined-global ---@type integer local x @@ -1263,9 +1254,3 @@ local MyClass = {} local w = MyClass(<!1!>) ]] - -config.remove(nil, 'Lua.diagnostics.disable', 'unused-local') -config.remove(nil, 'Lua.diagnostics.disable', 'unused-function') -config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global') -config.remove(nil, 'Lua.diagnostics.disable', 'redundant-return') -config.set(nil, 'Lua.type.castNumberToInteger', true) diff --git a/test/diagnostics/undefined-env-child.lua b/test/diagnostics/undefined-env-child.lua new file mode 100644 index 00000000..73904a74 --- /dev/null +++ b/test/diagnostics/undefined-env-child.lua @@ -0,0 +1,36 @@ +TEST [[ +---@type iolib +_ENV = io +<!print!>(stderr) -- `print` is warning but `stderr` is not +]] + +TEST [[ +---@type iolib +local _ENV = io +<!print!>(stderr) -- `print` is warning but `stderr` is not +]] + +TEST [[ +local _ENV = { print = print } +print(1) +]] + +TEST [[ +_ENV = {} +GLOBAL = 1 --> _ENV.GLOBAL = 1 +]] + +TEST [[ +_ENV = {} +local _ = print --> local _ = _ENV.print +]] + +TEST [[ +GLOBAL = 1 +_ENV = nil +]] + +TEST [[ +print(1) +_ENV = nil +]] diff --git a/test/diagnostics/undefined-global.lua b/test/diagnostics/undefined-global.lua new file mode 100644 index 00000000..f3585d03 --- /dev/null +++ b/test/diagnostics/undefined-global.lua @@ -0,0 +1,11 @@ +TEST [[ +local print, _G +print(<!x!>) +print(<!log!>) +print(<!X!>) +print(<!Log!>) +print(<!y!>) +print(Z) +print(_G) +Z = 1 +]] diff --git a/test/diagnostics/unused-function.lua b/test/diagnostics/unused-function.lua new file mode 100644 index 00000000..8f76bcdb --- /dev/null +++ b/test/diagnostics/unused-function.lua @@ -0,0 +1,21 @@ +TEST [[ +local <!function x() +end!> +]] + +TEST [[ +local x = <!function () end!> +]] + +TEST [[ +local x +x = <!function () end!> +]] + +TEST [[ +local <!function x() +end!> +local <!function y() + x() +end!> +]] diff --git a/test/diagnostics/unused-label.lua b/test/diagnostics/unused-label.lua new file mode 100644 index 00000000..3a89a147 --- /dev/null +++ b/test/diagnostics/unused-label.lua @@ -0,0 +1,3 @@ +TEST [[ +::<!LABEL!>:: +]] diff --git a/test/diagnostics/unused-local.lua b/test/diagnostics/unused-local.lua new file mode 100644 index 00000000..4e8294bf --- /dev/null +++ b/test/diagnostics/unused-local.lua @@ -0,0 +1,25 @@ +TEST [[ +local <!x!> +]] + +TEST [[ +local y +local x <close> = y +]] + +TEST [[ +local function x() +end +x() +]] + +TEST [[ +return function (x) + x.a = 1 +end +]] + +TEST [[ +local <!t!> = {} +<!t!>.a = 1 +]] |