todo_lib 11.0.0

Collection of utilities for todo.txt format
Documentation
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
use chrono::{Days, NaiveDate};
use todo_lib::todo::{Action, Conf, DateTagChange, NewDateValue, done, edit};
use todo_lib::todotxt::{
    CompletionConfig, CompletionDateMode, CompletionMode, Task, business_days_between, format_date,
};

#[test]
fn parse_tasks_simple() {
    struct Test {
        i: &'static str,
        t: Task,
    }
    let data: Vec<Test> = vec![
        Test { i: "just text", t: Task { subject: "just text".to_string(), ..Default::default() } },
        Test { i: "x just text", t: Task { subject: "just text".to_string(), finished: true, ..Default::default() } },
        Test { i: "X just text", t: Task { subject: "X just text".to_string(), ..Default::default() } },
        Test { i: "xjust text", t: Task { subject: "xjust text".to_string(), ..Default::default() } },
        Test { i: "(a) just text", t: Task { subject: "(a) just text".to_string(), ..Default::default() } },
        Test { i: "(B) just text", t: Task { subject: "just text".to_string(), priority: 1, ..Default::default() } },
        Test { i: "(B)just text", t: Task { subject: "(B)just text".to_string(), ..Default::default() } },
        Test {
            i: "2020-01-03 just text",
            t: Task {
                subject: "just text".to_string(),
                create_date: Some(NaiveDate::from_ymd_opt(2020, 01, 03).unwrap()),
                ..Default::default()
            },
        },
        Test {
            i: "2020-02-03 2020-01-03 just text",
            t: Task {
                subject: "2020-01-03 just text".to_string(),
                create_date: Some(NaiveDate::from_ymd_opt(2020, 02, 03).unwrap()),
                ..Default::default()
            },
        },
        Test {
            i: "x 2020-02-03 2020-01-03 just text",
            t: Task {
                subject: "just text".to_string(),
                create_date: Some(NaiveDate::from_ymd_opt(2020, 01, 03).unwrap()),
                finish_date: Some(NaiveDate::from_ymd_opt(2020, 02, 03).unwrap()),
                finished: true,
                ..Default::default()
            },
        },
        Test {
            i: "x (E) 2020-02-03 2020-01-03 just text",
            t: Task {
                subject: "just text".to_string(),
                priority: 4,
                create_date: Some(NaiveDate::from_ymd_opt(2020, 01, 03).unwrap()),
                finish_date: Some(NaiveDate::from_ymd_opt(2020, 02, 03).unwrap()),
                finished: true,
                ..Default::default()
            },
        },
        Test {
            i: "x (j) 2020-02-03 2020-01-03 just text",
            t: Task {
                subject: "(j) 2020-02-03 2020-01-03 just text".to_string(),
                finished: true,
                ..Default::default()
            },
        },
        Test {
            i: "2020-31-03 just text",
            t: Task { subject: "2020-31-03 just text".to_string(), ..Default::default() },
        },
        Test {
            i: "2020-01-43 just text",
            t: Task { subject: "2020-01-43 just text".to_string(), ..Default::default() },
        },
        Test {
            i: "2020-01-03a just text",
            t: Task { subject: "2020-01-03a just text".to_string(), ..Default::default() },
        },
    ];
    let base = NaiveDate::from_ymd_opt(2020, 3, 15).unwrap();
    for d in data.iter() {
        let t = Task::parse(d.i, base);
        assert_eq!(d.t, t, "{}", d.i);
        let back = format!("{}", t);
        assert_eq!(d.i, &back, "{}", d.i);
    }
}

#[test]
fn parse_tasks_tags() {
    struct Test {
        i: &'static str,
        o: &'static str,
        hk: Vec<&'static str>,
        hv: Vec<&'static str>,
    }
    let data: Vec<Test> = vec![
        Test { i: "task rec: due:2d", o: "task rec: due:2020-03-17", hk: vec!["due"], hv: vec!["2020-03-17"] },
        Test {
            i: "task rec:2w due:2d",
            o: "task rec:2w due:2020-03-17",
            hk: vec!["rec", "due"],
            hv: vec!["2w", "2020-03-17"],
        },
        Test {
            i: "due:2d task rec:2w due:2d",
            o: "due:2020-03-17 task rec:2w due:2020-03-17",
            hk: vec!["rec", "due"],
            hv: vec!["2w", "2020-03-17"],
        },
        Test {
            i: "task rec:2w due:2d t:1m",
            o: "task rec:2w due:2020-03-17 t:2020-04-15",
            hk: vec!["rec", "due", "t"],
            hv: vec!["2w", "2020-03-17", "2020-04-15"],
        },
        Test { i: "task rec:2w due:230 14:20 end", o: "=", hk: vec!["rec", "due", "14"], hv: vec!["2w", "230", "20"] },
        Test {
            i: "task rec:2w https://www.example.com 14:20 end https:/some",
            o: "=",
            hk: vec!["rec", "14", "https"],
            hv: vec!["2w", "20", "/some"],
        },
    ];
    let base = NaiveDate::from_ymd_opt(2020, 3, 15).unwrap();
    for d in data.iter() {
        let t = Task::parse(d.i, base);
        if t.tags.is_empty() {
            assert!(d.hk.is_empty(), "{} has no tags", d.i);
        } else {
            assert!(d.hk.len() == t.tags.len(), "{}", d.i);
        }
        assert_eq!(t.tags.len(), d.hk.len());
        for (k, v) in d.hk.iter().zip(d.hv.iter()) {
            let val = t.tags.get(*k);
            assert!(val.is_some(), "{} - {}", d.i, k);
            assert_eq!(v, val.unwrap(), "{} - {}", d.i, k);
        }

        let back = format!("{}", t);
        if d.o == "=" {
            assert_eq!(d.i, &back, "{}", d.i);
        } else if !d.o.is_empty() {
            assert_eq!(d.o, &back, "{}", d.i);
        }
    }
}

#[test]
#[allow(deprecated)]
fn complete_old_signature() {
    struct Test {
        i: &'static str,
        d: &'static str,
        u: &'static str,
        m: CompletionMode,
    }
    let data: Vec<Test> = vec![
        Test { i: "test", d: "x test", u: "test", m: CompletionMode::JustMark },
        Test {
            i: "2020-01-01 test",
            d: "x 2020-02-02 2020-01-01 test",
            u: "2020-01-01 test",
            m: CompletionMode::JustMark,
        },
        Test {
            i: "test rec:+1m due:2020-03-01",
            d: "x test rec:+1m due:2020-03-01",
            u: "test rec:+1m due:2020-03-01",
            m: CompletionMode::JustMark,
        },
        Test {
            i: "test rec:1m due:2020-03-01",
            d: "x test rec:1m due:2020-03-01",
            u: "test rec:1m due:2020-03-01",
            m: CompletionMode::JustMark,
        },
        Test {
            i: "2020-01-01 test rec:7d",
            d: "x 2020-02-02 2020-01-01 test rec:7d",
            u: "2020-01-01 test rec:7d",
            m: CompletionMode::JustMark,
        },
        Test { i: "(B) testb", d: "x (B) testb", u: "(B) testb", m: CompletionMode::JustMark },
        Test { i: "(B) testb", d: "x (B) testb", u: "(B) testb", m: CompletionMode::MovePriority },
        Test { i: "(B) testb", d: "x testb pri:B", u: "(B) testb", m: CompletionMode::PriorityToTag },
        Test { i: "(B) testb", d: "x testb", u: "testb", m: CompletionMode::RemovePriority },
        Test {
            i: "(B) 2020-01-01 testc",
            d: "x 2020-02-02 2020-01-01 (B) testc",
            u: "(B) 2020-01-01 testc",
            m: CompletionMode::MovePriority,
        },
        Test {
            i: "(B) 2020-01-01 testc",
            d: "x 2020-02-02 2020-01-01 testc pri:B",
            u: "(B) 2020-01-01 testc",
            m: CompletionMode::PriorityToTag,
        },
        Test {
            i: "(B) 2020-01-01 testc",
            d: "x 2020-02-02 2020-01-01 testc",
            u: "2020-01-01 testc",
            m: CompletionMode::RemovePriority,
        },
    ];
    let base = NaiveDate::from_ymd_opt(2020, 2, 2).unwrap();
    for d in data.iter() {
        let mut t = Task::parse(d.i, base);
        t.complete(base, d.m);
        assert_eq!(d.d, &format!("{}", t), "done '{}', mode: {:?}", d.i, d.m);
        if t.create_date.is_some() && t.recurrence.is_none() {
            assert_eq!(t.finish_date, Some(base));
        }
        if d.m != CompletionMode::RemovePriority {
            t.uncomplete(d.m);
            assert_eq!(d.u, &format!("{}", t), "undone '{}', mode: {:?}", d.i, d.m);
        }
    }
}

#[test]
fn complete_uncomplete() {
    struct Test {
        i: &'static str,
        d: &'static str,
        u: &'static str,
        m: CompletionMode,
        cdm: CompletionDateMode,
    }
    let data: Vec<Test> = vec![
        Test {
            i: "test",
            d: "x test",
            u: "test",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "test",
            d: "x 2020-02-02 test",
            u: "test",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::AlwaysSet,
        },
        Test {
            i: "2020-01-01 test",
            d: "x 2020-02-02 2020-01-01 test",
            u: "2020-01-01 test",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "test rec:+1m due:2020-03-01",
            d: "x test rec:+1m due:2020-03-01",
            u: "test rec:+1m due:2020-03-01",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "test rec:1m due:2020-03-01",
            d: "x test rec:1m due:2020-03-01",
            u: "test rec:1m due:2020-03-01",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "test rec:1m due:2020-03-01",
            d: "x test rec:1m due:2020-03-01",
            u: "test rec:1m due:2020-03-01",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "2020-01-01 test rec:7d",
            d: "x 2020-02-02 2020-01-01 test rec:7d",
            u: "2020-01-01 test rec:7d",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) testb",
            d: "x (B) testb",
            u: "(B) testb",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) testb",
            d: "x (B) 2020-02-02 testb",
            u: "(B) testb",
            m: CompletionMode::JustMark,
            cdm: CompletionDateMode::AlwaysSet,
        },
        Test {
            i: "(B) testb",
            d: "x (B) testb",
            u: "(B) testb",
            m: CompletionMode::MovePriority,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) testb",
            d: "x 2020-02-02 (B) testb",
            u: "(B) testb",
            m: CompletionMode::MovePriority,
            cdm: CompletionDateMode::AlwaysSet,
        },
        Test {
            i: "(B) testb",
            d: "x testb pri:B",
            u: "(B) testb",
            m: CompletionMode::PriorityToTag,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) testb",
            d: "x testb",
            u: "testb",
            m: CompletionMode::RemovePriority,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) testb",
            d: "x 2020-02-02 testb",
            u: "testb",
            m: CompletionMode::RemovePriority,
            cdm: CompletionDateMode::AlwaysSet,
        },
        Test {
            i: "(B) 2020-01-01 testc",
            d: "x 2020-02-02 2020-01-01 (B) testc",
            u: "(B) 2020-01-01 testc",
            m: CompletionMode::MovePriority,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) 2020-01-01 testc",
            d: "x 2020-02-02 2020-01-01 testc pri:B",
            u: "(B) 2020-01-01 testc",
            m: CompletionMode::PriorityToTag,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
        Test {
            i: "(B) 2020-01-01 testc",
            d: "x 2020-02-02 2020-01-01 testc",
            u: "2020-01-01 testc",
            m: CompletionMode::RemovePriority,
            cdm: CompletionDateMode::WhenCreationDateIsPresent,
        },
    ];
    let base = NaiveDate::from_ymd_opt(2020, 2, 2).unwrap();
    for d in data.iter() {
        let mut t = Task::parse(d.i, base);
        t.complete_with_config(base, CompletionConfig { completion_mode: d.m, completion_date_mode: d.cdm });
        assert_eq!(d.d, &format!("{}", t), "done '{}', mode: {:?}", d.i, d.m);
        if t.create_date.is_some() && t.recurrence.is_none() {
            assert_eq!(t.finish_date, Some(base));
        }
        if d.m != CompletionMode::RemovePriority {
            t.uncomplete(d.m);
            assert_eq!(d.u, &format!("{}", t), "undone '{}', mode: {:?}", d.i, d.m);
        }
    }
}

#[test]
fn complete_cleanup_recurrent_test() {
    struct Test {
        i: &'static str,
        n: &'static str,
        m: CompletionMode,
    }
    let data: Vec<Test> = vec![
        Test { i: "test rec:1d due:2020-02-01 tmr:off one", n: "test rec:1d one", m: CompletionMode::JustMark },
        Test { i: "test rec:1d due:2020-02-01 two spent:23", n: "test rec:1d two", m: CompletionMode::JustMark },
        Test {
            i: "test rec:1d due:2020-02-01 spent:23 three tmr:on four",
            n: "test rec:1d three four",
            m: CompletionMode::JustMark,
        },
    ];

    let base = NaiveDate::from_ymd_opt(2020, 2, 2).unwrap();
    for d in data.iter() {
        let t = Task::parse(d.i, base);
        let mut tasks: Vec<Task> = vec![t];
        let completion_config =
            CompletionConfig { completion_mode: d.m, completion_date_mode: CompletionDateMode::AlwaysSet };
        let changed = done(&mut tasks, None, completion_config);

        assert_eq!(changed.len(), 1, "Expected 1 changed tasks, got {0}", changed.len());
        println!("{:?}", tasks);
        assert_eq!(tasks.len(), 2, "Expected new task is created");
        let mut new_cleaned = tasks[1].clone();
        new_cleaned.update_tag("due:");
        assert_eq!(
            d.n,
            &format!("{}", new_cleaned),
            "Invalid new task [{0}], expected [{1}]",
            &format!("{}", new_cleaned),
            d.n
        );
    }
}

#[test]
fn complete_cleanup_recurrent_until_test() {
    struct Test {
        i: String,
        o: String,
    }
    let mut data: Vec<Test> = Vec::new();
    let base = chrono::Local::now().date_naive();

    data.push(Test {
        i: format!(
            "rec:+1w task due:{0} until:{1}",
            format_date(base.checked_add_days(Days::new(2)).unwrap()),
            format_date(base.checked_add_days(Days::new(15)).unwrap()),
        ),
        o: format!(
            "rec:+1w task due:{0} until:{1}",
            format_date(base.checked_add_days(Days::new(9)).unwrap()),
            format_date(base.checked_add_days(Days::new(15)).unwrap()),
        ),
    });
    data.push(Test {
        i: format!(
            "rec:+1w task due:{0} until:{1}",
            format_date(base.checked_add_days(Days::new(2)).unwrap()),
            format_date(base.checked_add_days(Days::new(5)).unwrap()),
        ),
        o: String::new(),
    });

    for d in data.iter() {
        let t = Task::parse(&d.i, base);
        let mut tasks: Vec<Task> = vec![t];
        let completion_config = CompletionConfig {
            completion_mode: CompletionMode::JustMark,
            completion_date_mode: CompletionDateMode::AlwaysSet,
        };
        let changed = done(&mut tasks, None, completion_config);

        assert_eq!(changed.len(), 1, "Expected 1 changed tasks, got {0}", changed.len());
        println!("{:?}", tasks);
        if d.o.is_empty() {
            assert_eq!(tasks.len(), 1, "Expected new task is created for {0}", d.i);
            assert!(tasks[0].finished, "Expected the task is done: {0}", tasks[0]);
        } else {
            assert_eq!(tasks.len(), 2, "Expected new task is created for {0}", d.i);
            assert_eq!(
                &d.o,
                &format!("{}", tasks[1]),
                "Invalid new task [{0}], expected [{1}]",
                &format!("{}", tasks[1]),
                d.o
            );
        }
    }
}

#[test]
fn business_days_between_test() {
    struct Test {
        s: NaiveDate,
        e: NaiveDate,
        d: i64,
    }
    let data: Vec<Test> = vec![
        Test {
            s: NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            e: NaiveDate::from_ymd_opt(2024, 2, 16).unwrap(),
            d: 0,
        },
        Test {
            s: NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            e: NaiveDate::from_ymd_opt(2024, 2, 21).unwrap(),
            d: 2,
        },
        Test {
            s: NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            e: NaiveDate::from_ymd_opt(2024, 2, 24).unwrap(),
            d: 4,
        },
        Test {
            s: NaiveDate::from_ymd_opt(2024, 2, 24).unwrap(),
            e: NaiveDate::from_ymd_opt(2024, 2, 24).unwrap(),
            d: 2,
        },
    ];
    for d in data.iter() {
        let r = business_days_between(d.s, d.e);
        assert_eq!(d.d, r, "done {} --> {}", d.s, d.e);
    }
}

#[test]
fn next_date() {
    struct Test {
        i: &'static str,
        d: &'static str,
    }
    let data: Vec<Test> = vec![
        Test { i: "2020-01-01 test", d: "2020-01-01 test" },
        Test { i: "test t:2020-03-02 rec:+1m due:2020-03-01", d: "test t:2020-04-02 rec:+1m due:2020-04-01" },
        Test { i: "test t:2020-02-29 rec:1m due:2020-03-01", d: "test t:2020-03-05 rec:1m due:2020-03-05" },
        Test { i: "2020-01-01 test rec:7d", d: "2020-01-01 test rec:7d" },
        Test { i: "2020-01-01 test due:2020-01-01", d: "2020-01-01 test due:2020-01-01" },
        Test { i: "test rec:7d due:2020-02-01", d: "test rec:7d due:2020-02-12" },
        Test { i: "test rec:1b due:2020-02-01", d: "test rec:1b due:2020-02-06" },
        Test { i: "test rec:7b due:2020-02-01", d: "test rec:7b due:2020-02-14" },
        Test { i: "test rec:14b due:2020-02-01", d: "test rec:14b due:2020-02-25" },
        Test { i: "test rec:+14b due:2020-02-01", d: "test rec:+14b due:2020-02-20" },
        Test { i: "test rec:tue,sat due:2020-02-01", d: "test rec:tue,sat due:2020-02-08" },
        Test { i: "test rec:wed,sat due:2020-02-01", d: "test rec:wed,sat due:2020-02-05" },
        Test { i: "test rec:mon,wed due:2020-02-01", d: "test rec:mon,wed due:2020-02-05" },
        Test { i: "test rec:sat due:2020-02-08", d: "test rec:sat due:2020-02-15" },
        Test { i: "test rec:mon due:2020-02-01", d: "test rec:mon due:2020-02-10" },
    ];
    // 2020-02-01 - Saturday
    // 2020-02-05 - Wednesday
    let base = NaiveDate::from_ymd_opt(2020, 2, 5).unwrap();
    for d in data.iter() {
        let mut t = Task::parse(d.i, base);
        let orig_due = t.due_date.clone();
        let orig_thr = t.threshold_date.clone();
        t.next_dates(base);
        assert_eq!(d.d, &format!("{}", t), "done {}", d.i);
        if orig_due.is_some() && t.recurrence.is_some() && t.due_date.is_some() {
            let orig = orig_due.unwrap();
            assert!(orig < t.due_date.unwrap(), "due must change: {}", d.i);
        }
        if orig_thr.is_some() && t.recurrence.is_some() && t.threshold_date.is_some() {
            let orig = orig_thr.unwrap();
            assert!(orig < t.threshold_date.unwrap(), "threshold must change: {}", d.i);
        }
    }
}

#[test]
fn replace_projects() {
    struct Test {
        i: &'static str,
        o: &'static str,
        r: &'static str,
        w: &'static str,
    }
    let data: Vec<Test> = vec![
        Test { i: "str abc", o: "str abc", r: "+tag", w: "+tg" },
        Test { i: "str +tag1 abc", o: "str +tag1 abc", r: "+tag", w: "+tg" },
        Test { i: "str some+tag abc", o: "str some+tag abc", r: "+tag", w: "+tg" },
        Test { i: "+tag str abc +tag1", o: "+newtag str abc +tag1", r: "+tag", w: "+newtag" },
        Test { i: "+tag str abc +tag1", o: "str abc +tag1", r: "+tag", w: "" },
        Test { i: "+tag str abc +tag1", o: "+tag str abc", r: "+tag1", w: "" },
        Test { i: "+tag str +abc +tag1", o: "+tag str +tag1", r: "abc", w: "" },
        Test { i: "efg ++tag str abc +tag", o: "efg ++tag str abc +newstr", r: "+tag", w: "+newstr" },
    ];

    let dt = NaiveDate::from_ymd_opt(2021, 01, 05).unwrap();
    for d in data.iter() {
        let mut t = Task::parse(d.i, dt);
        t.replace_project(d.r, d.w);
        assert_eq!(d.o, &t.subject, "{}: {} -> {}", d.i, d.r, d.w);
    }
}

#[test]
fn replace_contexts() {
    struct Test {
        i: &'static str,
        o: &'static str,
        r: &'static str,
        w: &'static str,
    }
    let data: Vec<Test> = vec![
        Test { i: "str abc", o: "str abc", r: "@tag", w: "@tg" },
        Test { i: "str @tag1 abc", o: "str @tag1 abc", r: "@tag", w: "@tg" },
        Test { i: "str some@tag abc", o: "str some@tag abc", r: "@tag", w: "@tg" },
        Test { i: "@tag str abc @tag1", o: "@newtag str abc @tag1", r: "@tag", w: "@newtag" },
        Test { i: "@tag str abc @tag1", o: "str abc @tag1", r: "@tag", w: "" },
        Test { i: "@tag str abc @tag1", o: "@tag str abc", r: "@tag1", w: "" },
        Test { i: "@tag str @abc @tag1", o: "@tag str @tag1", r: "abc", w: "" },
        Test { i: "efg @@tag str abc @tag", o: "efg @@tag str abc @newstr", r: "@tag", w: "@newstr" },
    ];

    let dt = NaiveDate::from_ymd_opt(2021, 01, 05).unwrap();
    for d in data.iter() {
        let mut t = Task::parse(d.i, dt);
        t.replace_context(d.r, d.w);
        assert_eq!(d.o, &t.subject, "{}: {} -> {}", d.i, d.r, d.w);
    }
}

#[test]
fn replace_recurrences() {
    struct Test {
        i: &'static str,
        o: &'static str,
        w: &'static str,
    }
    let data: Vec<Test> = vec![
        Test { i: "str abc", o: "str abc rec:12", w: "12" },
        Test { i: "str somrec:45 abc", o: "str somrec:45 abc rec:12", w: "12" },
        Test { i: "rec:", o: "rec: rec:345", w: "345" },
        Test { i: "abc rec: def", o: "abc rec: def rec:345", w: "345" },
        Test { i: "rec:11", o: "rec:345", w: "345" },
        Test { i: "str rec:22", o: "str rec:345", w: "345" },
        Test { i: "str rec:222 end", o: "str rec:345 end", w: "345" },
        Test { i: "rec:11 text rec:22", o: "rec:11 text rec:345", w: "345" },
        Test { i: "rec:22 text rec:22", o: "rec:345 text rec:345", w: "345" },
        Test { i: "rrec:11 text rec:22", o: "rrec:11 text rec:345", w: "345" },
    ];

    let dt = NaiveDate::from_ymd_opt(2021, 01, 05).unwrap();
    for d in data.iter() {
        let mut t = Task::parse(d.i, dt);
        t.update_tag_with_value("rec", d.w);
        assert_eq!(d.o, &t.subject, "{}-> {}", d.i, d.w);
    }
}

#[test]
fn finish_date_with_create_date() {
    let task = Task {
        finished: true,
        create_date: Some(NaiveDate::default()),
        finish_date: NaiveDate::default().succ_opt(),
        subject: "Feed cat".to_owned(),
        ..Default::default()
    };
    assert_eq!(task.to_string(), "x 1970-01-02 1970-01-01 Feed cat")
}

#[test]
fn finish_date_without_create_date() {
    let task = Task {
        finished: true,
        finish_date: NaiveDate::default().succ_opt(),
        subject: "Feed cat".to_owned(),
        ..Default::default()
    };
    assert_eq!(task.to_string(), "x 1970-01-02 Feed cat")
}

#[test]
fn due_expr_test() {
    struct Test {
        i: &'static str,
        e: &'static str,
        d: &'static str,
    }
    let data: Vec<Test> = vec![
        Test { i: "feed cat thr:2020-10-10", e: "thr+1d", d: "feed cat thr:2020-10-10 due:2020-10-11" },
        Test { i: "feed cat due:2020-10-09 thr:2020-10-10", e: "thr+1d", d: "feed cat due:2020-10-11 thr:2020-10-10" },
        Test { i: "feed cat due:2020-10-09 thr:2020-10-10", e: "due+1d", d: "feed cat due:2020-10-10 thr:2020-10-10" },
        Test {
            i: "feed cat due:2020-10-09 thr:2020-10-10",
            e: "due+1m-1d",
            d: "feed cat due:2020-11-08 thr:2020-10-10",
        },
    ];
    let base = NaiveDate::from_ymd_opt(2020, 10, 12).unwrap();
    for d in data.iter() {
        let t = Task::parse(d.i, base);
        let mut tasks = vec![t];
        let mut c = Conf::default();
        c.due = DateTagChange { action: Action::Set, value: NewDateValue::Expr(d.e.to_string()) };
        let changed = edit(&mut tasks, None, &c);
        assert_eq!(changed.len(), 1, "The subject must change");
        assert!(changed[0], "The subject of the first task must change");
        assert_eq!(tasks[0].subject, d.d, "New value must be {0}, got {1}", d.d, tasks[0].subject);
    }
}