forked from lunarmodules/luassert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_spec.lua
145 lines (126 loc) · 4.81 KB
/
state_spec.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
describe("Tests states of the assert engine", function()
it("checks levels created/reverted", function()
local start = assert:snapshot()
assert.is_nil(start.next)
local snapshot1 = assert:snapshot()
assert.is.table(start.next)
assert.are.equal(start.next, snapshot1)
assert.are.equal(start, snapshot1.previous)
assert.is_nil(snapshot1.next)
local snapshot2 = assert:snapshot()
assert.is.table(snapshot1.next)
assert.are.equal(snapshot2, snapshot1.next)
assert.are.equal(snapshot2.previous, snapshot1)
assert.is_nil(snapshot2.next)
snapshot2:revert()
assert.is.table(start.next)
assert.are.equal(start.next, snapshot1)
assert.are.equal(start, snapshot1.previous)
assert.is_nil(snapshot1.next)
snapshot1:revert()
assert.is_nil(start.next)
end)
it("checks to see if a formatter is reversed", function()
-- add a state level by creating a snapshot
local snapshot1 = assert:snapshot()
-- register extra formatters
local fmt1 = function(value)
if type(value) == "string" then return "ok" end
end
assert:add_formatter(fmt1)
local fmt2 = function(value)
if type(value) == "number" then return "1" end
end
assert:add_formatter(fmt2)
-- check formatters
assert.are.equal(#snapshot1.formatters, 2)
assert.are.equal(snapshot1.formatters[2], fmt1)
assert.are.equal(snapshot1.formatters[1], fmt2)
assert.are.equal("ok", assert:format({"some value"})[1])
assert.are.equal("1", assert:format({123})[1])
-- add another state level by creating a snapshot
local snapshot2 = assert:snapshot()
-- register extra formatter
local fmt3 = function(value)
if type(value) == "number" then return "2" end
end
assert:add_formatter(fmt3)
assert.are.equal(#snapshot2.formatters, 1)
assert.are.equal(snapshot2.formatters[1], fmt3)
-- check formatter newest level
assert.are.equal("2", assert:format({123})[1])
-- check formatter previous level
assert.are.equal("ok", assert:format({"some value"})[1])
-- check formatter initial level
assert.are.equal("(boolean) true", assert:format({true})[1])
-- revert 1 state up
snapshot2:revert()
assert.is_nil(snapshot1.next)
assert.are.equal(2, #snapshot1.formatters)
-- check formatter reverted level
assert.are.equal("1", assert:format({123})[1])
-- check formatter unchanged level
assert.are.equal("ok", assert:format({"some value"})[1])
-- check formatter unchanged level
assert.are.equal("(boolean) true", assert:format({true})[1])
-- revert 1 more up, to initial level
snapshot1:revert()
assert.are.equal("(number) 123", assert:format({123})[1])
assert.are.equal("(string) 'some value'", assert:format({"some value"})[1])
assert.are.equal("(boolean) true", assert:format({true})[1])
end)
it("checks to see if a parameter is reversed", function()
-- add a state level by creating a snapshot
local snapshot1 = assert:snapshot()
assert.is_nil(assert:get_parameter("Test_1"))
assert.is_nil(assert:get_parameter("Test_2"))
assert:set_parameter("Test_1", 1)
assert:set_parameter("Test_2", 2)
assert.are.equal(1, assert:get_parameter("Test_1"))
assert.are.equal(2, assert:get_parameter("Test_2"))
-- add another state level by creating a snapshot
local snapshot2 = assert:snapshot()
assert.are.equal(1, assert:get_parameter("Test_1"))
assert.are.equal(2, assert:get_parameter("Test_2"))
assert:set_parameter("Test_1", "one")
assert:set_parameter("Test_2", nil) -- test setting to nil
assert.are.equal("one", assert:get_parameter("Test_1"))
assert.is_nil(assert:get_parameter("Test_2"))
-- revert 1 state up
snapshot2:revert()
assert.are.equal(1, assert:get_parameter("Test_1"))
assert.are.equal(2, assert:get_parameter("Test_2"))
-- revert 1 more up, to initial level
snapshot1:revert()
assert.is_nil(assert:get_parameter("Test_1"))
assert.is_nil(assert:get_parameter("Test_2"))
end)
it("checks to see if a spy/stub is reversed", function()
local c1, c2 = 0, 0
local test = {
f1 = function() c1 = c1 + 1 end,
f2 = function() c2 = c2 + 1 end,
}
-- add a state level by creating a snapshot
local snapshot1 = assert:snapshot()
-- create spy/stub
local s1 = spy.on(test, "f1")
local s2 = stub(test, "f2")
-- call them both
test.f1()
test.f2()
assert.spy(test.f1).was.called(1)
assert.spy(test.f2).was.called(1)
assert.is_equal(1, c1)
assert.is_equal(0, c2) -- 0, because it's a stub
-- revert to initial level
snapshot1:revert()
test.f1()
test.f2()
-- check count is still 1 for both
assert.spy(s1).was.called(1)
assert.spy(s2).was.called(1)
assert.is_equal(2, c1)
assert.is_equal(1, c2)
end)
end)