-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjson_test.go
102 lines (76 loc) · 2.12 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
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
package fmtdate
import (
// "fmt"
"testing"
// "time"
)
func mustTimeDate(format, date string) TimeDate {
td, err := NewTimeDate(format, date)
if err != nil {
panic(err.Error())
}
return td
}
func TestMashalError(t *testing.T) {
td, err := NewTimeDate("hh:mm:ss ZZZZ", "16:05,06 +0070")
if err == nil {
t.Errorf("expected error while parsing the date, but got: %s", td.String())
}
}
func TestMashal(t *testing.T) {
tests := []struct {
format string
date string
expected string
}{
{"hh:mm:ss ZZZZ", "16:05:06 +0000", "16:05:06 +0000"},
{"hh:mm:ss ZZZZ", "", "null"},
{"", "2006-01-02 15:04:05", "2006-01-02 15:04:05"},
}
for _, test := range tests {
var td TimeDate
var err error
if test.date != "" {
td, err = NewTimeDate(test.format, test.date)
if err != nil {
t.Fatalf("NewTimeDate(%#v, %#v); returned error: %v", test.format, test.date, err)
}
}
b, err := td.MarshalJSON()
if err != nil {
t.Fatalf("(%#v,%#v).MarshalJSON(); returned error: %v", test.format, test.date, err)
}
if got, want := string(b), test.expected; got != want {
t.Errorf("(%#v,%#v).MarshalJSON() = %#v; want %#v", test.format, test.date, got, want)
}
}
}
func TestUnMashal(t *testing.T) {
tests := []struct {
format string
date string
expected string
}{
{"hh:mm:ss ZZZZ", "16:05:06 +0000", "16:05:06 +0000"},
{"hh:mm:ss ZZZZ", "null", "null"},
{"", "2006-01-02 15:04:05", "2006-01-02 15:04:05"},
}
for _, test := range tests {
var td TimeDate
td.Format = test.format
err := td.UnmarshalJSON([]byte(test.date))
if err != nil {
t.Fatalf("(%#v,%#v).UnmarshalJSON(); returned error: %v", test.format, test.date, err)
}
if td.IsNil() {
if got, want := "null", test.expected; got != want {
t.Errorf("(%#v,%#v).UnmarshalJSON() = %#v; want %#v", test.format, test.date, got, want)
}
// t.Fatalf("(%#v,%#v).UnmarshalJSON(); returned nil", test.format, test.date)
} else {
if got, want := Format(test.format, *td.Time), test.expected; got != want {
t.Errorf("(%#v,%#v).UnmarshalJSON() = %#v; want %#v", test.format, test.date, got, want)
}
}
}
}