@@ -8,26 +8,50 @@ import (
88
99func TestExpand (t * testing.T ) {
1010 tripStart := time .Date (2016 , 07 , 15 , 00 , 00 , 00 , 00 , time .UTC )
11- cutoff := time .Date (2016 , 07 , 10 , 00 , 00 , 00 , 00 , time .UTC )
11+ tripEnd := time .Date (2016 , 07 , 20 , 12 , 30 , 00 , 00 , time .UTC )
12+ stdCutoff := time .Date (2016 , 07 , 10 , 00 , 00 , 00 , 00 , time .UTC )
1213
1314 cases := []struct {
14- in []ChecklistItem
15- want []Task
15+ in []ChecklistItem
16+ cutoff time.Time
17+ want []Task
1618 }{{
1719 in : []ChecklistItem {},
1820 want : []Task {},
1921 }, {
20- in : []ChecklistItem {{Template : "foo" , Indent : 1 , Days : - 14 }},
22+ in : []ChecklistItem {{Template : "foo" , Indent : 1 , Due : "14 days before start" }},
23+ cutoff : stdCutoff ,
2124 want : []Task {{
2225 Content : "foo" ,
2326 Indent : 1 ,
2427 DueDate : time .Date (2016 , 07 , 01 , 20 , 00 , 00 , 00 , time .UTC ),
2528 }},
2629 }, {
2730 in : []ChecklistItem {
28- {Template : "before cutoff" , Indent : 1 , Days : - 8 },
29- {Template : "after cutoff" , Indent : 1 , Days : - 4 },
31+ {Template : "no day adjustment" , Indent : 1 , Due : "1 hour before start" },
32+ {Template : "end" , Indent : 1 , Due : "2 days before end" },
33+ {Template : "after" , Indent : 1 , Due : "4 hours after start" },
3034 },
35+ cutoff : tripEnd ,
36+ want : []Task {
37+ {Content : "no day adjustment" ,
38+ Indent : 1 ,
39+ DueDate : time .Date (2016 , 07 , 14 , 23 , 00 , 00 , 00 , time .UTC )},
40+ {Content : "end" ,
41+ Indent : 1 ,
42+ Position : 1 ,
43+ DueDate : time .Date (2016 , 07 , 18 , 20 , 00 , 00 , 00 , time .UTC )},
44+ {Content : "after" ,
45+ Indent : 1 ,
46+ Position : 2 ,
47+ DueDate : time .Date (2016 , 07 , 15 , 04 , 00 , 00 , 00 , time .UTC )},
48+ },
49+ }, {
50+ in : []ChecklistItem {
51+ {Template : "before cutoff" , Indent : 1 , Due : "8 days before start" },
52+ {Template : "after cutoff" , Indent : 1 , Due : "4 days before start" },
53+ },
54+ cutoff : stdCutoff ,
3155 want : []Task {{
3256 Content : "before cutoff" ,
3357 Indent : 1 ,
@@ -36,9 +60,58 @@ func TestExpand(t *testing.T) {
3660 }}
3761
3862 for _ , c := range cases {
39- got := Expand (c .in , tripStart , cutoff )
63+ got := Expand (c .in , tripStart , tripEnd , c . cutoff )
4064 if ! reflect .DeepEqual (got , c .want ) {
4165 t .Errorf ("Expand(%v) == %v, want %v" , c .in , got , c .want )
4266 }
4367 }
4468}
69+
70+ func TestParseDue (t * testing.T ) {
71+ cases := []struct {
72+ in string
73+ want due
74+ wantError bool
75+ }{{
76+ in : "1 hour from start" ,
77+ want : due {duration : - time .Hour },
78+ }, {
79+ in : "3 hours after end" ,
80+ want : due {duration : 3 * time .Hour , end : true },
81+ }, {
82+ in : "1 day before end" ,
83+ want : due {duration : - 24 * time .Hour , end : true },
84+ }, {
85+ in : "2 weeks before start" ,
86+ want : due {duration : - 2 * 7 * 24 * time .Hour },
87+ }, {
88+ in : "" ,
89+ wantError : true ,
90+ }, {
91+ in : "A days before start" ,
92+ wantError : true ,
93+ }, {
94+ in : "1 month before start" , // month not supported.
95+ wantError : true ,
96+ }, {
97+ in : "1 day prior to start" , // prior to not supported.
98+ wantError : true ,
99+ }, {
100+ in : "1 day before commencement" ,
101+ wantError : true ,
102+ }}
103+
104+ for _ , c := range cases {
105+ got , err := parseDue (c .in )
106+ if err != nil {
107+ if ! c .wantError {
108+ t .Errorf ("parseDue(%q) error %v want no error" , c .in , err )
109+ }
110+ continue
111+ }
112+ if ! reflect .DeepEqual (got , c .want ) {
113+ t .Errorf ("parseDue(%q) == %v want %v" , c .in , got , c .want )
114+ }
115+ }
116+
117+ }
0 commit comments