forked from CyCoreSystems/ari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime_test.go
144 lines (121 loc) · 3.22 KB
/
datetime_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
package ari
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
)
// test data
var dtMarshalTests = []struct {
Input dtTest
Output string
HasError bool
}{
{dtTest{DateTime(time.Date(2005, 02, 04, 13, 12, 6, 0, time.UTC))}, `{"dt":"2005-02-04T13:12:06.000+0000"}`, false},
}
var dtUnmarshalTests = []struct {
Input string
Output dtTest
HasError bool
}{
{`{"dt":"2005-02-04T13:12:06.000+0000"}`, dtTest{DateTime(time.Date(2005, 02, 04, 13, 12, 6, 0, time.UTC))}, false},
{`{"dt":"2x05-02-04T13:12:06.000+0000"}`, dtTest{}, true},
{`{"dt": 0 }`, dtTest{}, true},
}
var dsUnmarshalTests = []struct {
Input string
Output dsTest
HasError bool
}{
{`{"ds":4}`, dsTest{DurationSec(4 * time.Second)}, false},
{`{"ds":40}`, dsTest{DurationSec(40 * time.Second)}, false},
{`{"ds":"4"}`, dsTest{}, true},
{`{"ds":""}`, dsTest{}, true},
{`{"ds":"xzsad"}`, dsTest{}, true},
}
var dsMarshalTests = []struct {
Input dsTest
Output string
HasError bool
}{
{dsTest{DurationSec(4 * time.Second)}, `{"ds":4}`, false},
{dsTest{DurationSec(40 * time.Second)}, `{"ds":40}`, false},
}
// test runners
func TestDateTimeMarshal(t *testing.T) {
for _, tx := range dtMarshalTests {
ret := runTestMarshal(tx.Input, tx.Output, tx.HasError)
if ret != "" {
t.Errorf(ret)
}
}
}
func TestDateTimeUnmarshal(t *testing.T) {
for _, tx := range dtUnmarshalTests {
var out dtTest
ret := runTestUnmarshal(&out, tx.Input, &tx.Output, tx.HasError)
if ret != "" {
t.Errorf(ret)
}
}
}
func TestDurationSecsMarshal(t *testing.T) {
for _, tx := range dsMarshalTests {
ret := runTestMarshal(tx.Input, tx.Output, tx.HasError)
if ret != "" {
t.Errorf(ret)
}
}
}
func TestDurationSecsUnmarshal(t *testing.T) {
for _, tx := range dsUnmarshalTests {
var out dsTest
ret := runTestUnmarshal(&out, tx.Input, &tx.Output, tx.HasError)
if ret != "" {
t.Errorf(ret)
}
}
}
// generalized test functions
func runTestMarshal(input interface{}, output string, hasError bool) (ret string) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(input)
out := strings.TrimSpace(buf.String())
failed := false
failed = failed || (err == nil && hasError)
failed = failed || (out != output)
if failed {
ret = fmt.Sprintf("Marshal(%s) => '%s', 'err != nil => %v'; expected '%s', 'err != nil => %v'.", input, out, err != nil, output, hasError)
}
return
}
func runTestUnmarshal(out eq, input string, output eq, hasError bool) (ret string) {
err := json.NewDecoder(strings.NewReader(input)).Decode(&out)
failed := false
failed = failed || (err == nil && hasError)
failed = failed || (!out.Equal(output))
if failed {
ret = fmt.Sprintf("Unmarshal(%s) => '%s', 'err != nil => %v'; expected '%s', 'err != nil => %v'.", input, out, err != nil, output, hasError)
}
return
}
// test structures
type dtTest struct {
Date DateTime `json:"dt"`
}
func (dt *dtTest) Equal(i interface{}) bool {
o, ok := i.(*dtTest)
return ok && time.Time(dt.Date).Equal(time.Time(o.Date))
}
type eq interface {
Equal(i interface{}) bool
}
type dsTest struct {
Duration DurationSec `json:"ds"`
}
func (ds *dsTest) Equal(i interface{}) bool {
o, ok := i.(*dsTest)
return ok && time.Duration(ds.Duration) == time.Duration(o.Duration)
}