forked from teambition/rrule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
58 lines (49 loc) · 2.28 KB
/
json_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
package rrule
import (
"encoding/json"
"testing"
)
func TestRRule_MarshalJSON(t *testing.T) {
str := "FREQ=WEEKLY;DTSTART=20120201T093000Z;INTERVAL=5;WKST=TU;COUNT=2;UNTIL=20130130T230000Z;BYSETPOS=2;BYMONTH=3;BYYEARDAY=95;BYWEEKNO=1;BYDAY=MO,+2FR;BYHOUR=9;BYMINUTE=30;BYSECOND=0;BYEASTER=-1"
r, _ := StrToRRule(str)
toEnc := struct{ Rule *RRule }{r}
b, _ := json.Marshal(toEnc)
expected := `{"Rule":"` + str + `"}`
if string(b) != expected {
t.Errorf("json.Marshal(StrToRRule(%q)) = %q, want %q", str, string(b), expected)
}
}
func TestRRule_UnmarshalJSON(t *testing.T) {
str := "FREQ=WEEKLY;DTSTART=20120201T093000Z;INTERVAL=5;WKST=TU;COUNT=2;UNTIL=20130130T230000Z;BYSETPOS=2;BYMONTH=3;BYYEARDAY=95;BYWEEKNO=1;BYDAY=MO,+2FR;BYHOUR=9;BYMINUTE=30;BYSECOND=0;BYEASTER=-1"
j := []byte(`{"Rule":"` + str + `"}`)
var r struct{ Rule *RRule }
json.Unmarshal(j, &r)
if s := r.Rule.String(); s != str {
t.Errorf("json.Unmarshal(%q).String() = %q, want %q", string(j), s, str)
}
}
func TestSet_MarshalJSON(t *testing.T) {
str := `RRULE:FREQ=WEEKLY;DTSTART=20120201T093000Z;COUNT=10;BYDAY=MO,TU,WE,TH,FR
RDATE:20121201T093000Z
EXRULE:FREQ=WEEKLY;DTSTART=20120208T093000Z;COUNT=3;BYDAY=MO,TU,WE,TH,FR
EXDATE:20120203T093000Z`
s, _ := StrToRRuleSet(str)
toEnc := struct{ RuleSet *Set }{s}
b, _ := json.Marshal(toEnc)
expected := `{"RuleSet":["RRULE:FREQ=WEEKLY;DTSTART=20120201T093000Z;COUNT=10;BYDAY=MO,TU,WE,TH,FR","RDATE:20121201T093000Z","EXRULE:FREQ=WEEKLY;DTSTART=20120208T093000Z;COUNT=3;BYDAY=MO,TU,WE,TH,FR","EXDATE:20120203T093000Z"]}`
if string(b) != expected {
t.Errorf("json.Marshal(StrToRRule(%q)) = %q, want %q", str, string(b), expected)
}
}
func TestSet_UnmarshalJSON(t *testing.T) {
str := `RRULE:FREQ=WEEKLY;DTSTART=20120201T093000Z;COUNT=10;BYDAY=MO,TU,WE,TH,FR
RDATE:20121201T093000Z
EXRULE:FREQ=WEEKLY;DTSTART=20120208T093000Z;COUNT=3;BYDAY=MO,TU,WE,TH,FR
EXDATE:20120203T093000Z`
j := []byte(`{"RuleSet":["RRULE:FREQ=WEEKLY;DTSTART=20120201T093000Z;COUNT=10;BYDAY=MO,TU,WE,TH,FR","RDATE:20121201T093000Z","EXRULE:FREQ=WEEKLY;DTSTART=20120208T093000Z;COUNT=3;BYDAY=MO,TU,WE,TH,FR","EXDATE:20120203T093000Z"]}`)
var r struct{ RuleSet *Set }
json.Unmarshal(j, &r)
if s := r.RuleSet.String(); s != str {
t.Errorf("json.Unmarshal(%q).String() = %q, want %q", string(j), s, str)
}
}