Skip to content

Commit fbed399

Browse files
committed
Added some examples in README.
1 parent 604a154 commit fbed399

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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+
```

0 commit comments

Comments
 (0)