-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.lua
180 lines (141 loc) · 4.57 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
local spy = require("luassert.spy")
local stub = require("luassert.stub")
local assert = require("luassert")
local say = require("say")
local expect = require("bustez")()
expect(nil).to.never.be.ok()
expect(false).to.be.ok()
expect(true).to.be.ok()
expect(true).to.be.have.been.was.at.is.are.has.does.ok()
expect(true).to.never.never.be.ok()
expect(nil).to_not.be.ok()
expect(nil).not_to.be.ok()
expect(nil).to.be.no.ok()
expect(nil)._not.to.be.ok()
expect(nil)._not.to_be_ok()
expect(true).to.equal(true)
expect(true).to.never.equal(false)
expect(3).to.equal(3)
expect(1).to.never.equal(2)
do
local tab = {}
expect(tab).to.equal(tab)
expect(tab).to.never.equal({})
end
expect(1.999).to.be.near(2, 0.01)
expect(1).to.never.be.near(2, 0.1)
expect(true).to.be.a("boolean")
expect(4.5).to.be.a("number")
expect({}).to.be.a("table")
expect(true).to.be_true()
expect(false).to.be_false()
expect(5).to.be.a.number()
expect(true).to.be.a.boolean()
expect("foo").to.be.a.string()
expect(coroutine.create(function() end)).to.be.a.thread()
expect({}).to.be.a.table()
do
local file = io.open("test.lua")
expect(file).to.be.a.userdata()
file:close()
end
expect(function() end).to.be.a_function()
expect(nil).to.be_nil()
expect(true).to.be.like(true)
expect(true).to.look.like(true)
expect(true).to.never.be.like(false)
expect({ 1, 2, 3 }).to.be.like({ 1, 2, 3 })
expect({ 45, 50 }).to.never.be.like({ 90, 100 })
expect("value").to.never.be.like(nil)
expect(error).to.throw()
expect(function() end).to.never.throw()
local function fail()
error("oh no")
end
expect(fail).to.throw("oh no")
expect(fail).to.never.throw("o")
expect(fail).to.never.throw("Oh no!")
expect(function()
expect(nil).to.be.ok("totally not okay!")
end).to.match.error("totally not okay!")
local function assertFalse()
assert(false)
end
expect(assertFalse).to.throw("assertion failed!")
expect("some string").to.match("some")
expect("some string").to.match("string")
expect("\t \t \n").to.match("^%s+$")
expect("abcd").to.match("[a-z]*")
expect({ a = { 1, 2 }, b = { 1, 2 } }).never.to.be.unique(true)
expect({ a = { 1, 2 }, b = { 1, 2 } }).to.be.unique(false)
expect.array({ 1, 2, 3 }).to.have.no.holes()
expect.array({ 1, 2, 3 }).to.have.holes(4)
expect.array({ 1, 2, nil, 4 }).to.have.holes()
do
local funSpy = spy.new(function(...)
return 1, 2, 3
end)
funSpy(8, 10, 12)
expect.spy(funSpy).to.be.called_with(8, 10, 12)
expect.spy(funSpy).to.have.returned_with(1, 2, 3)
funSpy()
funSpy()
expect.spy(funSpy).to.be.called(3)
expect.spy(funSpy).to.be.called.at.least(1)
expect.spy(funSpy).to.be.called.at.least(3)
expect.spy(funSpy).not_to.be.called.at.least(4)
expect.spy(funSpy).not_to.be.called.at.most(2)
expect.spy(funSpy).to.be.called.at.most(3)
expect.spy(funSpy).to.be.called.at.most(5)
end
do
local obj = { some = function(...) end }
local funStub = stub(obj, "some")
obj.some(2, 3, 5)
expect.stub(funStub).to.be.called_with(2, 3, 5)
end
do -- error message is a string by default
local _, msg = pcall(function()
expect(true).to.be_false()
end)
expect(msg).to.be.a.string()
end
do -- and busted.fail is used when busted is loaded
package.loaded["luassert.bustez.expect"] = nil
require("busted")
local busted_expect = require("luassert.bustez.expect")
local _, msg = pcall(function()
busted_expect(true).to.be_false()
end)
expect(msg).to.be.a.table()
end
do
---@class bustez.Expectation
---asserts that our expectation is not `nil`
---@field exist fun(): bustez.Expectation
say:set("assertion.exist.positive", "Expected to exist, but value was:\n%s")
say:set("assertion.exist.negative", "Expected to not exist, but value was:\n%s")
local function exist(state, arguments, level)
return arguments[1] ~= nil
end
assert:register("assertion", "exist", exist, "assertion.exist.positive", "assertion.exist.negative")
expect(false).to.exist()
expect(nil).to.never.exist()
end
do
---@class bustez.Expectation
---asserts that our expectation contains the given field
---@field field fun(key: string): bustez.Expectation
say:set("assertion.field.positive", "Expected to have property.\nObject:\n%s\nProperty:\n%s")
say:set("assertion.field.negative", "Expected not to have property.\nObject:\n%s\nProperty:\n%s")
---@type bustez.Matcher
local function field(state, arguments, level)
if type(arguments[1]) ~= "table" then
error("expectation must be a table in have_property assertion")
end
return arguments[1][arguments[2]] ~= nil
end
assert:register("assertion", "field", field, "assertion.field.positive", "assertion.field.negative")
expect({ a = 1 }).to.have.field("a")
expect({}).to.never.have.field("a")
end