Skip to content

Commit 4cab9bd

Browse files
committed
Add NoError
1 parent 00e0588 commit 4cab9bd

File tree

7 files changed

+106
-59
lines changed

7 files changed

+106
-59
lines changed

base64/test/test_api.lua

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
1-
local base64 = require("base64")
2-
local strings = require("strings")
1+
local base64 = require 'base64'
2+
local strings = require 'strings'
3+
local assert = require 'assert'
34

45
function TestEncodeToString(t)
56
local tests = {
67
{
7-
name="input with \1 chars and RawStdEncoding",
8-
input="foo\01bar",
9-
encoder=base64.RawStdEncoding,
10-
expected="Zm9vAWJhcg",
8+
name = "input with \1 chars and RawStdEncoding",
9+
input = "foo\01bar",
10+
encoder = base64.RawStdEncoding,
11+
expected = "Zm9vAWJhcg",
1112
},
1213
{
13-
name="input with \1 chars and StdEncoding",
14-
input="foo\01bar",
15-
encoder=base64.StdEncoding,
16-
expected="Zm9vAWJhcg==",
14+
name = "input with \1 chars and StdEncoding",
15+
input = "foo\01bar",
16+
encoder = base64.StdEncoding,
17+
expected = "Zm9vAWJhcg==",
1718
},
1819
{
19-
name="input with <> chars and RawURLEncoding",
20-
input="this is a <tag> and should be encoded",
21-
encoder=base64.RawURLEncoding,
22-
expected="dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA",
20+
name = "input with <> chars and RawURLEncoding",
21+
input = "this is a <tag> and should be encoded",
22+
encoder = base64.RawURLEncoding,
23+
expected = "dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA",
2324
},
2425
{
25-
name="input with <> chars and URLEncoding",
26-
input="this is a <tag> and should be encoded",
27-
encoder=base64.URLEncoding,
28-
expected="dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==",
26+
name = "input with <> chars and URLEncoding",
27+
input = "this is a <tag> and should be encoded",
28+
encoder = base64.URLEncoding,
29+
expected = "dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==",
2930
},
3031
}
3132
for _, tt in ipairs(tests) do
3233
t:Run(tt.name, function(t)
3334
local got = tt.encoder:encode_to_string(tt.input)
34-
assert(tt.expected == got, string.format("'%s' ~= '%s'", tt.expected, got))
35+
assert:Equal(t, tt.expected , got)
3536
end)
3637
end
3738
end
3839

3940
function TestDecodeString(t)
4041
local tests = {
4142
{
42-
name="input with \1 chars and RawStdEncoding",
43-
input="Zm9vAWJhcg",
44-
encoder=base64.RawStdEncoding,
45-
expected="foo\01bar",
43+
name = "input with \1 chars and RawStdEncoding",
44+
input = "Zm9vAWJhcg",
45+
encoder = base64.RawStdEncoding,
46+
expected = "foo\01bar",
4647
},
4748
{
48-
name="input with \1 chars and StdEncoding",
49-
input="Zm9vAWJhcg==",
50-
encoder=base64.StdEncoding,
51-
expected="foo\01bar",
49+
name = "input with \1 chars and StdEncoding",
50+
input = "Zm9vAWJhcg==",
51+
encoder = base64.StdEncoding,
52+
expected = "foo\01bar",
5253
},
5354
{
54-
name="input with <> chars and RawURLEncoding",
55-
input="dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA",
56-
encoder=base64.RawURLEncoding,
57-
expected="this is a <tag> and should be encoded",
55+
name = "input with <> chars and RawURLEncoding",
56+
input = "dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA",
57+
encoder = base64.RawURLEncoding,
58+
expected = "this is a <tag> and should be encoded",
5859
},
5960
{
60-
name="input with <> chars and URLEncoding",
61-
input="dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==",
62-
encoder=base64.URLEncoding,
63-
expected="this is a <tag> and should be encoded",
61+
name = "input with <> chars and URLEncoding",
62+
input = "dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==",
63+
encoder = base64.URLEncoding,
64+
expected = "this is a <tag> and should be encoded",
6465
},
6566
}
6667
for _, tt in ipairs(tests) do
@@ -79,24 +80,24 @@ end
7980
function TestEncodeDecode(t)
8081
local tests = {
8182
{
82-
name="input with \1 chars and RawStdEncoding",
83-
input="foo\01bar",
84-
encoder=base64.RawStdEncoding,
83+
name = "input with \1 chars and RawStdEncoding",
84+
input = "foo\01bar",
85+
encoder = base64.RawStdEncoding,
8586
},
8687
{
87-
name="input with \1 chars and StdEncoding",
88-
input="foo\01bar",
89-
encoder=base64.StdEncoding,
88+
name = "input with \1 chars and StdEncoding",
89+
input = "foo\01bar",
90+
encoder = base64.StdEncoding,
9091
},
9192
{
92-
name="input with <> chars and RawURLEncoding",
93-
input="this is a <tag> and should be encoded",
94-
encoder=base64.RawURLEncoding,
93+
name = "input with <> chars and RawURLEncoding",
94+
input = "this is a <tag> and should be encoded",
95+
encoder = base64.RawURLEncoding,
9596
},
9697
{
97-
name="input with <> chars and URLEncoding",
98-
input="this is a <tag> and should be encoded",
99-
encoder=base64.URLEncoding,
98+
name = "input with <> chars and URLEncoding",
99+
input = "this is a <tag> and should be encoded",
100+
encoder = base64.URLEncoding,
100101
},
101102
}
102103
for _, tt in ipairs(tests) do

tests/assert_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: assert.lua
4-
// generated: Fri Nov 11 18:09:20 PST 2022
4+
// generated: Fri Nov 11 18:27:20 PST 2022
55

66
package tests
77

tests/assertions.lua

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,22 @@ function assertions:cleanseString(s)
2222
end
2323

2424
function assertions:Fail(t, ...)
25+
assert(t.LogHelper, "First parameter must be t (the testing.T object)")
2526
t:LogHelper(2, ...)
2627
if self.fail_now then
27-
assert(t.FailNow, "First parameter must be t (the testing.T object)")
2828
t:FailNow()
2929
else
30-
assert(t.Fail, "First parameter must be t (the testing.T object)")
3130
t:Fail()
3231
end
3332
return false
3433
end
3534

3635
function assertions:Failf(t, fmt, ...)
36+
assert(t.LogHelperf, "First parameter must be t (the testing.T object)")
3737
t:LogHelperf(2, fmt, ...)
3838
if self.fail_now then
39-
assert(t.FailNow, "First parameter must be t (the testing.T object)")
4039
t:FailNow()
4140
else
42-
assert(t.Fail, "First parameter must be t (the testing.T object)")
4341
t:Fail()
4442
end
4543
return false
@@ -134,4 +132,27 @@ Messages: %s
134132
]], fmt), ...)
135133
end
136134

135+
function assertions:NoError(t, err, ...)
136+
if not err then
137+
return true
138+
end
139+
return self:Fail(t, string.format([[
140+
141+
Error: Received unexpected error:
142+
%s
143+
Messages: ]], err), ...)
144+
end
145+
146+
function assertions:NoErrorf(t, err, fmt, ...)
147+
if not err then
148+
return true
149+
end
150+
return self:Fail(t, string.format([[
151+
152+
Error: Received unexpected error:
153+
%s
154+
Messages: %s
155+
]], err, fmt), ...)
156+
end
157+
137158
return assertions

tests/assertions_const.go

Lines changed: 26 additions & 5 deletions
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: assertions.lua
4-
// generated: Fri Nov 11 18:09:19 PST 2022
4+
// generated: Fri Nov 11 18:27:19 PST 2022
55

66
package tests
77

@@ -29,24 +29,22 @@ function assertions:cleanseString(s)
2929
end
3030
3131
function assertions:Fail(t, ...)
32+
assert(t.LogHelper, "First parameter must be t (the testing.T object)")
3233
t:LogHelper(2, ...)
3334
if self.fail_now then
34-
assert(t.FailNow, "First parameter must be t (the testing.T object)")
3535
t:FailNow()
3636
else
37-
assert(t.Fail, "First parameter must be t (the testing.T object)")
3837
t:Fail()
3938
end
4039
return false
4140
end
4241
4342
function assertions:Failf(t, fmt, ...)
43+
assert(t.LogHelperf, "First parameter must be t (the testing.T object)")
4444
t:LogHelperf(2, fmt, ...)
4545
if self.fail_now then
46-
assert(t.FailNow, "First parameter must be t (the testing.T object)")
4746
t:FailNow()
4847
else
49-
assert(t.Fail, "First parameter must be t (the testing.T object)")
5048
t:Fail()
5149
end
5250
return false
@@ -141,5 +139,28 @@ Messages: %s
141139
]], fmt), ...)
142140
end
143141
142+
function assertions:NoError(t, err, ...)
143+
if not err then
144+
return true
145+
end
146+
return self:Fail(t, string.format([[
147+
148+
Error: Received unexpected error:
149+
%s
150+
Messages: ]], err), ...)
151+
end
152+
153+
function assertions:NoErrorf(t, err, fmt, ...)
154+
if not err then
155+
return true
156+
end
157+
return self:Fail(t, string.format([[
158+
159+
Error: Received unexpected error:
160+
%s
161+
Messages: %s
162+
]], err, fmt), ...)
163+
end
164+
144165
return assertions
145166
`

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: Fri Nov 11 18:09:19 PST 2022
4+
// generated: Fri Nov 11 18:27:19 PST 2022
55

66
package tests
77

tests/require_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: require.lua
4-
// generated: Fri Nov 11 18:09:21 PST 2022
4+
// generated: Fri Nov 11 18:27:21 PST 2022
55

66
package tests
77

tests/testdata/test_assertions_failing.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@ end
4343

4444
function TestStringsEqual(t)
4545
assert:Equal(t, "expected text", "actual text")
46+
end
47+
48+
function TestNoError(t)
49+
assert:NoError(t, "foo bar")
4650
end

0 commit comments

Comments
 (0)