File: tasklist.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (838 lines) | stat: -rw-r--r-- 20,004 bytes parent folder | download | duplicates (2)
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838

# Copyright 2011-2017 Jaap Karssenberg <jaap.karssenberg@gmail.com>



import tests

from functools import partial

from zim.plugins import PluginManager
from zim.plugins.tasklist import *
from zim.plugins.tasklist.indexer import *
from zim.plugins.tasklist.indexer import _MAX_DUE_DATE, _MIN_START_DATE

from zim.parse.dates import old_parse_date
from zim.parse.tokenlist import testTokenStream
from zim.formats.wiki import Parser as WikiParser

WIKI_TEXT = '''\
Try all kind of combos - see if the parser trips

=== TODO: test heading with label ===

=== Not a task ===

TODO:
[ ] A
[ ] B
[ ] C

[ ] D
[ ] E

FIXME: dus
~~FIXME:~~ foo

[ ] Simple
[ ] List

[ ] List with
	[ ] Nested items
	[*] Some are done
	[*] Done but with open child
		[x] Others not
		[ ] FOOOOO
[ ] Bar

[ ] And then there are @tags
[ ] Waiting: waiting items
[ ] Date [d: 11/12]
[ ] Date [d: 11/12/2012]
	[ ] TODO: BAR !!!
[ ] Date <2012-03-27 >2012-03-01
[ ] Date < wk1213.3
[ ] Date < wk1213.3! with punctuation
[ ] Not a date < wk1213.8
[ ] Not a date < wk1213foooooo

TODO @home:
[ ] Some more tasks !!!
	[ ] Foo !
		* some sub item
		* some other item
	[ ] Bar

TODO: dus
FIXME: jaja - TODO !! @FIXME
~~TODO~~: Ignore this one - it is strike out

* TODO: dus - list item
* FIXME: jaja - TODO !! @FIXME - list item
* ~~TODO~~: Ignore this one - it is strike out - list item

* Bullet list
* With tasks as sub items
	[ ] Sub item bullets
* dus

Some text
	With indenting that looks like list but isn't

1. Numbered list
2. With tasks as sub items
	[ ] Sub item numbered
3. dus

Test task inheritance:

[ ] Main @tag1 @tag2 !
	[*] Sub1
	[ ] Sub2 @tag3 !!!!
		[*] Sub2-1
		[*] Sub2-2 @tag4
		[ ] Sub2-3
	[ ] Sub3

TODO: @someday
[ ] A
[ ] B
	[ ] B-1
[ ] C

TODO @home
[ ] main task
	[x] do this
	[ ] Next: do that
	[ ] Next: do something else

[*] Closed parent task
	[ ] With open child
	[ ] Must be open as well to show up in list

[*] Closed parent task
	[*] With closed children
	[*] Should not

Edge case with wrongly nested list
* Foo
		* bullet 1
		* bullet 2

'''


def t(desc, status=TASK_STATUS_OPEN, waiting=False, start=_MIN_START_DATE, due=_MAX_DUE_DATE, prio=0, tags=''):
	# Generate a task tuple
	# 0:status, 1:prio, 2:waiting, 3:start, 4:due, 5:tags, 6:desc
	if tags:
		tags = set(str(tags).split(','))
	else:
		tags = set()
	return [status, prio, waiting, start, due, tags, str(desc)]


class TestTaskParser(tests.TestCase):

	def assertWikiTextToTasks(self, wikitext, wanted, parser_args={}, parse_args={}):
		tree = WikiParser().parse(wikitext)
		tokens = list(tree.iter_tokens())
		testTokenStream(tokens)

		parser = TaskParser(**parser_args)
		with tests.LoggingFilter('zim.plugins.tasklist', 'Invalid date format'):
			tasks = parser.parse(tokens, **parse_args)

		#~ import pprint; pprint.pprint(tasks)
		self.assertEqual(tasks, wanted)

	def testAllCheckboxes(self):
		mydate = '%04i-%02i-%02i' % old_parse_date('11/12')

		wanted = [
			(t('TODO: test heading with label'), []),
			(t('A'), []),
			(t('B'), []),
			(t('C'), []),
			(t('D'), []),
			(t('E'), []),
			(t('FIXME: dus'), []),
			(t('Simple'), []),
			(t('List'), []),
			(t('List with'), [
				(t('Nested items'), []),
				(t('Some are done', status=TASK_STATUS_CLOSED), []),
				(t('Done but with open child'), [
					(t('Others not', status=TASK_STATUS_CANCELLED), []),
					(t('FOOOOO'), []),
				]),
			]),
			(t('Bar'), []),
			(t('And then there are @tags', tags='tags'), []),
			(t('Waiting: waiting items', waiting=True), []),
			(t('Date [d: 11/12]', due=mydate), []),
			(t('Date [d: 11/12/2012]', due='2012-12-11'), [
				(t('TODO: BAR !!!', prio=3, due='2012-12-11'), []),
				# due date is inherited
			]),
			(t('Date <2012-03-27 >2012-03-01', due='2012-03-27', start='2012-03-01'), []),
			(t('Date < wk1213.3', due='2012-03-28'), []),
			(t('Date < wk1213.3! with punctuation', due='2012-03-28', prio=1), []),
			(t('Not a date < wk1213.8'), []),
			(t('Not a date < wk1213foooooo'), []),

			# this list inherits the @home tag - and inherits prio
			(t('Some more tasks !!!', prio=3, tags='home'), [
				(t('Foo !', prio=1, tags='home'), []),
				(t('Bar', prio=3, tags='home'), []),
			]),
			(t('TODO: dus'), []),
			(t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []),
			(t('TODO: dus - list item'), []),
			(t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []),
			(t('Sub item bullets'), []),
			(t('Sub item numbered'), []),
			(t('Main @tag1 @tag2 !', prio=1, tags='tag1,tag2'), [
				(t('Sub1', prio=1, status=TASK_STATUS_CLOSED, tags='tag1,tag2'), []),
				(t('Sub2 @tag3 !!!!', prio=4, tags='tag1,tag2,tag3'), [
					(t('Sub2-1', prio=4, status=TASK_STATUS_CLOSED, tags='tag1,tag2,tag3'), []),
					(t('Sub2-2 @tag4', prio=4, status=TASK_STATUS_CLOSED, tags='tag1,tag2,tag3,tag4'), []),
					(t('Sub2-3', prio=4, tags='tag1,tag2,tag3'), []),
				]),
				(t('Sub3', prio=1, tags='tag1,tag2'), []),
			]),
			(t('A', tags='someday'), []),
			(t('B', tags='someday'), [
				(t('B-1', tags='someday'), []),
			]),
			(t('C', tags='someday'), []),
			(t('main task', tags='home'), [
				(t('do this', status=TASK_STATUS_CANCELLED, tags='home'), []),
				(t('Next: do that', tags='home'), []),
				(t('Next: do something else', tags='home'), []),
			]),
			(t('Closed parent task'), [
				(t('With open child'), []),
				(t('Must be open as well to show up in list'), []),
			]),
			(t('Closed parent task', status=TASK_STATUS_CLOSED), [
				(t('With closed children', status=TASK_STATUS_CLOSED), []),
				(t('Should not', status=TASK_STATUS_CLOSED), []),
			]),
		]

		self.assertWikiTextToTasks(WIKI_TEXT, wanted)

	def testLabelledCheckboxes(self):
		mydate = '%04i-%02i-%02i' % old_parse_date('11/12')

		wanted = [
			(t('TODO: test heading with label'), []),
			(t('A'), []),
			(t('B'), []),
			(t('C'), []),
			(t('FIXME: dus'), []),

			# this time does not inherit due-date from non-task:
			(t('TODO: BAR !!!', prio=3), []),

			(t('Some more tasks !!!', prio=3, tags='home'), [
				(t('Foo !', prio=1, tags='home'), []),
				(t('Bar', prio=3, tags='home'), []),
			]),
			(t('TODO: dus'), []),
			(t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []),
			(t('TODO: dus - list item'), []),
			(t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []),
			(t('A', tags='someday'), []),
			(t('B', tags='someday'), [
				(t('B-1', tags='someday'), []),
			]),
			(t('C', tags='someday'), []),
			(t('main task', tags='home'), [
				(t('do this', status=TASK_STATUS_CANCELLED, tags='home'), []),
				(t('Next: do that', tags='home'), []),
				(t('Next: do something else', tags='home'), []),
			]),
		]

		self.assertWikiTextToTasks(WIKI_TEXT, wanted, parser_args={'all_checkboxes': False})

	def testDate(self):
		text = '''\
[ ] Task <2018-12
[ ] Task >2018-12
'''
		wanted = [
			(t('Task <2018-12', due='2018-12-31'), []),
			(t('Task >2018-12', start='2018-12-01'), [])
		]

		self.assertWikiTextToTasks(text, wanted)

	def testDueDateInHeading(self):
		text = '''
=== Head {{id: 2021-10-30}} ===
[ ] Task 1

TODO: Task 2
'''
		wanted = [
			(t('Task 1', due='2021-10-30'), []),
			(t('TODO: Task 2', due='2021-10-30'), []),
		]

		daterange = (datetime.date(2021, 10, 25), datetime.date(2021, 10, 31))
		self.assertWikiTextToTasks(text, wanted, parse_args={
			'default_due_date': daterange[1].isoformat(),
			'daterange': daterange
		})

	def testStartDateInHeading(self):
		text = '''
=== Head {{id: 20211030}} ===
[ ] Task 1

TODO: Task 2
'''
		wanted = [
			(t('Task 1', start='2021-10-30'), []),
			(t('TODO: Task 2', start='2021-10-30'), []),
		]

		daterange = (datetime.date(2021, 10, 25), datetime.date(2021, 10, 31))
		self.assertWikiTextToTasks(text, wanted, parse_args={
			'default_start_date': daterange[0].isoformat(),
			'daterange': daterange
		})

	def testDateInHeadingEnforceRange(self):
		text = '''
=== Head {{id: 2021-10-30}} ===
[ ] Task 1
=== Head {{id: 2022-10-30}} ===
[ ] Task 2
=== Head {{id: 2021-10-30}} ===
[ ] Task 3
'''

		wanted = [
			(t('Task 1', start='2021-10-30'), []),
			(t('Task 2', start='2021-10-25'), []),
			(t('Task 3', start='2021-10-30'), []),
		]

		daterange = (datetime.date(2021, 10, 25), datetime.date(2021, 10, 31))
		self.assertWikiTextToTasks(text, wanted, parse_args={
			'default_start_date': daterange[0].isoformat(),
			'daterange': daterange
		})

	def testDateInHeadingMultipleIDs(self):
		text = '''
=== Head {{id: foo}} {{id: 2021-10-30}} {{id bar}} ===
[ ] Task
'''
		wanted = [
			(t('Task', start='2021-10-30'), []),
		]

		daterange = (datetime.date(2021, 10, 25), datetime.date(2021, 10, 31))
		self.assertWikiTextToTasks(text, wanted, parse_args={
			'default_start_date': daterange[0].isoformat(),
			'daterange': daterange
		})

	def testDateInHeadingResetNextHeading(self):
		text = '''
== Top Head ==
=== Head {{id: 2021-10-30}} ===
[ ] Task 1
==== Subhead ====
[ ] Task 2
=== New Head ===
[ ] Task 3
=== New Head {{id: 2021-10-31}} ===
[ ] Task 4
== Top Head ==
[ ] Task 5
'''

		wanted = [
			(t('Task 1', start='2021-10-30'), []),
			(t('Task 2', start='2021-10-30'), []),
			(t('Task 3', start='2021-10-25'), []),
			(t('Task 4', start='2021-10-31'), []),
			(t('Task 5', start='2021-10-25'), []),
		]

		daterange = (datetime.date(2021, 10, 25), datetime.date(2021, 10, 31))
		self.assertWikiTextToTasks(text, wanted, parse_args={
			'default_start_date': daterange[0].isoformat(),
			'daterange': daterange
		})

	def testInheritTags(self):
		text = '''
[ ] Task 1 @work
	[ ] Task 2 @phone
[ ] Task 3 @home
'''

		wanted = [
			(t('Task 1 @work', tags='work'), [
				(t('Task 2 @phone', tags='work,phone'), []),
			]),
			(t('Task 3 @home', tags='home'), []),
		]

		self.assertWikiTextToTasks(text, wanted)

	def testLabelsWithAndWithoutColon(self):
		text = '''
=== TODO heading 1 ===

=== TODO: heading 2 ===

TODO: Task 1
TODO Task 2
'''

		wanted = [
			(t('TODO heading 1'), []),
			(t('TODO: heading 2'), []),
			(t('TODO: Task 1'), []),
			(t('TODO Task 2'), []),
		]

		self.assertWikiTextToTasks(text, wanted)

	def testParseTaskLabels(self):
		from zim.plugins.tasklist.indexer import _parse_task_labels
		self.assertEqual(_parse_task_labels('TODO, FIXME'), ['TODO', 'FIXME'])
		self.assertEqual(_parse_task_labels('TODO,FIXME'), ['TODO', 'FIXME'])
		self.assertEqual(_parse_task_labels('TODO FIXME'), ['TODO', 'FIXME'])

	def testLabelHeadedList(self):
		# with / without colon, with / without empty line
		text = '''
TODO:
[ ] Task 1
[ ] Task 2

TODO
[ ] Task 3
[ ] Task 4


TODO:

[ ] No task 1
[ ] No task 2

TODO: my task
[ ] No task 3
[ ] No task 4
'''

		wanted = [
			(t('Task 1'), []),
			(t('Task 2'), []),
			(t('Task 3'), []),
			(t('Task 4'), []),
			(t('TODO:'), []),
			(t('TODO: my task'), []),
		]

		self.assertWikiTextToTasks(text, wanted, parser_args={'all_checkboxes': False})

	def testLabelHeadedListInheritAll(self):
		# tags, start / due date, priority all same as a parent task, vary colon placement
		text = '''
TODO @home:
[ ] Task 1
[ ] Task 2

TODO !!!
[ ] Task 3
[ ] Task 4

TODO: <2021-11-01 >2021-10-01
[ ] Task 5
[ ] Task 6
'''

		wanted = [
			(t('Task 1', tags='home'), []),
			(t('Task 2', tags='home'), []),
			(t('Task 3', prio=3), []),
			(t('Task 4', prio=3), []),
			(t('Task 5', start='2021-10-01', due='2021-11-01'), []),
			(t('Task 6', start='2021-10-01', due='2021-11-01'), []),
		]

		self.assertWikiTextToTasks(text, wanted, parser_args={'all_checkboxes': False})


class TestTaskList(tests.TestCase):

	def testIndexing(self):
		'''Check indexing of tasklist plugin'''
		plugin = PluginManager.load_plugin('tasklist')
		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)

		# Test indexing based on index signals
		notebook.index.check_and_update()

		for klass in (AllTasks, ActiveTasks, NextActionTasks, InboxTasks, OpenProjectsTasks): # WaitingTasks
			view = klass.new_from_index(notebook.index)
			tasks = list(view)
			self.assertGreater(len(tasks), 3, klass.__name__)
			for task in tasks:
				path = view.get_path(task)
				self.assertTrue(not path is None)

	def testTaskListTreeView(self):
		plugin = PluginManager.load_plugin('tasklist')

		notebook = self.setUpNotebook(content=tests.FULL_NOTEBOOK)
		notebook.index.check_and_update()

		from zim.plugins.tasklist.gui import TaskListTreeView
		view = AllTasks.new_from_index(notebook.index)
		opener = tests.MockObject()
		treeview = TaskListTreeView(view, opener, task_labels=['TODO', 'FIXME'])

		menu = treeview.get_popup()

		# Check these do not cause errors - how to verify state ?
		tests.gtk_activate_menu_item(menu, _("Expand _All"))
		tests.gtk_activate_menu_item(menu, _("_Collapse All"))

		# Copy tasklist -> csv
		from zim.gui.clipboard import Clipboard
		tests.gtk_activate_menu_item(menu, _('_Copy'))
		text = Clipboard.get_text()
		lines = text.splitlines()
		self.assertTrue(len(lines) > 10)
		self.assertTrue(len(lines[0].split(',')) > 3)
		self.assertFalse(any('<span' in l for l in lines)) # make sure encoding is removed

		# Test tags
		#tags = treeview.get_tags()
		#for tag in ('home', 'FIXME', '__no_tags__', 'tags'):
		#	self.assertIn(tag, tags)
		#	self.assertGreater(tags[tag], 0)

		# TODO test filtering for tags, labels, string - all case insensitive


	#~ def testDialog(self):
		#~ '''Check tasklist plugin dialog'''
		#
		# TODO


class TestIndexViewMixin(object):

	def setUpClass():
		PluginManager.load_plugin('tasklist')

	def setUp(self):
		self.notebook = self.setUpNotebook(content={'Test': self.input})
		self.selection = self.klass.new_from_index(self.notebook.index)

	def testListTasks(self):
		def walk(parent):
			tasks = []
			for row in self.selection.list_tasks(parent=parent):
				task = t(row['description'], row['status'], bool(row['waiting']), row['start'], row['due'], row['prio'], row['tags'])
				if row['haschildren']:
					children = walk(row)
					tasks.append((task, children))
				else:
					tasks.append((task, []))

			return tasks

		tasks = walk(None)
		self.assertEqual(tasks, self.tasks)


class TestAllTasks(TestIndexViewMixin, tests.TestCase):

	klass = AllTasks

	input = '''
	[ ] Foo
	[*] Bar
	[ ] Parent 1
		[ ] Child 1.1
	[ ] Parent 2
		[x] Child 2.1
	[ ] Parent with prio !!!
		[*] Child with prio !!!
	[ ] Later >3000-01-01
	[ ] Started >2000-01-01
	[ ] Waiting: foo
	[ ] Prio !!!
	[ ] With due date <3000-01-01
	'''

	tasks = [
		(t('Parent with prio !!!', prio=3), []),
		(t('Prio !!!', prio=3), []),
		(t('With due date <3000-01-01', due='3000-01-01'), []),
		(t('Foo'), []),
		(t('Parent 1'), [
			(t('Child 1.1'), [])
		]),
		(t('Parent 2'), []),
		(t('Started >2000-01-01', start='2000-01-01'), []),
		(t('Waiting: foo', waiting=True), []),
		(t('Later >3000-01-01', start='3000-01-01'), []),
	]


class TestActiveTasks(TestIndexViewMixin, tests.TestCase):

	klass = ActiveTasks

	input = '''
	[ ] Foo
	[*] Bar
	[ ] Parent 1
		[ ] Child 1.1
	[ ] Parent 2
		[x] Child 2.1
	[ ] Later >3000-01-01
	[ ] Started >2000-01-01
	[ ] Waiting: foo
	'''

	tasks = [
		(t('Foo'), []),
		(t('Child 1.1'), []),
		(t('Parent 2'), []),
		(t('Started >2000-01-01', start='2000-01-01'), []),
	]


class TestNextActionTasks(TestIndexViewMixin, tests.TestCase):

	klass = NextActionTasks

	input = '''
	[ ] Foo
	[*] Bar
	[ ] Parent 1
		[ ] Child 1.1
	[ ] Parent 2
		[x] Child 2.1
	[ ] Parent with prio !!!
		[*] Child with prio !!!
	[ ] Later >3000-01-01
	[ ] Started >2000-01-01
	[ ] Waiting: foo
	[ ] Prio !!!
	[ ] With due date <3000-01-01
	'''

	tasks = [
		(t('Prio !!!', prio=3), []),
		(t('With due date <3000-01-01', due='3000-01-01'), []),
		(t('Child 1.1'), []),
	]


class TestInboxTasks(TestIndexViewMixin, tests.TestCase):

	klass = InboxTasks

	input = '''
	[ ] Foo
	[*] Bar
	[ ] Parent 1
		[ ] Child 1.1
	[ ] Parent 2
		[x] Child 2.1
	[ ] Parent with prio !!!
		[*] Child with prio !!!
	[ ] Later >3000-01-01
	[ ] Started >2000-01-01
	[ ] Waiting: foo
	[ ] Prio !!!
	[ ] With due date <3000-01-01
	'''

	tasks = [
		(t('Foo'), []),
		(t('Started >2000-01-01', start='2000-01-01'), []),
	]


class TestOpenProjectsTasks(TestIndexViewMixin, tests.TestCase):

	klass = OpenProjectsTasks

	input = '''
	[ ] Foo
	[*] Bar
	[ ] Parent 1
		[ ] Child 1.1
	[ ] Parent 2
		[x] Child 2.1
	[ ] Parent with prio !!!
		[*] Child with prio !!!
	[ ] Later >3000-01-01
	[ ] Started >2000-01-01
	[ ] Waiting: foo
	[ ] Prio !!!
	[ ] With due date <3000-01-01
	'''

	tasks = [
		(t('Parent with prio !!!', prio=3), []),
		(t('Parent 1'), [
			(t('Child 1.1'), [])
		]),
		(t('Parent 2'), []),
	]


class TestWaitingTasks(TestIndexViewMixin, tests.TestCase):

	klass = WaitingTasks

	input = '''
	[ ] Foo
	[ ] Waiting: Bar
	'''

	tasks = [
		(t('Waiting: Bar', waiting=True), [])
	]


class TestSelection(tests.TestCase):

	def runTest(self):
		PluginManager.load_plugin('tasklist')
		notebook = self.setUpNotebook(content={
			'PageA': '''
			[ ] Foo
			[ ] Bar @bar

			TODO: test
			TODO: test @bar
			''',
			'PageB': '''
			[ ] Foo
			[ ] Bar @bar
			[ ] Test @foo @bar
			''',
			'PageB:Child': '''
			[ ] Foo
			[ ] Bar @bar

			FIXME: dus
			FIXME: dus @foo
			'''
		})
		alltasks = AllTasks.new_from_index(notebook.index)
		count = partial(alltasks.count_labels_and_tags_pages, task_labels=('TODO', 'FIXME'))

		self.assertEqual(count(), (
			{'TODO': 2, 'FIXME': 2},
			{'__no_tags__': 5, 'bar': 5, 'foo': 2},
			{'PageA': 4, 'PageB': 7, 'Child': 4} # "PageB" includes child pages
		))
		self.assertEqual(count(intersect=((), ('bar',))), (
			{'TODO': 1},
			{'__no_tags__': 0, 'bar': 5, 'foo': 1},
			{'PageA': 2, 'PageB': 3, 'Child': 1}
		))
		self.assertEqual(count(intersect=((), ('foo',))), (
			{'FIXME': 1},
			{'__no_tags__': 0, 'bar': 1, 'foo': 2},
			{'PageB': 2, 'Child': 1}
		))
		self.assertEqual(count(intersect=((), ('foo', 'bar'))), ( # "Tag AND tag"
			{},
			{'__no_tags__': 0, 'bar': 1, 'foo': 1},
			{'PageB': 1}
		))
		self.assertEqual(count(intersect=(('TODO',), ())), (
			{'TODO': 2},
			{'__no_tags__': 1, 'bar': 1},
			{'PageA': 2}
		))
		self.assertEqual(count(intersect=(('TODO',), ('bar',))), ( # "Label AND tag"
			{'TODO': 1},
			{'__no_tags__': 0, 'bar': 1},
			{'PageA': 1}
		))
		self.assertEqual(count(intersect=(('TODO', 'FIXME'), ())), ( # "Label OR label"
			{'TODO': 2, 'FIXME': 2},
			{'__no_tags__': 2, 'bar': 1, 'foo': 1},
			{'PageA': 2, 'PageB': 2, 'Child': 2}
		))
		self.assertEqual(count(intersect=(('TODO', 'FIXME'), ('bar',))), ( # "(Label OR label) AND tag"
			{'TODO': 1},
			{'__no_tags__': 0, 'bar': 1},
			{'PageA': 1}
		))
		self.assertEqual(count(intersect=(('TODO', 'FIXME'), ('bar', 'foo'))), ( # "(Label OR label) AND (tag AND tag)"
			{},
			{'__no_tags__': 0},
			{}
		))


class TestUI(tests.TestCase):

	def testDaysToString(self):
		from zim.plugins.tasklist.gui import days_to_str

		# Without use_workweek
		for days, wanted in (
			(600, '2y'),
			(300, '1y'),
			(270, '9m'),
			(200, '7m'),
			(30, '1m'),
			(21, '3w'),
			(14, '2w'),
			(10, '10d'),
			(7, '7d'),
			(2, '2d'),
		):
			self.assertEqual(days_to_str(days, False, 1), wanted)

		# With use_workweek
		for days, weekday, wanted in (
			# all offsets for monday
			(14, 1, '2w'), # due: mon in two weeks
			(13, 1, '9d'), # due: next sun == fri
			(12, 1, '9d'), # due: next sat == fri
			(11, 1, '9d'), # due: next fri
			(10, 1, '8d'), # due: next thu
			(9, 1, '7d'), # due: next wed
			(8, 1, '6d'), # due: next tue
			(7, 1, '5d'), # due: next mon
			(6, 1, '4d'), # due: sun == fri
			(5, 1, '4d'), # due: sat == fri
			(4, 1, '4d'), # due: fri
			(3, 1, '3d'), # due: thu
			(2, 1, '2d'), # due: wed
			(1, 1, '1d'), # due: tue
			# different weekdays
			(4, 1, '4d'), # mon: tue, wed, thu, fri
			(4, 2, '3d'), # tue: wed, thu, fri, (sat)
			(4, 3, '2d'), # wed: thu, fri, (sat, sun)
			(4, 4, '2d'), # thu: fri (sat, sun), mon
			(4, 5, '2d'), # fri: (sat, sun) mon, thu
			(4, 6, '3d'), # sat: (sun), mon, tue, wed
			(4, 7, '4d'), # sun: mon, tue, wed, thu
		):
			self.assertEqual(days_to_str(days, True, weekday), wanted)