-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
hexcolor_test.go
55 lines (48 loc) · 1.34 KB
/
hexcolor_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
package colorful
import (
"encoding/json"
"reflect"
"testing"
)
func TestHexColor(t *testing.T) {
for _, tc := range []struct {
hc HexColor
s string
}{
{HexColor{R: 0, G: 0, B: 0}, "#000000"},
{HexColor{R: 1, G: 0, B: 0}, "#ff0000"},
{HexColor{R: 0, G: 1, B: 0}, "#00ff00"},
{HexColor{R: 0, G: 0, B: 1}, "#0000ff"},
{HexColor{R: 1, G: 1, B: 1}, "#ffffff"},
} {
var gotHC HexColor
if err := gotHC.Scan(tc.s); err != nil {
t.Errorf("_.Scan(%q) == %v, want <nil>", tc.s, err)
}
if !reflect.DeepEqual(gotHC, tc.hc) {
t.Errorf("_.Scan(%q) wrote %v, want %v", tc.s, gotHC, tc.hc)
}
if gotValue, err := tc.hc.Value(); err != nil || !reflect.DeepEqual(gotValue, tc.s) {
t.Errorf("%v.Value() == %v, %v, want %v, <nil>", tc.hc, gotValue, err, tc.s)
}
}
}
type CompositeType struct {
Name string `json:"name,omitempty"`
Color HexColor `json:"color,omitempty"`
}
func TestHexColorCompositeJson(t *testing.T) {
var obj = CompositeType{Name: "John", Color: HexColor{R: 1, G: 0, B: 1}}
var jsonData, err = json.Marshal(obj)
if err != nil {
t.Errorf("json.Marshall(obj) wrote %v", err)
}
var obj2 CompositeType
err = json.Unmarshal(jsonData, &obj2)
if err != nil {
t.Errorf("json.Unmarshall(%s) wrote %v", jsonData, err)
}
if !reflect.DeepEqual(obj2, obj) {
t.Errorf("json.Unmarshal(json.Marsrhall(obj)) != obj")
}
}