-
Notifications
You must be signed in to change notification settings - Fork 0
/
varobject_test.go
93 lines (71 loc) · 1.6 KB
/
varobject_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
package schemer
import (
"bytes"
"fmt"
"testing"
)
func TestDecodeVarObject1(t *testing.T) {
m := map[int]string{1: "b"}
// setup an example schema
s, err := SchemaOf(m)
if err != nil {
t.Fatal(err)
}
varObjectSchema, ok := s.(*VarObjectSchema)
if !ok {
t.Fatal("varObjectSchema assertion failed")
return
}
// encode it
b, err := varObjectSchema.MarshalSchemer()
if err != nil {
t.Fatal(err)
}
// make sure we can successfully decode it
tmp, err := DecodeSchema(bytes.NewReader(b))
if err != nil {
t.Fatal("cannot encode binary encoded VarObjectSchema", err)
}
decodedIntSchema := tmp.(*VarObjectSchema)
// and then check the actual contents of the decoded schema
// to make sure it contains the correct values
if decodedIntSchema.Nullable() != varObjectSchema.Nullable() {
t.Fatal("unexpected values when decoding binary EnumSchema")
}
}
func TestDecodeVarObject2(t *testing.T) {
strToIntMap := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
var buf bytes.Buffer
var err error
buf.Reset()
varObjectSchema, err := SchemaOf(&strToIntMap)
if err != nil {
t.Error(err)
return
}
err = varObjectSchema.Encode(&buf, strToIntMap)
if err != nil {
t.Error(err)
return
}
fmt.Println("map to map")
r := bytes.NewReader(buf.Bytes())
// pass in a nil map
// to make sure decode will allocate it for us
var mapToDecode map[string]int
err = varObjectSchema.Decode(r, &mapToDecode)
if err != nil {
t.Error(err)
return
}
for key, element := range strToIntMap {
if element != mapToDecode[key] {
t.Error("encoded data not present in decoded map")
}
}
}