-
Notifications
You must be signed in to change notification settings - Fork 8
/
mapslice_test.go
52 lines (42 loc) · 1 KB
/
mapslice_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
package mapslice
import (
"encoding/json"
"fmt"
"testing"
)
func TestMarshal(t *testing.T) {
ms := MapSlice{
MapItem{Key: "abc", Value: 123},
MapItem{Key: "def", Value: 456},
MapItem{Key: "ghi", Value: 789},
}
b, err := json.Marshal(ms)
if err != nil {
t.Fatal(err)
}
e := "{\"abc\":123,\"def\":456,\"ghi\":789}"
r := string(b)
if r != e {
t.Errorf("expected: %s\ngot: %s", e, r)
}
}
func TestMarshalError(t *testing.T) {
ms := MapSlice{
MapItem{Key: "abc", Value: make(chan int)},
}
e := "json: error calling MarshalJSON for type mapslice.MapSlice: json: unsupported type: chan int"
if _, err := json.Marshal(ms); err != nil && e != err.Error() {
t.Errorf("expected: %s\ngot: %v", e, err)
}
}
func TestUnmarshal(t *testing.T) {
ms := MapSlice{}
if err := json.Unmarshal([]byte("{\"abc\":123,\"def\":456,\"ghi\":789}"), &ms); err != nil {
t.Fatal(err)
}
e := "[{abc 123} {def 456} {ghi 789}]"
r := fmt.Sprintf("%v", ms)
if r != e {
t.Errorf("expected: %s\ngot: %s", e, r)
}
}