Skip to content

Commit 6ad7057

Browse files
committed
Add assert and require features like testify has.
1 parent 6afdfc8 commit 6ad7057

File tree

10 files changed

+387
-8
lines changed

10 files changed

+387
-8
lines changed

tests/assert.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local assertions = require 'assertions'
2+
local orig_assert = assert
3+
return assertions:new {
4+
call = orig_assert,
5+
}

tests/assert_const.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// GENERATED BY textFileToGoConst
2+
// GitHub: github.com/logrusorgru/textFileToGoConst
3+
// input file: assert.lua
4+
// generated: Fri Nov 11 12:42:16 PST 2022
5+
6+
package tests
7+
8+
const lua_assert = `local assertions = require 'assertions'
9+
local orig_assert = assert
10+
return assertions:new {
11+
call = orig_assert,
12+
}
13+
`

tests/assertions.lua

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
local assertions = {
2+
fail_now = false,
3+
}
4+
5+
function assertions:new(o)
6+
o = o or {} -- create object if user does not provide one
7+
setmetatable(o, self)
8+
self.__index = self
9+
return o
10+
end
11+
12+
function assertions:__call(...)
13+
assert(self.call, 'attempt to call a non-function object')
14+
return self.call(...)
15+
end
16+
17+
function assertions:Fail(t, ...)
18+
t:LogHelper(2, ...)
19+
if self.fail_now then
20+
print("Failing now")
21+
t:FailNow()
22+
else
23+
print("Failing")
24+
t:Fail()
25+
end
26+
return false
27+
end
28+
29+
function assertions:Failf(t, fmt, ...)
30+
t:LogHelperf(2, fmt, ...)
31+
if self.fail_now then
32+
t:FailNow()
33+
else
34+
t:Fail()
35+
end
36+
return false
37+
end
38+
39+
function assertions:Equal(t, expected, actual, ...)
40+
if expected == actual then
41+
return true
42+
end
43+
return self:Fail(t, string.format([[expected "%s"; got "%s"]] .. '\n', expected, actual), ...)
44+
end
45+
46+
function assertions:Equalf(t, expected, actual, fmt, ...)
47+
if expected == actual then
48+
return true
49+
end
50+
return self:Failf(t, string.format([[expected "%s"; got "%s"%s%s]], expected, actual, '\n', fmt), ...)
51+
end
52+
53+
function assertions:NotEqual(t, expected, actual, ...)
54+
if expected ~= actual then
55+
return true
56+
end
57+
return self:Fail(t, string.format([[expected ~= "%s";]] .. '\n', expected), ...)
58+
end
59+
60+
function assertions:NotEqualf(t, expected, actual, fmt, ...)
61+
if expected ~= actual then
62+
return true
63+
end
64+
return self:Failf(t, string.format([[expected ~= "%s"; got "%s"%s%s]], expected, '\n', fmt), ...)
65+
end
66+
67+
function assertions:True(t, actual, ...)
68+
if actual then
69+
return true
70+
end
71+
return self:Fail(t, string.format([[expected true; got %s]] .. '\n', actual), ...)
72+
end
73+
74+
function assertions:Truef(t, actual, fmt, ...)
75+
if actual then
76+
return true
77+
end
78+
return self:Failf(t, string.format([[expected true; got %s%s%s]], actual, '\n', fmt), ...)
79+
end
80+
81+
return assertions

tests/assertions_const.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// GENERATED BY textFileToGoConst
2+
// GitHub: github.com/logrusorgru/textFileToGoConst
3+
// input file: assertions.lua
4+
// generated: Fri Nov 11 12:42:15 PST 2022
5+
6+
package tests
7+
8+
const lua_assertions = `local assertions = {
9+
fail_now = false,
10+
}
11+
12+
function assertions:new(o)
13+
o = o or {} -- create object if user does not provide one
14+
setmetatable(o, self)
15+
self.__index = self
16+
return o
17+
end
18+
19+
function assertions:__call(...)
20+
assert(self.call, 'attempt to call a non-function object')
21+
return self.call(...)
22+
end
23+
24+
function assertions:Fail(t, ...)
25+
t:LogHelper(2, ...)
26+
if self.fail_now then
27+
print("Failing now")
28+
t:FailNow()
29+
else
30+
print("Failing")
31+
t:Fail()
32+
end
33+
return false
34+
end
35+
36+
function assertions:Failf(t, fmt, ...)
37+
t:LogHelperf(2, fmt, ...)
38+
if self.fail_now then
39+
t:FailNow()
40+
else
41+
t:Fail()
42+
end
43+
return false
44+
end
45+
46+
function assertions:Equal(t, expected, actual, ...)
47+
if expected == actual then
48+
return true
49+
end
50+
return self:Fail(t, string.format([[expected "%s"; got "%s"]] .. '\n', expected, actual), ...)
51+
end
52+
53+
function assertions:Equalf(t, expected, actual, fmt, ...)
54+
if expected == actual then
55+
return true
56+
end
57+
return self:Failf(t, string.format([[expected "%s"; got "%s"%s%s]], expected, actual, '\n', fmt), ...)
58+
end
59+
60+
function assertions:NotEqual(t, expected, actual, ...)
61+
if expected ~= actual then
62+
return true
63+
end
64+
return self:Fail(t, string.format([[expected ~= "%s";]] .. '\n', expected), ...)
65+
end
66+
67+
function assertions:NotEqualf(t, expected, actual, fmt, ...)
68+
if expected ~= actual then
69+
return true
70+
end
71+
return self:Failf(t, string.format([[expected ~= "%s"; got "%s"%s%s]], expected, '\n', fmt), ...)
72+
end
73+
74+
function assertions:True(t, actual, ...)
75+
if actual then
76+
return true
77+
end
78+
return self:Fail(t, string.format([[expected true; got %s]] .. '\n', actual), ...)
79+
end
80+
81+
function assertions:Truef(t, actual, fmt, ...)
82+
if actual then
83+
return true
84+
end
85+
return self:Failf(t, string.format([[expected true; got %s%s%s]], actual, '\n', fmt), ...)
86+
end
87+
88+
return assertions
89+
`

tests/lua_const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// GENERATED BY textFileToGoConst
22
// GitHub: github.com/logrusorgru/textFileToGoConst
33
// input file: suite.lua
4-
// generated: Sat Nov 5 20:27:23 PDT 2022
4+
// generated: Fri Nov 11 12:42:15 PST 2022
55

66
package tests
77

tests/require.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local assertions = require 'assertions'
2+
local orig_require = require
3+
return assertions:new {
4+
fail_now = true,
5+
call = orig_require,
6+
}
7+

tests/require_const.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// GENERATED BY textFileToGoConst
2+
// GitHub: github.com/logrusorgru/textFileToGoConst
3+
// input file: require.lua
4+
// generated: Fri Nov 11 12:42:17 PST 2022
5+
6+
package tests
7+
8+
const lua_require = `local assertions = require 'assertions'
9+
local orig_require = require
10+
return assertions:new {
11+
fail_now = true,
12+
call = orig_require,
13+
}
14+
15+
`

tests/testdata/test_assertions.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local inspect = require 'inspect'
2+
local require = require 'require'
3+
local assert = require 'assert'
4+
5+
function TestAssertGivesTwoResults(t)
6+
assert:Equal(t, "one", "two", "foobar")
7+
assert:Equalf(t, "three", "four", "This is a test %s: %d", "foobar", 123)
8+
end
9+
10+
function TestRequireThenAssertGivesOneResult(t)
11+
require:Equal(t, "abc", "def")
12+
assert:Equal(t, true, false, "shouldn't reach here")
13+
end
14+
15+
function TestTrue(t)
16+
assert:True(t, false, 'zoiks')
17+
assert:Truef(t, false, 'crikey %d', 123)
18+
end
19+
20+
function TestTablesEqual(t)
21+
local t1 = {
22+
foo = "bar"
23+
}
24+
local t2 = {
25+
foo = "notbar"
26+
}
27+
assert:Equal(t, inspect(t1), inspect(t2, { process = remove_all_metatables }))
28+
end
29+
30+
function TestTablesNotEqual(t)
31+
local t1 = {
32+
foo = "bar"
33+
}
34+
local t2 = {
35+
foo = "bar"
36+
}
37+
assert:NotEqual(t, inspect(t1), inspect(t2, { process = remove_all_metatables }))
38+
end

0 commit comments

Comments
 (0)