Skip to content

Commit bcf14a6

Browse files
authored
Merge pull request #53 from scr-oath/assertion-helpers
Add assert and require features like testify has.
2 parents 3dec4ec + fbed399 commit bcf14a6

File tree

13 files changed

+767
-69
lines changed

13 files changed

+767
-69
lines changed

base64/test/test_api.lua

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,111 @@
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
6768
t:Run(tt.name, function(t)
6869
local got, err = tt.encoder:decode_string(tt.input)
6970
if tt.want_err then
70-
assert(err, "expected err")
71+
assert:Error(t, err)
7172
return
7273
end
73-
assert(not err, err)
74-
assert(tt.expected == got, string.format("'%s' ~= '%s'", tt.expected, got))
74+
assert:NoError(t, err)
75+
assert:Equal(t, tt.expected, got)
7576
end)
7677
end
7778
end
7879

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
103104
t:Run(tt.name, function(t)
104105
local encoded = tt.encoder:encode_to_string(tt.input)
105106
local decoded, err = tt.encoder:decode_string(encoded)
106-
assert(not err, err)
107-
assert(tt.input == decoded, string.format("'%s' ~= '%s'", tt.input, decoded))
107+
assert:NoError(t, err)
108+
assert:Equal(t, tt.input, decoded)
108109
end)
109110
end
110111
end
@@ -115,42 +116,42 @@ function TestEncoder(t)
115116
encoder:write("foo", "bar", "baz")
116117
encoder:close()
117118
local s = writer:string()
118-
assert(s == "Zm9vYmFyYmF6", string.format("'%s' ~= '%s'", s, "Zm9vYmFyYmF6"))
119+
assert:Equal(t, "Zm9vYmFyYmF6", s)
119120
end
120121

121122
function TestDecoder(t)
122123
local reader = strings.new_reader("Zm9vYmFyYmF6")
123124
local decoder = base64.new_decoder(base64.StdEncoding, reader)
124125
local s = decoder:read("*a")
125-
assert(s == "foobarbaz", string.format("'%s' ~= '%s'", s, "foobarbaz"))
126+
assert:Equal(t, "foobarbaz", s)
126127
end
127128

128129
function TestDecoderReadNum(t)
129130
local encoded = base64.StdEncoding:encode_to_string("123 456 789")
130131
local reader = strings.new_reader(encoded)
131132
local decoder = base64.new_decoder(base64.StdEncoding, reader)
132133
local n = decoder:read("*n")
133-
assert(n == 123, string.format("%d ~= %d", n, 123))
134+
assert:Equal(t, 123, n)
134135
n = decoder:read("*n")
135-
assert(n == 456, string.format("%d ~= %d", n, 456))
136+
assert:Equal(t, 456, n)
136137
n = decoder:read("*n")
137-
assert(n == 789, string.format("%d ~= %d", n, 789))
138+
assert:Equal(t, 789, n)
138139
end
139140

140141
function TestDecoderReadCount(t)
141142
local encoded = base64.StdEncoding:encode_to_string("123 456 789")
142143
local reader = strings.new_reader(encoded)
143144
local decoder = base64.new_decoder(base64.StdEncoding, reader)
144145
local s = decoder:read(3)
145-
assert(s == "123", string.format("'%s' ~= '%s'", s, "123"))
146+
assert:Equal(t, "123", s)
146147
end
147148

148149
function TestDecoderReadline(t)
149150
local encoded = base64.StdEncoding:encode_to_string("foo\nbar")
150151
local reader = strings.new_reader(encoded)
151152
local decoder = base64.new_decoder(base64.StdEncoding, reader)
152153
local s = decoder:read("*l")
153-
assert(s == "foo", string.format("'%s' ~= '%s'", s, "foo"))
154+
assert:Equal(t, "foo", s)
154155
s = decoder:read("*l")
155-
assert(s == "bar", string.format("'%s' ~= '%s'", s, "bar"))
156+
assert:Equal(t, "bar", s)
156157
end

tests/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,42 @@ function TestSuite(t)
9393
assert(MySuite.tearDownSuiteCount == 1, tostring(MySuite.tearDownSuiteCount))
9494
end
9595
```
96+
97+
## Example use of assert and require
98+
99+
Similar to testify [assert](https://pkg.go.dev/github.com/stretchr/testify/assert) and
100+
[require](https://pkg.go.dev/github.com/stretchr/testify/require), Lua's `assert` and `require` can be enhanced to
101+
add structured assertions.
102+
103+
```lua
104+
local require = require 'require'
105+
local assert = require 'assert'
106+
local inspect = require 'inspect'
107+
108+
function TestAssertions(t)
109+
local s1 = "foo"
110+
local s2 = "foo"
111+
assert:Equal(t, s1, s2)
112+
assert:Equalf(t, s1, s2, "I really didn't expect them to be equal %d", 123)
113+
114+
local o1 = {
115+
foo = "bar",
116+
}
117+
local o2 = {
118+
foo = "bar",
119+
}
120+
assert:Equal(t, inspect(o1), inspect(o2))
121+
assert:NotEqual(t, 123, 456, [[wow - they're equal?]])
122+
123+
local err = nil
124+
assert:NoError(t, err, "I got an error?!?")
125+
126+
assert:False(t, false, "expected false")
127+
assert:Falsef(t, false, "expected false for %s", "foobar")
128+
129+
assert:True(t, true, "I wanted the truth")
130+
131+
err = 'foo bar'
132+
assert:Error(t, err)
133+
end
134+
```

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 18:29:58 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+
`

0 commit comments

Comments
 (0)