Skip to content

Commit 0d416e9

Browse files
committed
Output the assertion in a way that the IDE can report.
1 parent 8988203 commit 0d416e9

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

tests/assertions.lua

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ function assertions:__call(...)
1414
return self.call(...)
1515
end
1616

17+
function assertions:cleanseString(s)
18+
s = string.gsub(s, '\n', '\\n')
19+
s = string.gsub(s, '\t', '\\t')
20+
s = string.gsub(s, '\r', '\\r')
21+
return s
22+
end
23+
1724
function assertions:Fail(t, ...)
1825
t:LogHelper(2, ...)
1926
if self.fail_now then
20-
print("Failing now")
27+
assert(t.FailNow, "First parameter must be t (the testing.T object)")
2128
t:FailNow()
2229
else
23-
print("Failing")
30+
assert(t.Fail, "First parameter must be t (the testing.T object)")
2431
t:Fail()
2532
end
2633
return false
@@ -29,8 +36,10 @@ end
2936
function assertions:Failf(t, fmt, ...)
3037
t:LogHelperf(2, fmt, ...)
3138
if self.fail_now then
39+
assert(t.FailNow, "First parameter must be t (the testing.T object)")
3240
t:FailNow()
3341
else
42+
assert(t.Fail, "First parameter must be t (the testing.T object)")
3443
t:Fail()
3544
end
3645
return false
@@ -40,56 +49,89 @@ function assertions:Equal(t, expected, actual, ...)
4049
if expected == actual then
4150
return true
4251
end
43-
return self:Fail(t, string.format([[expected "%s"; got "%s"]] .. '\n', expected, actual), ...)
52+
return self:Fail(t, string.format([[
53+
54+
Error: Not equal:
55+
expected: "%s"
56+
actual : "%s"
57+
Messages: ]], self:cleanseString(expected), self:cleanseString(actual)), ...)
4458
end
4559

4660
function assertions:Equalf(t, expected, actual, fmt, ...)
4761
if expected == actual then
4862
return true
4963
end
50-
return self:Failf(t, string.format([[expected "%s"; got "%s"%s%s]], expected, actual, '\n', fmt), ...)
64+
return self:Failf(t, string.format([[
65+
66+
Error: Not equal:
67+
expected: "%s"
68+
actual : "%s"
69+
Messages: %s
70+
]], self:cleanseString(expected), self:cleanseString(actual), fmt), ...)
5171
end
5272

5373
function assertions:NotEqual(t, expected, actual, ...)
5474
if expected ~= actual then
5575
return true
5676
end
57-
return self:Fail(t, string.format([[expected ~= "%s";]] .. '\n', expected), ...)
77+
return self:Fail(t, string.format([[
78+
79+
Error: Should not be %s
80+
Messages: ]], expected), ...)
5881
end
5982

6083
function assertions:NotEqualf(t, expected, actual, fmt, ...)
6184
if expected ~= actual then
6285
return true
6386
end
64-
return self:Failf(t, string.format([[expected ~= "%s"; got "%s"%s%s]], expected, '\n', fmt), ...)
87+
return self:Failf(t, string.format([[
88+
89+
Error: Should not be %s
90+
Messages: %s
91+
]], expected, fmt), ...)
6592
end
6693

6794
function assertions:True(t, actual, ...)
6895
if actual then
6996
return true
7097
end
71-
return self:Fail(t, string.format([[expected true; got %s]] .. '\n', actual), ...)
98+
return self:Fail(t, string.format([[
99+
100+
Error: Should be true
101+
Messages: ]]), ...)
72102
end
73103

74104
function assertions:Truef(t, actual, fmt, ...)
75105
if actual then
76106
return true
77107
end
78-
return self:Failf(t, string.format([[expected true; got %s%s%s]], actual, '\n', fmt), ...)
108+
return self:Failf(t, string.format([[
109+
110+
Error: Should be true
111+
Messages: %s
112+
]], fmt), ...)
113+
79114
end
80115

81116
function assertions:False(t, actual, ...)
82117
if not actual then
83118
return true
84119
end
85-
return self:Fail(t, string.format([[expected false; got %s]] .. '\n', actual), ...)
120+
return self:Fail(t, string.format([[
121+
122+
Error Should be false
123+
Messages: ]]), ...)
86124
end
87125

88126
function assertions:Falsef(t, actual, fmt, ...)
89127
if not actual then
90128
return true
91129
end
92-
return self:Failf(t, string.format([[expected false; got %s%s%s]], actual, '\n', fmt), ...)
130+
return self:Failf(t, string.format([[
131+
132+
Error: Should be false
133+
Messages: %s
134+
]], fmt), ...)
93135
end
94136

95137
return assertions

tests/testdata/test_assertions_failing.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ function TestTablesNotEqual(t)
3838
end
3939

4040
function TestFalse(t)
41-
assert:False(true, "oh noes")
41+
assert:False(t, true, "oh noes")
42+
end
43+
44+
function TestStringsEqual(t)
45+
assert:Equal(t, "expected text", "actual text")
4246
end

tests/testutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ func RunLuaTestFile(t *testing.T, preload PreloadFunc, filename string) (numTest
263263

264264
registerTType(L)
265265
PreloadSuite(L)
266-
PreloadAssertions(L)
267-
PreloadAssert(L)
268-
PreloadRequire(L)
266+
//PreloadAssertions(L)
267+
//PreloadAssert(L)
268+
//PreloadRequire(L)
269269
require.NotNil(t, preload)
270270
preload(L)
271271
L.SetGlobal("t", tLua(L, t))

tests/testutil_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ func TestAssertions(t *testing.T) {
3232
assert.NotZero(t, RunLuaTestFile(t, preload, "testdata/test_assertions_failing.lua"))
3333
})
3434
}
35+
36+
func TestFail(t *testing.T) {
37+
assert.Equal(t, "foo", "bar", "yikes", "bob")
38+
assert.True(t, false, "another message")
39+
assert.NotEqual(t, 123, 123)
40+
}

0 commit comments

Comments
 (0)