-
Notifications
You must be signed in to change notification settings - Fork 16
/
impl_test.lua
270 lines (244 loc) · 9.17 KB
/
impl_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
local createPomodoro = require('impl')
local wibox = {
widget = {
textbox = function()
return {
set_markup = function(self, s) return nil end,
buttons = function(self, bs) return nil end,
}
end,
imagebox = function()
return {
set_image = function(self, image_path) return nil end,
buttons = function(self, bs) return nil end,
}
end,
base = {
make_widget = function()
return {
emit_signal = function(self, s) return nil end
}
end
}
}
}
local awful = {
util = {
getdir = function(str) return '/home/cooluser/.config/awesome' end,
table = {
join = function(elements) return nil end
}
},
spawn = {
pread = function(cmd) return "" end,
},
button = function(modifier, mouseButton, f) return nil end,
tooltip = function(table) return nil end
}
local naughty = {
notify = function(bg, fg, title, text, timeout)
end
}
local beautiful = {}
local timer = function(t)
return {
again = function(self, f) return nil end,
stop = function(self) return nil end,
connect_signal = function(self, f) return nil end,
}
end
local awesome = {
connect_signal = function(self, f) return nil end,
}
local pomodoro = createPomodoro(wibox, awful, naughty, beautiful, timer, awesome)
describe("Should set the default values properly", function()
it('pause duration should be 5 minutes', function()
assert.are.equal(300, pomodoro.pause_duration)
end)
it('work duration should be set to 25 minutes', function()
assert.are.equal(1500, pomodoro.work_duration)
end)
it('default changing value for increasing and decreasing should be one minute', function()
assert.are.equal(60, pomodoro.change)
end)
it('working pomodoro should be the next state', function()
assert.are.equal(true, pomodoro.working)
end)
end)
describe('Set time should change the textbox appropriately', function()
local s = spy.on(pomodoro.widget, "set_markup")
it('more than one hour pomodoro should be formatted with an hour part', function()
pomodoro:settime(3601)
assert.spy(s).was_called_with(pomodoro.widget, "Pomodoro: <b>01:00:01</b>")
end)
it('less than one hour should be set with only minutes and seconds', function()
pomodoro:settime(1500)
assert.spy(s).was_called_with(pomodoro.widget, "Pomodoro: <b>25:00</b>")
end)
end)
describe('Notifications should send a naughty notification and change pomodoro object appropriately', function()
-- TODO: For some reason I cannot mock naughty...
it('naughty should be called properly', function()
local s = spy.on(naughty, 'notify')
pomodoro:notify('title', 'sometext', 10, true)
end)
it('should set the left to the new duration for the new state', function()
pomodoro:notify('title', 'sometext', 10, true)
assert.are.equal(10, pomodoro.left)
end)
it('should set working to false if the next timer is not for a work duration', function()
pomodoro:notify('title', 'sometext', 10, false)
assert.are.equal(false, pomodoro.working)
end)
end)
describe('Starting a pomodoro', function()
it('should start the timer', function()
local s = spy.on(pomodoro.timer, 'again')
pomodoro:start()
assert.spy(s).was_called_with(pomodoro.timer)
end)
end)
describe('Stopping a pomodoro', function()
it('should stop the timer', function()
local s = spy.on(pomodoro.timer, 'stop')
pomodoro:stop()
assert.spy(s).was_called_with(pomodoro.timer)
end)
it('should set the next pomodoro to be for work', function()
pomodoro:stop()
assert.are.equal(true, pomodoro.working)
end)
it('should set time left to the work duration', function()
pomodoro:stop()
assert.are.equal(1500, pomodoro.left)
end)
it('should set the textbox to the work duration', function()
local s = spy.on(pomodoro, 'settime')
pomodoro:stop()
assert.spy(s).was_called_with(pomodoro, 1500)
end)
end)
describe('Pausing a pomodoro', function()
it('should stop the timer', function()
local s = spy.on(pomodoro.timer, 'stop')
pomodoro:stop()
assert.spy(s).was_called_with(pomodoro.timer)
end)
end)
describe('Preserve the pomodoro before restart if any', function()
it('should find the last time in X resource DB', function()
awful.spawn.pread = function(s)
return [[
awesome.Pomodoro.time: 716
XTerm*faceName: consolas
xterm*.background: grey5
]]
end
pomodoro:init()
assert.are.equal(716, pomodoro.left)
end)
it('should start the pomodoro right away if the value is found in the database after a restart and it was started', function()
local s = spy.on(pomodoro, 'start')
awful.spawn.pread = function(s)
return [[
awesome.Pomodoro.time: 716
awesome.Pomodoro.started: 1
XTerm*faceName: consolas
xterm*.background: grey5
]]
end
pomodoro:init()
assert.spy(s).was_called()
end)
it('should use the normal duration and don\'t start a pomodoro if not found in the database', function()
local s = spy.on(pomodoro, 'start')
awful.spawn.pread = function(s)
return [[
awesome.pomodoro.time: 716
XTerm*faceName: consolas
xterm*.background: grey5
]]
end
pomodoro:init()
assert.spy(s).was_not_called()
assert.are.equal(1500, pomodoro.left)
end)
it('should not start the timer if it was paused or stopped', function()
local s = spy.on(pomodoro, 'start')
awful.spawn.pread = function(s)
return [[
awesome.Pomodoro.time: 716
awesome.Pomodoro.started: 0
XTerm*faceName: consolas
xterm*.background: grey5
]]
end
pomodoro:init()
assert.spy(s).was_not_called()
assert.are.equal(716, pomodoro.left)
end)
end)
describe('Should use the images properly', function()
path_we_got = nil
local pomodoro = createPomodoro(wibox, awful, naughty, beautiful, timer, awesome)
pomodoro.module_path = "pomodoro/"
pomodoro.icon_widget.set_image = function(self, image_path)
path_we_got = image_path
end
pomodoro.working = true
it('should set the default icon to gray by default', function()
pomodoro:init()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/gray.png', path_we_got)
end)
it('should set the image to the locked one when we pause a pomodoro', function()
pomodoro:pause()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/locked.png', path_we_got)
end)
it('should set the image to the gray one when we stop a pomodoro', function()
pomodoro:stop()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/gray.png', path_we_got)
end)
it('should change the image depending on the time that elapsed for the pomodoro', function()
-- there is more than 2/3 from the next break
pomodoro.left = 26
pomodoro.work_duration = 30
pomodoro:ticking_time()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/green.png', path_we_got)
-- there is more than 1/3 from the next break but smaller than 2/3
pomodoro.left = 16
pomodoro.work_duration = 30
pomodoro:ticking_time()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/orange.png', path_we_got)
pomodoro.left = 9
pomodoro.work_duration = 30
pomodoro:ticking_time()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/red.png', path_we_got)
end)
it('should set the icon back to gray when the pomodoro finishes', function()
pomodoro.left = 0
pomodoro:ticking_time()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/gray.png', path_we_got)
end)
it('shouldn\'t change the icon if we are currently not working', function()
pomodoro.working = false
pomodoro.work_duration = 30
interval_elements = {26, 16, 9}
for i, element in ipairs(interval_elements) do
pomodoro.left = element
pomodoro:ticking_time()
assert.are.equal('/home/cooluser/.config/awesome/pomodoro/images/green.png', path_we_got)
end
end)
end)
describe('Long breaks', function()
local pomodoro = createPomodoro(wibox, awful, naughty, beautiful, timer, awesome)
it('should properly start a long break after 4 full pomodoros', function()
for i=1,4,1 do
pomodoro.working = true
pomodoro.left = 0
assert.are.not_equal(15 * 60, pomodoro.pause_duration)
pomodoro:ticking_time()
end
assert.are.equal(15 * 60, pomodoro.pause_duration)
end)
end)