forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcoding_test.go
131 lines (115 loc) · 2.79 KB
/
transcoding_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
package gocb
import (
"testing"
)
var defaultTranscoder DefaultTranscoder
var (
jsonObjStr = []byte("{\"test\":\"value\"}")
jsonNumStr = []byte("2222")
jsonStrStr = []byte("Hello World")
)
func testBytesEqual(t *testing.T, left, right []byte) {
if len(left) != len(right) {
t.Errorf("Slice lengths do not match")
return
}
for i, _ := range left {
if left[i] != right[i] {
t.Errorf("Slice values do not match")
return
}
}
}
func testDecode(t *testing.T, bytes []byte, flags uint32, out interface{}) {
err := defaultTranscoder.Decode(bytes, flags, out)
if err != nil {
t.Errorf("Failed to decode %v", err)
}
}
func testEncode(t *testing.T, in interface{}) ([]byte, uint32) {
bytes, flags, err := defaultTranscoder.Encode(in)
if err != nil {
t.Errorf("Failed to decode %v", err)
}
return bytes, flags
}
func TestDecodeLegacyJson(t *testing.T) {
var testOut map[string]string
testDecode(t, jsonObjStr, 0, &testOut)
if testOut["test"] != "value" {
t.Errorf("Decoding failed")
}
}
func TestDecodeJson(t *testing.T) {
var testOut map[string]string
testDecode(t, jsonObjStr, 0x2000000, &testOut)
if testOut["test"] != "value" {
t.Errorf("Decoding failed")
}
}
func TestDecodeJsonStruct(t *testing.T) {
var testOut struct {
Test string `json:"test"`
}
testDecode(t, jsonObjStr, 0x2000000, &testOut)
if testOut.Test != "value" {
t.Errorf("Decoding failed")
}
}
func TestDecodeNumber(t *testing.T) {
var testOut int
testDecode(t, jsonNumStr, 0x2000000, &testOut)
if testOut != 2222 {
t.Errorf("Decoding failed")
}
}
func TestDecodeString(t *testing.T) {
var testOut string
testDecode(t, jsonStrStr, 0x4000000, &testOut)
if testOut != "Hello World" {
t.Errorf("Decoding failed")
}
}
func TestDecodeBadType(t *testing.T) {
var testOut string
err := defaultTranscoder.Decode(jsonNumStr, 0x2000000, &testOut)
if err == nil {
t.Errorf("Decoding succeeded but should have failed")
}
}
func TestEncodeJson(t *testing.T) {
testIn := make(map[string]string)
testIn["test"] = "value"
bytes, flags := testEncode(t, &testIn)
if flags != 0x2000000 {
t.Errorf("Bad flags generated")
}
testBytesEqual(t, bytes, jsonObjStr)
}
func TestEncodeJsonStruct(t *testing.T) {
var testIn struct {
Test string `json:"test"`
}
testIn.Test = "value"
bytes, flags := testEncode(t, &testIn)
if flags != 0x2000000 {
t.Errorf("Bad flags generated")
}
testBytesEqual(t, bytes, jsonObjStr)
}
func TestEncodeNumber(t *testing.T) {
testIn := 2222
bytes, flags := testEncode(t, &testIn)
if flags != 0x2000000 {
t.Errorf("Bad flags generated")
}
testBytesEqual(t, bytes, jsonNumStr)
}
func TestEncodeString(t *testing.T) {
testIn := "Hello World"
bytes, flags := testEncode(t, &testIn)
if flags != 0x4000000 {
t.Errorf("Bad flags generated")
}
testBytesEqual(t, bytes, jsonStrStr)
}