diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-08-11 16:28:47 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-08-11 17:57:29 +0800 |
commit | 6fe0ea884f692a460cdf084c28900fc44dec0018 (patch) | |
tree | 68fdfcf8bdaf3c18ab06af2a7a32d6eb3fc1c809 /test/diagnostics | |
parent | 736e54a8ecaada2dacd0fd0ae35ad03aa75a7149 (diff) | |
download | lua-language-server-6fe0ea884f692a460cdf084c28900fc44dec0018.zip |
cleanup tests
Diffstat (limited to 'test/diagnostics')
22 files changed, 426 insertions, 553 deletions
diff --git a/test/diagnostics/ambiguity-1.lua b/test/diagnostics/ambiguity-1.lua new file mode 100644 index 00000000..6b8e41da --- /dev/null +++ b/test/diagnostics/ambiguity-1.lua @@ -0,0 +1,29 @@ +TEST [[ +local x +x = <!x or 0 + 1!> +]] + +TEST [[ +local x, y +x = <!x + y or 0!> +]] + +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 +]] diff --git a/test/diagnostics/assign-type-mismatch.lua b/test/diagnostics/assign-type-mismatch.lua index 5aacb168..d4632563 100644 --- a/test/diagnostics/assign-type-mismatch.lua +++ b/test/diagnostics/assign-type-mismatch.lua @@ -465,3 +465,10 @@ local arr2 = { <!3!>, -- no warnings } ]] + +TEST [[ +local t = {} +t.a = 1 +t.a = 2 +return t +]] diff --git a/test/diagnostics/close-non-object.lua b/test/diagnostics/close-non-object.lua new file mode 100644 index 00000000..10f207f0 --- /dev/null +++ b/test/diagnostics/close-non-object.lua @@ -0,0 +1,11 @@ +TEST [[ +local _ <close> = <!1!> +]] + +TEST [[ +local _ <close> = <!''!> +]] + +TEST [[ +local c <close> = <!(function () return 1 end)()!> +]] diff --git a/test/diagnostics/code-after-break.lua b/test/diagnostics/code-after-break.lua new file mode 100644 index 00000000..a150948b --- /dev/null +++ b/test/diagnostics/code-after-break.lua @@ -0,0 +1,7 @@ +TEST [[ +while true do + break + <!print() + print()!> +end +]] diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index e0a3e342..24ec4c8c 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -1,455 +1,12 @@ local config = require 'config' local util = require 'utility' -TEST [[ -local function x(a, b) - return a, b -end -x(1) -]] - -TEST [[ ----@param a integer ----@param b integer -local function x(a, b) - return a, b -end -<!x(1)!> -]] - -TEST [[ ----@param a integer ----@param b integer -local function x(a, b) - return a, b -end -<!x()!> -]] - -TEST [[ ----@param a integer ----@param b integer ----@param ... integer -local function x(a, b, ...) - return a, b, ... -end -x(1, 2) -]] - -TEST [[ ----@param a integer ----@param b integer -local function f(a, b) -end - -f(...) -]] - -TEST [[ ----@param a integer ----@param b integer -local function f(a, b) -end - -local function return2Numbers() - return 1, 2 -end - -f(return2Numbers()) -]] - -TEST [[ ----@param a integer ----@param b? integer -local function x(a, b) - return a, b -end -x(1) -]] - -TEST [[ ----@param b integer? -local function x(a, b) - return a, b -end -x(1) -]] - -TEST [[ ----@param b integer|nil -local function x(a, b) - return a, b -end -x(1) -]] - -TEST [[ -local m = {} -function m.x() -end -m:x() -]] - -TEST [[ -InstanceName = 1 -Instance = _G[InstanceName] -]] - -TEST [[ -local _ = (''):sub(1, 2) -]] - -TEST [=[ -return [[ - -]] -]=] - -util.arrayInsert(disables, 'close-non-object') -TEST [[ -local _ <close> = function () end -]] -util.arrayRemove(disables, 'close-non-object') - -TEST [[ -local _ <close> = <!1!> -]] - -TEST [[ -local _ <close> = <!''!> -]] - -TEST [[ -local c <close> = <!(function () return 1 end)()!> -]] - -util.arrayInsert(disables, 'unused-local') -TEST [[ -local f = <!function () end!> -]] - -TEST [[ -local f;f = <!function () end!> -]] - -TEST [[ -local <!function f() end!> -]] - -TEST [[ -local <!function f() - f() -end!> -]] - - -TEST [[ -local <!function test() -end!> - -local <!function foo () -end!> -]] - -util.arrayRemove(disables, 'unused-local') -TEST [[ -local mt, x -function mt:m() - function x:m() - end -end -return mt, x -]] - -TEST [[ -local mt = {} -function mt:f() -end -return mt -]] - -TEST [[ -local <!mt!> = {} -function <!mt!>:f() -end -]] - -TEST [[ -local <!x!> = {} -<!x!>.a = 1 -]] - -TEST [[ -local <!x!> = {} -<!x!>['a'] = 1 -]] - -TEST [[ -local function f(<!self!>) - return 'something' -end -f() -]] - -TEST [[ -local function f(<!...!>) - return 'something' -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, <!3!>, <!4!>) -]] - -TEST [[ -local mt = {} -function mt:f(a, b) - return a, b -end -mt.f(mt, 2, 3, <!4!>) -]] - - -TEST [[ -local mt = {} -function mt.f(a, b) - return a, b -end -mt:f(1, <!2!>, <!3!>, <!4!>) -]] - -TEST [[ -local mt = {} -function mt:f(a, b) - return a, b -end -mt:f(1, 2, <!3!>, <!4!>) -]] - -TEST [[ -local function f(a, b, ...) - return a, b, ... -end -f(1, 2, 3, 4) -]] - -TEST [[ -local _ = next({}, 1, <!2!>) -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(<!1!>) ---tostring = realTostring ---tostring(1) ---]] - -TEST [[ -<!aa!> = 1 -tostring = 1 -ROOT = 1 -_G.bb = 1 -]] - -TEST [[ -local f = load('') -if f then - f(1, 2, 3) -end -]] - -TEST [[ -local _ = <!unpack!> -]] - -TEST [[ -X = table[<!x!>] -]] - -TEST [[ -return { - <!x!> = 1, - y = 2, - <!x!> = 3, -} -]] -TEST [[ -return { - x = 1, - y = 2, -}, { - x = 1, - y = 2, -} -]] - -TEST [[ -local m = {} -function m.open() -end - -m:open() -]] - -TEST [[ -local m = {} -function m:open() -end -m.open(m) -]] -TEST [[ -<!if true then -end!> -]] -TEST [[ -<!if true then -else -end!> -]] -TEST [[ -if true then -else - return -end -]] -TEST [[ -while true do -end -]] - -TEST [[ -<!for _ = 1, 10 do -end!> -]] - -TEST [[ -<!for _ in pairs({}) do -end!> -]] - -TEST [[ -local _ = 1, <!2!> -]] - -TEST [[ -_ = 1, <!2!> -]] - -TEST [[ -function X() - do - local k - print(k) - end - local k = 1 - print(k) -end -]] - -TEST [[ -function X() - local loc - print(loc) -end -]] - -TEST [[ -local <!t!> = {} -<!t!>[1] = 1 -]] - -TEST [[ -T1 = 1 -_ENV.T2 = 1 -_G.T3 = 1 -_ENV._G.T4 = 1 -_G._G._G.T5 = 1 -rawset(_G, 'T6', 1) -rawset(_ENV, 'T7', 1) -print(T1) -print(T2) -print(T3) -print(T4) -print(T5) -print(T6) -print(T7) -]] - -TEST [[ -local x -x = <!x or 0 + 1!> -]] - -TEST [[ -local x, y -x = <!x + y or 0!> -]] - -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 = {} -t.a = 1 -t.a = 2 -return t -]] - -TEST [[ -table.insert({}, 1, 2, <!3!>) -]] - -TEST [[ -while true do - break - <!print() - print()!> -end -]] TEST [[ local x, <!y!>, <!z!> = 1 diff --git a/test/diagnostics/deprecated.lua b/test/diagnostics/deprecated.lua new file mode 100644 index 00000000..1086ecb2 --- /dev/null +++ b/test/diagnostics/deprecated.lua @@ -0,0 +1,3 @@ +TEST [[ +local _ = <!unpack!> +]] diff --git a/test/diagnostics/duplicate-doc-field.lua b/test/diagnostics/duplicate-doc-field.lua new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/diagnostics/duplicate-doc-field.lua diff --git a/test/diagnostics/duplicate-index.lua b/test/diagnostics/duplicate-index.lua new file mode 100644 index 00000000..feb5381d --- /dev/null +++ b/test/diagnostics/duplicate-index.lua @@ -0,0 +1,17 @@ +TEST [[ +return { + <!x!> = 1, + y = 2, + <!x!> = 3, +} +]] + +TEST [[ +return { + x = 1, + y = 2, +}, { + x = 1, + y = 2, +} +]] diff --git a/test/diagnostics/empty-block.lua b/test/diagnostics/empty-block.lua new file mode 100644 index 00000000..750397a4 --- /dev/null +++ b/test/diagnostics/empty-block.lua @@ -0,0 +1,32 @@ +TEST [[ +<!if true then +end!> +]] + +TEST [[ +<!if true then +else +end!> +]] + +TEST [[ +if true then +else + return +end +]] + +TEST [[ +while true do +end +]] + +TEST [[ +<!for _ = 1, 10 do +end!> +]] + +TEST [[ +<!for _ in pairs({}) do +end!> +]] diff --git a/test/diagnostics/incomplete-signature-doc.lua b/test/diagnostics/incomplete-signature-doc.lua index 6c529a62..7cd144c0 100644 --- a/test/diagnostics/incomplete-signature-doc.lua +++ b/test/diagnostics/incomplete-signature-doc.lua @@ -1,30 +1,3 @@ -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', -{ - ['incomplete-signature-doc'] = 'Any!' -- override groupFileStatus -}) - -- ------------------------------------- -- about the structure of these test cases -- @@ -42,6 +15,7 @@ config.set(nil, 'Lua.diagnostics.neededFileStatus', -- global functions no parameter, no return value -- no incomplete signature docs possible + TEST [[ function FG0() end @@ -422,11 +396,3 @@ end local vpr4 = FLPR4(0) ]] - --- reset configurations -config.set(nil, 'Lua.diagnostics.groupFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.neededFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.globals', -{})
\ No newline at end of file diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index 302bb0a9..3fb90f39 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -88,9 +88,18 @@ check 'return-type-mismatch' check 'missing-return' check 'missing-return-value' check 'redundant-return-value' -require 'diagnostics.incomplete-signature-doc' -require 'diagnostics.missing-global-doc' -require 'diagnostics.missing-local-export-doc' +check 'incomplete-signature-doc' +check 'missing-global-doc' +check 'missing-local-export-doc' check 'global-element' +check 'missing-parameter' +check 'close-non-object' +check 'duplicate-doc-field' +check 'lowercase-global' +check 'deprecated' +check 'duplicate-index' +check 'empty-block' +check 'redundant-value' +check 'code-after-block' require 'diagnostics.common' diff --git a/test/diagnostics/lowercase-global.lua b/test/diagnostics/lowercase-global.lua new file mode 100644 index 00000000..5e1d70a9 --- /dev/null +++ b/test/diagnostics/lowercase-global.lua @@ -0,0 +1,6 @@ +TEST [[ +<!aa!> = 1 +tostring = 1 +ROOT = 1 +_G.bb = 1 +]] diff --git a/test/diagnostics/missing-global-doc.lua b/test/diagnostics/missing-global-doc.lua index 2ebc237a..de5250fd 100644 --- a/test/diagnostics/missing-global-doc.lua +++ b/test/diagnostics/missing-global-doc.lua @@ -1,31 +1,3 @@ -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', -{ - ['missing-global-doc'] = 'Any!' -- override groupFileStatus -}) - - -- check global functions TEST [[ <!function FG0() @@ -323,11 +295,3 @@ end local vpr4 = FLPR4(0) ]] - --- reset configurations -config.set(nil, 'Lua.diagnostics.groupFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.neededFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.globals', -{})
\ No newline at end of file diff --git a/test/diagnostics/missing-local-export-doc.lua b/test/diagnostics/missing-local-export-doc.lua index bdbf1cfb..3d942216 100644 --- a/test/diagnostics/missing-local-export-doc.lua +++ b/test/diagnostics/missing-local-export-doc.lua @@ -1,31 +1,3 @@ -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', -{ - ['missing-local-export-doc'] = 'Any!' -- override groupFileStatus -}) - - -- check global functions TEST [[ local mod = {} @@ -201,11 +173,3 @@ mod.flpr3 = flpr3 mod.flpr4 = flpr4 return mod ]] - --- reset configurations -config.set(nil, 'Lua.diagnostics.groupFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.neededFileStatus', -{}) -config.set(nil, 'Lua.diagnostics.globals', -{})
\ No newline at end of file diff --git a/test/diagnostics/missing-parameter.lua b/test/diagnostics/missing-parameter.lua new file mode 100644 index 00000000..91e9b88d --- /dev/null +++ b/test/diagnostics/missing-parameter.lua @@ -0,0 +1,82 @@ + +TEST [[ +local function x(a, b) + return a, b +end +x(1) +]] + +TEST [[ +---@param a integer +---@param b integer +local function x(a, b) + return a, b +end +<!x(1)!> +]] + +TEST [[ +---@param a integer +---@param b integer +local function x(a, b) + return a, b +end +<!x()!> +]] + +TEST [[ +---@param a integer +---@param b integer +---@param ... integer +local function x(a, b, ...) + return a, b, ... +end +x(1, 2) +]] + +TEST [[ +---@param a integer +---@param b integer +local function f(a, b) +end + +f(...) +]] + +TEST [[ +---@param a integer +---@param b integer +local function f(a, b) +end + +local function return2Numbers() + return 1, 2 +end + +f(return2Numbers()) +]] + +TEST [[ +---@param a integer +---@param b? integer +local function x(a, b) + return a, b +end +x(1) +]] + +TEST [[ +---@param b integer? +local function x(a, b) + return a, b +end +x(1) +]] + +TEST [[ +---@param b integer|nil +local function x(a, b) + return a, b +end +x(1) +]] diff --git a/test/diagnostics/redundant-parameter.lua b/test/diagnostics/redundant-parameter.lua index b63d0987..cd9d7a43 100644 --- a/test/diagnostics/redundant-parameter.lua +++ b/test/diagnostics/redundant-parameter.lua @@ -41,3 +41,95 @@ function m.x(a, b) end m:x(1, <!2!>, <!3!>, <!4!>) ]] + +TEST [[ +local m = {} +function m.x() +end +m:x() +]] + +TEST [[ +local function f(a, b) + return a, b +end +f(1, 2, <!3!>, <!4!>) +]] + +TEST [[ +local mt = {} +function mt:f(a, b) + return a, b +end +mt.f(mt, 2, 3, <!4!>) +]] + +TEST [[ +local mt = {} +function mt.f(a, b) + return a, b +end +mt:f(1, <!2!>, <!3!>, <!4!>) +]] + +TEST [[ +local mt = {} +function mt:f(a, b) + return a, b +end +mt:f(1, 2, <!3!>, <!4!>) +]] + +TEST [[ +local function f(a, b, ...) + return a, b, ... +end +f(1, 2, 3, 4) +]] + +TEST [[ +local _ = next({}, 1, <!2!>) +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(<!1!>) +--tostring = realTostring +--tostring(1) +--]] + +TEST [[ +local f = load('') +if f then + f(1, 2, 3) +end +]] + +TEST [[ +local m = {} +function m.open() +end + +m:open() +]] + +TEST [[ +local m = {} +function m:open() +end + +m.open(m) +]] + +TEST [[ +table.insert({}, 1, 2, <!3!>) +]] diff --git a/test/diagnostics/redundant-value.lua b/test/diagnostics/redundant-value.lua new file mode 100644 index 00000000..a63544df --- /dev/null +++ b/test/diagnostics/redundant-value.lua @@ -0,0 +1,7 @@ +TEST [[ +local _ = 1, <!2!> +]] + +TEST [[ +_ = 1, <!2!> +]] diff --git a/test/diagnostics/trailing-space.lua b/test/diagnostics/trailing-space.lua index be1458be..ff794714 100644 --- a/test/diagnostics/trailing-space.lua +++ b/test/diagnostics/trailing-space.lua @@ -24,3 +24,9 @@ TEST [[ -- [=[ ]=] ]] + +TEST [=[ +return [[ + +]] +]=] diff --git a/test/diagnostics/undefined-global.lua b/test/diagnostics/undefined-global.lua index f3585d03..55ef5a00 100644 --- a/test/diagnostics/undefined-global.lua +++ b/test/diagnostics/undefined-global.lua @@ -9,3 +9,23 @@ print(Z) print(_G) Z = 1 ]] + +TEST [[ +X = table[<!x!>] +]] +TEST [[ +T1 = 1 +_ENV.T2 = 1 +_G.T3 = 1 +_ENV._G.T4 = 1 +_G._G._G.T5 = 1 +rawset(_G, 'T6', 1) +rawset(_ENV, 'T7', 1) +print(T1) +print(T2) +print(T3) +print(T4) +print(T5) +print(T6) +print(T7) +]] diff --git a/test/diagnostics/unused-function.lua b/test/diagnostics/unused-function.lua index 8f76bcdb..c2cea23a 100644 --- a/test/diagnostics/unused-function.lua +++ b/test/diagnostics/unused-function.lua @@ -19,3 +19,30 @@ local <!function y() x() end!> ]] + +TEST [[ +local f = <!function () end!> +]] + +TEST [[ +local f;f = <!function () end!> +]] + +TEST [[ +local <!function f() end!> +]] + +TEST [[ +local <!function f() + f() +end!> +]] + + +TEST [[ +local <!function test() +end!> + +local <!function foo () +end!> +]] diff --git a/test/diagnostics/unused-local.lua b/test/diagnostics/unused-local.lua index 4e8294bf..1faace25 100644 --- a/test/diagnostics/unused-local.lua +++ b/test/diagnostics/unused-local.lua @@ -23,3 +23,64 @@ TEST [[ local <!t!> = {} <!t!>.a = 1 ]] + +TEST [[ +InstanceName = 1 +Instance = _G[InstanceName] +]] + +TEST [[ +local _ = (''):sub(1, 2) +]] + +TEST [[ +local mt, x +function mt:m() + function x:m() + end +end +return mt, x +]] + +TEST [[ +local mt = {} +function mt:f() +end +return mt +]] + +TEST [[ +local <!mt!> = {} +function <!mt!>:f() +end +]] + +TEST [[ +local <!x!> = {} +<!x!>.a = 1 +]] + +TEST [[ +local <!x!> = {} +<!x!>['a'] = 1 +]] + +TEST [[ +local function f(<!self!>) + return 'something' +end +f() +]] + +TEST [[ +local function f(var) + print(var) +end +local var +f(var) +]] + +TEST [[ +local <!t!> = {} +<!t!>[1] = 1 +]] diff --git a/test/diagnostics/unused-vararg.lua b/test/diagnostics/unused-vararg.lua new file mode 100644 index 00000000..2bed4aab --- /dev/null +++ b/test/diagnostics/unused-vararg.lua @@ -0,0 +1,6 @@ +TEST [[ +local function f(<!...!>) + return 'something' +end +f() +]] |