-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserkio-tagger-tests.lua
274 lines (229 loc) · 10.4 KB
/
serkio-tagger-tests.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
270
271
272
273
274
describe('Serkio tagger', function()
describe('Entering tag', function()
tagger = require('serkio-tagger')
-- utility function to repeatedly call `tag_input_handler`
local function input(chars)
for k, v in pairs(chars) do
tagger:tag_input_handler(v)
end
end
describe('Return keypress', function()
it('Should switch to normal mode without input',
function()
input({'enter'})
assert.are_equal('normal', tagger.state.mode)
end)
it('Should switch to normal mode with input',
function()
input({'b', 'u', 'b', 'b', 'l', 'e', 'g', 'u', 'm', 'enter'})
assert.are_equal('normal', tagger.state.mode)
end)
it('Should clear input buffer',
function()
input({'i', 'c', 'e', '-', 'k', 'i', 'n', 'g', 'enter'})
assert.are_equal('', tagger.state.input_tag_string)
end)
it('Should choose tag if input is provided',
function()
input({'f', 'i', 'n', 'n', 'enter'})
assert.are_equal('finn', tagger.state.chosen_tag)
end)
it('Should select a tag',
function()
stub(tagger, 'select_tag')
input({'j', 'a', 'k', 'e', 'enter'})
assert.stub(tagger.select_tag).was.called()
tagger.select_tag:revert()
end)
end)
describe('Escape keypress', function()
it('Should switch to normal mode without input',
function()
input({'esc'})
assert.are_equal('normal', tagger.state.mode)
end)
it('Should switch to normal mode with input',
function()
input({'b', 'e', 'e', 'm', 'o', 'esc'})
assert.are_equal('normal', tagger.state.mode)
end)
it('Should discard tag input',
function()
input({'m', 'a', 'r', 'c', 'e', 'l', 'i', 'n', 'e', 'esc'})
assert.are_equal('', tagger.state.input_tag_string)
end)
it('Should not change the current chosen tag',
function()
tagger.state.chosen_tag = 'peppermint-butler'
input({'t', 'h', 'e', '-', 'l', 'i', 'c', 'h', 'esc'})
assert.are_equal('peppermint-butler', tagger.state.chosen_tag)
end)
end)
describe('Backspace keypress', function()
it('Should delete the last character if input exists',
function()
tagger.state.input_tag_string = 'tree-forta'
input({'bs'})
assert.are_equal('tree-fort', tagger.state.input_tag_string)
end)
it('Should do nothing if there is no input',
function()
tagger.state.input_tag_string = ''
input({'bs'})
assert.are_equal('', tagger.state.input_tag_string)
end)
end)
describe('Clean input', function()
it('Should convert uppercase letters to lowercase',
function()
input({'F', 'o', 'R', 'e', 'S', 't'})
assert.are_equal('forest', tagger.state.input_tag_string)
end)
it('Should not allow two or more dashes in a row',
function()
tagger.state.input_tag_string = ''
input({'l', '-', '-', 'm', '-', '-', 'o'})
assert.are_equal('l-m-o', tagger.state.input_tag_string)
end)
it('Should not allow a tag to start with a dash',
function()
tagger.state.input_tag_string = ''
input({'-'})
assert.are_equal('', tagger.state.input_tag_string)
end)
it('Should not allow a tag to end with a dash',
function()
tagger.state.input_tag_string = ''
tagger.state.chosen_tag = ''
input({'c', 'a', 'v', 'e', '-', 'enter'})
assert.are_equal('cave', tagger.state.chosen_tag)
end)
end)
end)
describe('utility functions', function()
local tagger = require('serkio-tagger')
it('`colour` should convert colours correctly', function()
assert.are_equal(
'{\\1a&HFF&\\1c&HCCBBAA&}',
tagger.colour(1, 'AABBCC00')
)
end)
it('`get_filename` should return a suitable filename to store tag data', function()
tagger.mp.get_property = function(property)
return '/home/matt/Rick.and.Morty.S03E02.720p.HDTV.x264-BATV.mkv'
end
assert.are_equal(
'/home/matt/Rick.and.Morty.S03E02.720p.HDTV.x264-BATV.json',
tagger:get_filename()
)
end)
end)
describe('time conversion functionality', function()
local tagger = require('serkio-tagger')
it('`ms_to_time` should convert milliseconds to `HH:MM:SS.mmm` format', function()
assert.are_equal('01:03:03.007', tagger.ms_to_time(3783007))
end)
it('`time_to_ms` should convert a `HH:MM:SS.mmm` time string to milliseconds', function()
assert.are_equal(7, tagger.time_to_ms('00:00:00.007'))
assert.are_equal(3007, tagger.time_to_ms('00:00:03.007'))
assert.are_equal(183007, tagger.time_to_ms('00:03:03.007'))
assert.are_equal(3783007, tagger.time_to_ms('01:03:03.007'))
end)
end)
describe('tag data api', function()
local tagger = require('serkio-tagger')
it('`remove_tag` should remove a tag instance', function()
tagger.data.tags = {jake={{1, 5}, {12, 13}}}
tagger:remove_tag('jake', 1, 5)
assert.are_same({jake={{12, 13}}}, tagger.data.tags)
end)
it('`remove_tag` should remove a tag if no more instances exist', function()
tagger.data.tags = {jake={{1, 5}}, finn={{1, 5}}}
tagger:remove_tag('jake', 1, 5)
assert.are_same({finn={{1, 5}}}, tagger.data.tags)
end)
it('`add_tag` should add a tag instance', function()
tagger.data.tags = {}
tagger:add_tag('jake', 1, 5)
assert.are_same({jake={{1, 5}}}, tagger.data.tags)
tagger:add_tag('jake', 3, 6)
assert.are_same({jake={{1, 5}, {3, 6}}}, tagger.data.tags)
end)
it('`add_tag` should order the start position before the end position', function()
tagger.data.tags = {}
tagger:add_tag('jake', 5, 1)
assert.are_same({jake={{1, 5}}}, tagger.data.tags)
end)
it('`add_tag` should order tags chronologically', function()
tagger.data.tags = {}
tagger:add_tag('jake', 3, 5)
tagger:add_tag('jake', 1, 2)
tagger:add_tag('jake', 7, 9)
assert.are_same({jake={{1, 2}, {3, 5}, {7, 9}}}, tagger.data.tags)
end)
it('`add_tag` should merge tags if they overlap', function()
tagger.data.tags = {jake={{3, 4}, {5, 7}}}
tagger:add_tag('jake', 1, 3)
assert.are_same({jake={{1, 4}, {5, 7}}}, tagger.data.tags)
end)
it('`order_tags` should order all tags if called without tag name', function()
tagger.data.tags = {jake={{5, 7}, {3, 4}}, finn={{2, 3}, {1, 2}}}
tagger:order_tags()
assert.are_same(
{jake={{3, 4}, {5, 7}}, finn={{1, 2}, {2, 3}}},
tagger.data.tags
)
end)
it('`order_tags` should only order a single tag if called with a tag name', function()
tagger.data.tags = {jake={{5, 7}, {3, 4}}, finn={{2, 3}, {1, 2}}}
tagger:order_tags('jake')
assert.are_same(
{jake={{3, 4}, {5, 7}}, finn={{2, 3}, {1, 2}}},
tagger.data.tags
)
end)
it('`push_tag` should push a tag instances end position forward', function()
tagger.data.tags = {jake={{1, 5}}}
tagger:push_tag('jake', 1, 5, 6)
assert.are_same({jake={{1, 6}}}, tagger.data.tags)
end)
it('`pull_tag` should pull a tag instances start position backward', function()
tagger.data.tags = {jake={{2, 5}}}
tagger:pull_tag('jake', 2, 5, 1)
assert.are_same({jake={{1, 5}}}, tagger.data.tags)
end)
it('`tag_is_equal` should return true if a tag is equal to time positions', function()
assert.is_true(tagger.tag_is_equal({1, 3}, 1, 3))
end)
it('`tag_is_equal` should return false if a tag is not equal to time positions', function()
assert.is_false(tagger.tag_is_equal({2, 3}, 1, 3))
end)
it('`tag_exists_at` should return true if a tag exists at a time position', function()
assert.is_true(tagger.tag_exists_at({1, 5}, 4))
end)
it('`tag_exists_at` should return false if a tag does not exist at a time position', function()
assert.is_false(tagger.tag_exists_at({2, 5}, 1))
assert.is_false(tagger.tag_exists_at({2, 5}, 6))
end)
it('`get_tags` should return a table of all tags', function()
tagger.state.chosen_tag = ''
tagger.data.tags = {jake={}, finn={}, treehouse={}}
assert.are_same({'finn', 'jake', 'treehouse'}, tagger:get_tags())
end)
it('`get_tags` should return a table of all tags which exist at a time position', function()
tagger.state.chosen_tag = ''
tagger.data.tags = {jake={{6, 9}, {1, 3}}, finn={{3, 4}}, treehouse={{2, 3}}}
assert.are_same({'jake', 'treehouse'}, tagger:get_tags(2))
end)
it('`get_tags` should always include the tag currently being marked', function()
tagger.state.chosen_tag = 'finn'
tagger.state.current_tag.marking = true
tagger.data.tags = {jake={}, treehouse={}}
assert.are_same({'finn', 'jake', 'treehouse'}, tagger:get_tags())
end)
it('`get_tag_times` should return the start and end times of any matching tag instance', function()
tagger.data.tags = {jake={{1, 3}, {5, 7}}, finn={{1, 2}}}
assert.are_same({5, 7}, tagger:get_tag_times('jake', 6))
end)
end)
end)