-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkwhen_test.go
203 lines (196 loc) · 4.08 KB
/
markwhen_test.go
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
package markwhen
import (
"encoding/json"
"reflect"
"strings"
"testing"
"time"
)
func marshal(mw *MarkWhen) string {
b, _ := json.Marshal(mw)
return string(b)
}
func mustParseTime(t string) time.Time {
result, err := time.Parse(time.RFC3339, t)
if err != nil {
panic(err)
}
return result
}
func TestParse(t *testing.T) {
tests := []struct {
name string
input string
want *MarkWhen
wantErr bool
}{
{
"parses title",
"title: This is a title",
&MarkWhen{
[]*Page{
{
&Header{Title: "This is a title", DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{},
},
},
make(Tags),
},
false,
},
{
"parses description",
"description: This is a description",
&MarkWhen{
[]*Page{
{
&Header{Description: "This is a description", DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{},
},
},
make(Tags),
},
false,
},
{
"EU Date example",
`// To indicate we are using European date formatting
dateFormat: d/M/y
// 2 weeks
01/01/2023 - 14/01/2023: Phase 1 #Exploratory
// Another 2 weeks
15/01/2023 - 31/01/2023: Phase 2 #Implementation
// 3 days, after a one week buffer
07/03/2023 - 10/03/2023: Phase 4 - kickoff! #Launch
`,
&MarkWhen{
[]*Page{
{
&Header{
DateFormat: euDateFormat,
Tags: make(Tags),
},
[]*Collection{
{
Type: CollectionFree,
Events: []*Event{
{From: mustParseTime("2023-01-01T00:00:00Z"), To: mustParseTime("2023-01-14T00:00:00Z"), Body: "Phase 1 #Exploratory"},
{From: mustParseTime("2023-01-15T00:00:00Z"), To: mustParseTime("2023-01-31T00:00:00Z"), Body: "Phase 2 #Implementation"},
{From: mustParseTime("2023-03-07T00:00:00Z"), To: mustParseTime("2023-03-10T00:00:00Z"), Body: "Phase 4 - kickoff! #Launch"},
},
},
},
},
},
make(Tags),
},
false,
},
{
"works with multiple pages",
`title: This is a title for page 1
_-_-_break_-_-_
title: This is a title for page 2`,
&MarkWhen{
[]*Page{
{
&Header{Title: "This is a title for page 1", DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{},
},
{
&Header{Title: "This is a title for page 2", DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{},
},
},
make(Tags),
},
false,
},
{
"handles groups",
`group potato
endGroup`,
&MarkWhen{
[]*Page{
{
&Header{DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{NewCollection(CollectionGroup)},
},
},
make(Tags),
},
false,
},
{
"parses tags",
`#potato: #cecece`,
&MarkWhen{
[]*Page{
{
&Header{DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{},
},
},
Tags{"potato": "#cecece"},
},
false,
},
{
"parses tags with comments",
`#Travel: blue
#Education: green
#Economics: #abc // hex color`,
&MarkWhen{
[]*Page{
{
&Header{DateFormat: DefaultDateFormat, Tags: make(Tags)},
[]*Collection{},
},
},
Tags{
"Travel": "blue",
"Education": "green",
"Economics": "#abc",
},
},
false,
},
{
"EDTF Date example",
"2023-01-01 / 2023-14-01: Phase 1 #Exploratory",
&MarkWhen{
[]*Page{
{
&Header{
Tags: make(Tags),
DateFormat: DefaultDateFormat,
},
[]*Collection{
{
Type: CollectionFree,
Events: []*Event{
{From: mustParseTime("2023-01-01T00:00:00Z"), To: mustParseTime("2023-01-14T00:00:00Z"), Body: "Phase 1 #Exploratory"},
},
},
},
},
},
make(Tags),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := strings.NewReader(tt.input)
got, err := Parse(r)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Parse() = \n%+v\n, want \n%+v\n", marshal(got), marshal(tt.want))
}
})
}
}