Skip to content

Commit 96bcbfd

Browse files
committed
Improve test comparisons code sharing
1 parent 686f96f commit 96bcbfd

File tree

8 files changed

+94
-215
lines changed

8 files changed

+94
-215
lines changed

test/code_action/init.lua

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,21 @@
1-
local core = require 'core.code-action'
2-
local files = require 'files'
3-
local lang = require 'language'
4-
local catch = require 'catch'
5-
local furi = require 'file-uri'
1+
local core = require 'core.code-action'
2+
local files = require 'files'
3+
local lang = require 'language'
4+
local catch = require 'catch'
5+
local furi = require 'file-uri'
6+
local compare = require 'compare'
67

78
rawset(_G, 'TEST', true)
89

9-
local EXISTS = {}
10+
local EXISTS = compare.EXISTS
1011

11-
local function eq(expected, result)
12-
if expected == EXISTS and result ~= nil then
13-
return true
14-
end
15-
if result == EXISTS and expected ~= nil then
16-
return true
17-
end
18-
local tp1, tp2 = type(expected), type(result)
19-
if tp1 ~= tp2 then
20-
return false, string.format(": expected type %s, got %s", tp1, tp2)
21-
end
22-
if tp1 == 'table' then
23-
local mark = {}
24-
for k in pairs(expected) do
25-
local ok, err = eq(expected[k], result[k])
26-
if not ok then
27-
return false, string.format(".%s%s", k, err)
28-
end
29-
mark[k] = true
30-
end
31-
for k in pairs(result) do
32-
if not mark[k] then
33-
return false, string.format(".%s: missing key in result", k)
34-
end
35-
end
36-
return true
37-
end
38-
return expected == result, string.format(": expected %s, got %s", expected, result)
39-
end
40-
41-
function TEST(script)
12+
local function TEST(script)
4213
return function (expect)
4314
local newScript, catched = catch(script, '?')
4415
files.setText(TESTURI, newScript)
4516
local results = core(TESTURI, catched['?'][1][1], catched['?'][1][2])
4617
assert(results)
47-
assert(eq(expect, results))
18+
assert(compare.eq(expect, results))
4819
files.remove(TESTURI)
4920
end
5021
end
@@ -72,7 +43,7 @@ local function TEST_CROSSFILE(testfiles)
7243

7344
local results = core(TESTURI, catched['?'][1][1], catched['?'][1][2])
7445
assert(results)
75-
assert(eq(expected, results))
46+
assert(compare.eq(expected, results))
7647
end
7748
end
7849

test/compare.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
local export = {}
3+
local EXISTS = {}
4+
5+
export.EXISTS = EXISTS
6+
7+
local divider = "\n=================\n"
8+
local header1 = ("%stest failed. got:%s"):format(divider, divider)
9+
local header2 = ("%sexpected:%s"):format(divider, divider)
10+
local template = header1 .. "%s" .. header2 .. "%s" .. divider
11+
12+
function export.eq(a, b)
13+
if a == EXISTS and b ~= nil then
14+
return true
15+
end
16+
if b == EXISTS and a ~= nil then
17+
return true
18+
end
19+
local tp1, tp2 = type(a), type(b)
20+
if tp1 ~= tp2 then
21+
return false, template:format("type is: " .. tp1, "type is: " .. tp2)
22+
end
23+
if tp1 == 'table' then
24+
local mark = {}
25+
for k in pairs(a) do
26+
local ok, err = export.eq(a[k], b[k])
27+
if not ok then
28+
return false, string.format(".%s%s", k, err)
29+
end
30+
mark[k] = true
31+
end
32+
for k in pairs(b) do
33+
if not mark[k] then
34+
return false, string.format(".%s: missing key in result", k)
35+
end
36+
end
37+
return true
38+
end
39+
40+
if a == b then
41+
return true
42+
end
43+
44+
return false, template:format(tostring(a), tostring(b))
45+
end
46+
47+
return export

test/completion/init.lua

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
1-
local core = require 'core.completion'
2-
local files = require 'files'
3-
local catch = require 'catch'
4-
local guide = require 'parser.guide'
1+
local core = require 'core.completion'
2+
local files = require 'files'
3+
local catch = require 'catch'
4+
local guide = require 'parser.guide'
5+
local compare = require 'compare'
56

6-
EXISTS = {'EXISTS'}
7-
8-
local function eq(a, b)
9-
if a == EXISTS and b ~= nil then
10-
return true
11-
end
12-
local tp1, tp2 = type(a), type(b)
13-
if tp1 ~= tp2 then
14-
return false
15-
end
16-
if tp1 == 'table' then
17-
local mark = {}
18-
for k in pairs(a) do
19-
if not eq(a[k], b[k]) then
20-
return false
21-
end
22-
mark[k] = true
23-
end
24-
for k in pairs(b) do
25-
if not mark[k] then
26-
return false
27-
end
28-
end
29-
return true
30-
end
31-
return a == b
32-
end
7+
EXISTS = compare.EXISTS
338

349
local function include(a, b)
3510
if a == EXISTS and b ~= nil then
@@ -46,7 +21,7 @@ local function include(a, b)
4621
for _, v1 in ipairs(a) do
4722
local ok = false
4823
for _, v2 in ipairs(b) do
49-
if eq(v1, v2) then
24+
if compare.eq(v1, v2) then
5025
ok = true
5126
break
5227
end
@@ -57,7 +32,7 @@ local function include(a, b)
5732
end
5833
return true
5934
end
60-
return a == b
35+
return compare.eq(a, b)
6136
end
6237

6338
rawset(_G, 'TEST', true)
@@ -138,7 +113,7 @@ function TEST(script)
138113
expect.include = nil
139114
assert(include(expect, result))
140115
else
141-
assert(eq(expect, result))
116+
assert(compare.eq(expect, result))
142117
end
143118
end
144119
files.remove(TESTURI)

test/crossfile/completion.lua

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,14 @@ local util = require 'utility'
66
local config = require 'config'
77
local catch = require 'catch'
88
local define = require 'proto.define'
9+
local compare = require 'compare'
910

1011
rawset(_G, 'TEST', true)
1112

1213
local CompletionItemKind = define.CompletionItemKind
1314
local NeedRemoveMeta = false
1415

15-
local EXISTS = {}
16-
17-
local function eq(a, b)
18-
if a == EXISTS and b ~= nil then
19-
return true
20-
end
21-
local tp1, tp2 = type(a), type(b)
22-
if tp1 ~= tp2 then
23-
return false
24-
end
25-
if tp1 == 'table' then
26-
local mark = {}
27-
for k in pairs(a) do
28-
if not eq(a[k], b[k]) then
29-
return false
30-
end
31-
mark[k] = true
32-
end
33-
for k in pairs(b) do
34-
if not mark[k] then
35-
return false
36-
end
37-
end
38-
return true
39-
end
40-
return a == b
41-
end
16+
local EXISTS = compare.EXISTS
4217

4318
local Cared = {
4419
['label'] = true,
@@ -57,7 +32,7 @@ local function removeMetas(results)
5732
end
5833

5934
---@diagnostic disable: await-in-sync
60-
function TEST(data)
35+
local function TEST(data)
6136
local mainUri
6237
local pos
6338
for _, info in ipairs(data) do
@@ -115,7 +90,7 @@ function TEST(data)
11590
end
11691
end
11792
assert(result)
118-
assert(eq(expect, result))
93+
assert(compare.eq(expect, result))
11994
end
12095

12196
local function WITH_CONFIG(cfg, f)
@@ -139,7 +114,7 @@ TEST {
139114
---@class A
140115
---@field f1 integer
141116
---@field f2 boolean
142-
117+
143118
---@type A[]
144119
X = {}
145120
]],

test/crossfile/definition.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ TEST {
570570
571571
function mt:<!add!>(a, b)
572572
end
573-
573+
574574
return function ()
575575
return setmetatable({}, mt)
576576
end
@@ -650,10 +650,10 @@ TEST {
650650
function lib:fn1()
651651
return self
652652
end
653-
653+
654654
function lib:<!fn2!>()
655655
end
656-
656+
657657
return lib:fn1()
658658
]]
659659
},

test/crossfile/hover.lua

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,13 @@
1-
local files = require 'files'
2-
local furi = require 'file-uri'
3-
local core = require 'core.hover'
4-
local config = require 'config'
5-
local catch = require 'catch'
1+
local files = require 'files'
2+
local furi = require 'file-uri'
3+
local core = require 'core.hover'
4+
local catch = require 'catch'
5+
local compare = require 'compare'
66

77
rawset(_G, 'TEST', true)
88

9-
local EXISTS = {}
10-
11-
local function eq(a, b)
12-
if a == EXISTS and b ~= nil then
13-
return true
14-
end
15-
if b == EXISTS and a ~= nil then
16-
return true
17-
end
18-
local tp1, tp2 = type(a), type(b)
19-
if tp1 ~= tp2 then
20-
return false
21-
end
22-
if tp1 == 'table' then
23-
local mark = {}
24-
for k in pairs(a) do
25-
if not eq(a[k], b[k]) then
26-
return false
27-
end
28-
mark[k] = true
29-
end
30-
for k in pairs(b) do
31-
if not mark[k] then
32-
return false
33-
end
34-
end
35-
return true
36-
end
37-
return a == b
38-
end
39-
409
---@diagnostic disable: await-in-sync
41-
function TEST(expect)
10+
local function TEST(expect)
4211
local sourcePos, sourceUri
4312
for _, file in ipairs(expect) do
4413
local script, list = catch(file.content, '?')
@@ -60,7 +29,7 @@ function TEST(expect)
6029
local hover = core.byUri(sourceUri, sourcePos, 1)
6130
assert(hover)
6231
local content = tostring(hover):gsub('\r\n', '\n')
63-
assert(eq(content, expect.hover))
32+
assert(compare.eq(content, expect.hover))
6433
end
6534

6635
TEST {

0 commit comments

Comments
 (0)