-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hkshell.py
executable file
·548 lines (430 loc) · 16.6 KB
/
test_hkshell.py
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/python
# This file is part of Heapkeeper.
#
# Heapkeeper is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Heapkeeper is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# Heapkeeper. If not, see <http://www.gnu.org/licenses/>.
# Copyright (C) 2009 Csaba Hoch
# Copyright (C) 2009 Attila Nagy
"""Tests the hkshell module.
Usage:
$ python test_hkshell.py
"""
from __future__ import with_statement
import unittest
import os
import hkutils
import hklib
import test_hklib
import hkshell
import time
class Test__1(unittest.TestCase):
"""Tests that do not require a PostDB."""
def test__listeners(self):
def listener(e):
event_list.append(e.type)
## Testing the `listener` list and the `event` function
event_list = []
hkshell.event(0)
hkshell.listeners.append(listener)
hkshell.event(1)
hkshell.listeners.append(listener)
# The following event will be received by the `listener` function twice
hkshell.event(2)
hkshell.listeners.remove(listener)
hkshell.event(3)
hkshell.listeners.remove(listener)
hkshell.event(4)
self.assertEquals(event_list, [1, 2, 2, 3])
## Testing the `append_listener` and `remove_listener` functions
event_list = []
hkshell.event(0)
hkshell.append_listener(listener)
# a listener cannot be appended if it's in the list
self.assertRaises(
hkutils.HkException,
lambda: hkshell.append_listener(listener))
hkshell.event(1)
hkshell.remove_listener(listener)
# a listener cannot be removed if it's not in the list
self.assertRaises(
hkutils.HkException,
lambda: hkshell.remove_listener(listener))
hkshell.event(2)
self.assertEquals(event_list, [1])
## Testing instance variables of Event
def listener2(e):
event_list.append(e)
event_list = []
hkshell.append_listener(listener2)
hkshell.event(type='mytype')
hkshell.event(type='mytype', command='mycommand')
hkshell.remove_listener(listener2)
self.assertEquals(event_list[0].type, 'mytype')
self.assertEquals(event_list[0].command, None)
self.assertEquals(event_list[1].type, 'mytype')
self.assertEquals(event_list[1].command, 'mycommand')
def test_hkshell_events(self):
hkshell.append_listener(lambda e: event_list.append(e))
## Testing the add_events decorator
# Default name for the `command` attribute
@hkshell.add_events()
def f():
hkshell.event(type='f_body', command='f')
return 1
event_list = []
result = f()
self.assertEquals(result, 1)
self.assertEquals(event_list[0].type, 'before')
self.assertEquals(event_list[0].command, 'f')
self.assertEquals(event_list[1].type, 'f_body')
self.assertEquals(event_list[1].command, 'f')
self.assertEquals(event_list[2].type, 'after')
self.assertEquals(event_list[2].command, 'f')
# Specified name for the `command` attribute
@hkshell.add_events('f_cmd1')
def f():
hkshell.event(type='f_body', command='f_cmd2')
return 1
event_list = []
result = f()
self.assertEquals(result, 1)
self.assertEquals(event_list[0].type, 'before')
self.assertEquals(event_list[0].command, 'f_cmd1')
self.assertEquals(event_list[1].type, 'f_body')
self.assertEquals(event_list[1].command, 'f_cmd2')
self.assertEquals(event_list[2].type, 'after')
self.assertEquals(event_list[2].command, 'f_cmd1')
class Test__2(unittest.TestCase, test_hklib.PostDBHandler):
"""Tests that require a PostDB."""
# Thread structure:
# 0 <- 1 <- 2
# <- 3
# 4
def setUp(self):
self.setUpDirs()
self._postdb = self.createPostDB()
self.create_threadst()
def tearDown(self):
self.tearDownDirs()
def test_ModificationListener(self):
postdb = self._postdb
def my_cmd(fun):
hkshell.event(type='before')
fun()
hkshell.event(type='after')
# Adding the listener
mod_listener = hkshell.ModificationListener(postdb)
self.assertEquals(postdb.listeners, [mod_listener])
hkshell.append_listener(mod_listener)
# Using the listener
self.assert_(mod_listener.touched_posts().is_set([]))
my_cmd(lambda: None)
self.assert_(mod_listener.touched_posts().is_set([]))
my_cmd(lambda: self._posts[0].set_subject("other"))
self.assert_(mod_listener.touched_posts().is_set([self._posts[0]]))
my_cmd(lambda: self._posts[1].set_subject("other"))
my_cmd(lambda: self._posts[2].set_subject("other"))
self.assert_(mod_listener.touched_posts().is_set([self._posts[2]]))
def f():
self._posts[0].set_subject("other2")
self._posts[1].set_subject("other2")
my_cmd(f)
self.assert_(
mod_listener.touched_posts().is_set(
[self._posts[0], self._posts[1]]))
# Removing the listener
hkshell.remove_listener(mod_listener)
mod_listener.close()
self.assertEquals(postdb.listeners, [])
class Test__3(unittest.TestCase, test_hklib.PostDBHandler):
"""Tests that require a hkshell."""
# Thread structure:
# 0 <- 1 <- 2
# <- 3
# 4
def setUp(self):
# Reload is necessary only when the test cases do not clean up after
# themselves (e.g. they do not set the default hkshell values).
# reload(hkshell)
self.setUpDirs()
self._postdb = self.createPostDB()
self.create_threadst()
for post in self._postdb.posts():
post.set_subject('')
hkshell.options.postdb = self._postdb
# Redirect the output of hkshell to nowhere.
class NullOutput():
def write(self, str):
pass
hkshell.options.output = NullOutput()
def tearDown(self):
self.tearDownDirs()
def ae(self, x, y):
self.assertEquals(x, y)
def init_hkshell(self):
hkshell.init()
def my_cmd(self, fun):
hkshell.event(type='before', command='my_cmd')
fun()
hkshell.event(type='after')
def touch_posts_cmd(self, postindices):
hkshell.event(type='before', command='touch_posts_cmd')
for postindex in postindices:
self._posts[postindex].touch()
hkshell.event(type='after')
def _test_gen_indices(self, on, off):
"""
Arguments:
on -- A function that turns on the `gen_indices` feature.
Type: fun()
off -- A function that turns off the `gen_indices` feature.
Type: fun()
"""
call_count = [0]
def gen_indices(postdb):
call_count[0] += 1
self.assertEquals(postdb, self._postdb)
# Initializing hkshell
hkshell.options.callbacks.gen_indices = gen_indices
self.init_hkshell()
# Before turning it on
self.my_cmd(lambda: self._posts[0].touch())
self.assertEquals(call_count, [0])
# Testing
on()
self.my_cmd(lambda: self._posts[0].touch())
self.assertEquals(call_count, [1])
off()
# After turning it off
self.my_cmd(lambda: self._posts[0].touch())
self.assertEquals(call_count, [1])
def test_gen_indices_listener(self):
self._test_gen_indices(
on=lambda: hkshell.append_listener(hkshell.gen_indices_listener),
off=lambda: hkshell.remove_listener(hkshell.gen_indices_listener))
def test_gen_indices__feature(self):
def on_fun():
self.assertEquals(hkshell.features()['gen_indices'], 'off')
hkshell.on('gen_indices')
self.assertEquals(hkshell.features()['gen_indices'], 'on')
def off_fun():
self.assertEquals(hkshell.features()['gen_indices'], 'on')
hkshell.off('gen_indices')
self.assertEquals(hkshell.features()['gen_indices'], 'off')
self._test_gen_indices(on=on_fun, off=off_fun)
def _test_save(self, on, off):
self.init_hkshell()
# Before turning it on
self.my_cmd(lambda: self._posts[0].set_subject('newsubject0'))
self.assertFalse(os.path.exists(self._posts[0].postfilename()))
on()
# Auto-save works
self.my_cmd(lambda: self._posts[0].set_subject('newsubject1'))
post0 = hklib.Post.from_file(self._posts[0].postfilename())
self.assertEquals(post0.subject(), 'newsubject1')
off()
# After turning it off
self.my_cmd(lambda: self._posts[0].set_subject('newsubject2'))
post0 = hklib.Post.from_file(self._posts[0].postfilename())
self.assertEquals(post0.subject(), 'newsubject1')
def test_save_listener(self):
self._test_save(
on=lambda: hkshell.append_listener(hkshell.save_listener),
off=lambda: hkshell.remove_listener(hkshell.save_listener))
def test_save_listener(self):
self._test_save(
on=lambda: hkshell.on('save'),
off=lambda: hkshell.off('save'))
def _test_TouchedPostPrinter(self, on, off):
class MyOutput():
@staticmethod
def write(str):
output_list.append(str)
output = MyOutput()
hkshell.options.output = output
hkshell.touching_commands.append('touch_posts_cmd')
self.init_hkshell()
on()
# No output should be printed
output_list = []
self.my_cmd(lambda: None)
self.assertEquals(
output_list,
[])
# No post touched
output_list = []
self.touch_posts_cmd([])
self.assertEquals(
output_list,
['No post has been touched.\n'])
# One post touched
output_list = []
self.touch_posts_cmd([1])
self.assertEquals(
output_list,
['1 post has been touched:\n',
"['1']\n"])
# More than one posts touched
output_list = []
self.touch_posts_cmd([1, 2])
self.assertEquals(
output_list,
['2 posts have been touched:\n',
"['1', '2']\n"])
off()
def test_TouchedPostPrinterListener(self):
self._test_TouchedPostPrinter(
on=lambda: hkshell.append_listener(
hkshell.touched_post_printer_listener),
off=lambda: hkshell.remove_listener(
hkshell.touched_post_printer_listener))
def test_TouchedPostPrinter__feature(self):
def on_fun():
self.assertEquals(hkshell.features()['touched_post_printer'], 'off')
hkshell.on('touched_post_printer')
self.assertEquals(hkshell.features()['touched_post_printer'], 'on')
def off_fun():
self.assertEquals(hkshell.features()['touched_post_printer'], 'on')
hkshell.off('touched_post_printer')
self.assertEquals(hkshell.features()['touched_post_printer'], 'off')
self._test_TouchedPostPrinter(on=on_fun, off=off_fun)
def test_tagset(self):
def test(pretagset, tagset):
self.assertEquals(hkshell.tagset(pretagset), tagset)
test('t', set(['t']))
test('t1', set(['t1']))
test(['t'], set(['t']))
test(['t1', 't2'], set(['t1', 't2']))
test(set(['t']), set(['t']))
test(set(['t1', 't2']), set(['t1', 't2']))
def f():
hkshell.tagset(0)
self.assertRaises(hkutils.HkException, f)
def subjects(self):
return [ self._posts[i].subject() for i in range(5) ]
def tags(self):
return [ self._posts[i].tags() for i in range(5) ]
def test_self(self):
self.init_hkshell()
self.assertEquals(self.subjects(), ['','','','',''])
self.assertEquals(self.tags(), [[],[],[],[],[]])
def test_ls(self):
"""Tests :func:`hkshell.ls`."""
class MyOutput():
@staticmethod
def write(str):
output_list.append(str)
self.init_hkshell()
output = MyOutput()
hkshell.options.output = output
hklib.localtime_fun = time.gmtime
# ls with default parameters
output_list = []
hkshell.ls()
self.assertEquals(
output_list,
['<0> author0 (2008.08.20. 15:41)\n',
' <1> author1 (2008.08.20. 15:41)\n',
' <2> author2 (2008.08.20. 15:41)\n',
' <3> author3 (2008.08.20. 15:41)\n',
'<4> author4 (2008.08.20. 15:41)\n'])
# ls with given parameters
hkshell.aTr(1,['mytag1','mytag2'])
output_list = []
hkshell.ls(hkshell.ps([1,2,4]), show_author=False, show_tags=True,
show_date=False, indent=4)
self.assertEquals(
output_list,
[' <1> [mytag1,mytag2]\n',
' <2> [mytag1,mytag2]\n',
'<4> []\n'])
def test_enew(self):
self.init_hkshell()
# creating a new post
hkshell.options.callbacks.edit_file = lambda file: True
post = hkshell.enew()
self.assert_(
hkshell.modification_listener.touched_posts().is_set([post]))
# not creating a new post
hkshell.options.callbacks.edit_file = lambda file: False
post = hkshell.enew()
self.assertEquals(post, None)
self.assert_(hkshell.modification_listener.touched_posts().is_set([]))
def test_pT__1(self):
self.init_hkshell()
hkshell.aT(1, 't')
self.assertEquals(self.tags(), [[],['t'],[],[],[]])
hkshell.pT(1)
self.assertEquals(self.tags(), [[],['t'],['t'],[],[]])
def test_pT__2(self):
self.init_hkshell()
hkshell.aT(0, 't1')
hkshell.aT(1, 't2')
hkshell.pT(0)
self.assertEquals(self.tags(), [['t1'],['t1','t2'],['t1'],['t1'],[]])
def test_pT__3(self):
self.init_hkshell()
hkshell.aT(0, 't1')
hkshell.aT(0, 't2')
hkshell.pT(0)
t = ['t1', 't2']
self.assertEquals(self.tags(), [t, t, t, t, []])
def test_aT(self):
self.init_hkshell()
hkshell.aT(1, 't')
self.assertEquals(self.tags(), [[],['t'],[],[],[]])
hkshell.aT(1, ['t', 'u'])
self.assertEquals(self.tags(), [[],['t','u'],[],[],[]])
def test_aTr(self):
self.init_hkshell()
hkshell.aTr(1, 't')
self.assertEquals(self.tags(), [[],['t'],['t'],[],[]])
hkshell.aTr(1, ['t', 'u'])
self.assertEquals(self.tags(), [[],['t','u'],['t','u'],[],[]])
def test_rT_AND_rTr(self):
self.init_hkshell()
hkshell.aTr(0, ['t', 'u'])
self.ae(self.tags(), [['t','u'],['t','u'],['t','u'],['t','u'],[]])
hkshell.rT(1, ['t'])
self.ae(self.tags(), [['t','u'],['u'],['t','u'],['t','u'],[]])
hkshell.rTr(1, ['u'])
self.ae(self.tags(), [['t','u'],[],['t'],['t','u'],[]])
def test_sT_AND_sTr(self):
self.init_hkshell()
hkshell.aTr(0, ['t', 'u'])
self.ae(self.tags(), [['t','u'],['t','u'],['t','u'],['t','u'],[]])
hkshell.sT(1, ['x', 'y'])
self.ae(self.tags(), [['t','u'],['x','y'],['t','u'],['t','u'],[]])
hkshell.sTr(1, 'z')
self.ae(self.tags(), [['t','u'],['z'],['z'],['t','u'],[]])
def test_sS_AND_pS(self):
self.init_hkshell()
hkshell.sS(1, 's')
self.assertEquals(self.subjects(), ['','s','','',''])
hkshell.pS(1)
self.assertEquals(self.subjects(), ['','s','s','',''])
def test_sSr(self):
self.init_hkshell()
hkshell.sSr(1, 's')
self.assertEquals(self.subjects(), ['','s','s','',''])
def test_cS_AND_cSr(self):
self.init_hkshell()
hkshell.sSr(0, 'su')
self.assertEquals(self.subjects(), ['su','su','su','su',''])
hkshell.cS(2)
self.assertEquals(self.subjects(), ['su','su','Su','su',''])
hkshell.cSr(0)
self.assertEquals(self.subjects(), ['Su','Su','Su','Su',''])
if __name__ == '__main__':
hklib.set_log(False)
unittest.main()