-
Notifications
You must be signed in to change notification settings - Fork 24
/
attributes_test.go
123 lines (113 loc) · 3.35 KB
/
attributes_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
package apigee
import (
"encoding/json"
"testing"
"bytes"
)
const (
attrsJson1 = `[ {
"name" : "access",
"value" : "private"
} ]`
attrsJson2 = `[ {
"name" : "access",
"value" : "private"
} , {
"name" : "creator",
"value" : "Brahma"
} ]`
attrsJson3 = `[ {
"name" : "access",
"value" : "private"
}, {
"name" : "creator",
"value" : "Brahma"
}, {
"name" : "lastModified",
"value" : "Wednesday, 7 September 2016, 14:45"
} ]`
)
var (
attrsMap1 = Attributes{"access":"private"}
attrsMap2 = Attributes{"access":"private", "creator":"Brahma"}
attrsMap3 = Attributes{"access":"private", "creator":"Brahma","lastModified":"Wednesday, 7 September 2016, 14:45"}
)
func TestAttributes_Unmarshal(t *testing.T) {
testCases := []struct {
desc string
data string
expected Attributes
expectError bool
equal bool
}{
{"one member ", attrsJson1, attrsMap1, false, true},
{"two members ", attrsJson2, attrsMap2, false, true},
{"three members", attrsJson3, attrsMap3, false, true},
}
for _, tc := range testCases {
var got Attributes
err := json.Unmarshal([]byte(tc.data), &got)
t.Logf("%s: got=%v", tc.desc, got)
if gotErr := err != nil; gotErr != tc.expectError {
t.Errorf("%s: gotErr=%v, expectError=%v, err=%v", tc.desc, gotErr, tc.expectError, err)
continue
}
equal := got.String() == tc.expected.String()
if equal != tc.equal {
t.Errorf("%14s: value[got=%#v, expected=%#v], equal[got=%v, expected=%v]",
tc.desc, got, tc.expected, equal, tc.equal)
}
}
}
func TestAttributes_Marshal(t *testing.T) {
testCases := []struct {
desc string
data Attributes
expected string
expectError bool
equal bool
}{
{"case 1 ", attrsMap1, attrsJson1, false, true},
{"case 2 ", attrsMap2, attrsJson2, false, true},
{"case 3 ", attrsMap3, attrsJson3, false, true},
}
for _, tc := range testCases {
out, err := json.Marshal(tc.data)
if gotErr := (err != nil); gotErr != tc.expectError {
t.Errorf("%s: gotErr=%v, expectError=%v, err=%v", tc.desc, gotErr, tc.expectError, err)
}
got := string(out)
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, []byte(tc.expected)); err != nil {
t.Errorf("%s: %v", tc.desc, err)
}
equal := got == buffer.String()
if equal != tc.equal {
t.Errorf("%s: value[actual=%s, expected=%s], equal[actual=%v, expected=%v]",
tc.desc, got, tc.expected, equal, tc.equal)
}
}
}
// func TestTimstamp_MarshalReflexivity(t *testing.T) {
// testCases := []struct {
// desc string
// data Timestamp
// }{
// {"Reference", Timestamp{referenceTime}},
// {"WorkStart", Timestamp{workStartDate}},
// {"UnixOrigin", Timestamp{unixOriginTime}},
// {"Empty", Timestamp{}}, // degenerate case. I don't really care about this; it will never happen.
// }
// for _, tc := range testCases {
// data, err := json.Marshal(tc.data)
// if err != nil {
// t.Errorf("%s: Marshal err=%v", tc.desc, err)
// }
// var got Timestamp
// err = json.Unmarshal(data, &got)
// t.Logf("%s: %+v ?= %s", tc.desc, got, string(data))
// if got.String() != tc.data.String() {
// t.Errorf("%s: %+v != %+v", tc.desc, got, data)
// }
// }
// }