-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshalling_test.go
93 lines (70 loc) · 2.01 KB
/
marshalling_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 jsonwrapper
import (
"bytes"
"fmt"
"testing"
)
func TestToJsonBytes(t *testing.T) {
data := []string{"test", "data"}
expectedJson := `{"code":200,"status":"success","data":["test","data"]}`
response := Success(data)
asJson, err := ToJsonBytes(&response)
if err != nil {
t.Error("Expected error to be nil, was", err)
}
if bytes.Equal(asJson, []byte(expectedJson)) != true {
t.Error("Expected byte format did not match")
}
}
func TestToJsonString(t *testing.T) {
data := []string{"test", "data"}
expectedJson := `{"code":200,"status":"success","data":["test","data"]}`
response := Success(data)
asJson, err := ToJsonString(&response)
if err != nil {
t.Error("Expected error to be nil, was", err)
}
if asJson != expectedJson {
t.Error("Expected string format did not match")
}
}
func TestFromBytes(t *testing.T) {
data := []string{"test", "data"}
jsonData := `{"code":200,"status":"success","data":["test","data"]}`
response, err := FromBytes([]byte(jsonData))
if err != nil {
t.Error("Expected error to be nil, was", err)
}
if response.Code != 200 {
t.Error("Expected code to be 200")
}
if response.Status != StatusSuccess {
t.Error("Expected status to be success")
}
if response.Message != "" {
t.Error("Expected message to be empty")
}
if fmt.Sprint(response.Data) != fmt.Sprint(data) {
t.Error("Expected the data to match our definitions, was", response.Data, data)
}
}
func TestString(t *testing.T) {
data := []string{"test", "data"}
jsonData := `{"code":200,"status":"success","data":["test","data"]}`
response, err := FromString(jsonData)
if err != nil {
t.Error("Expected error to be nil, was", err)
}
if response.Code != 200 {
t.Error("Expected code to be 200")
}
if response.Status != StatusSuccess {
t.Error("Expected status to be success")
}
if response.Message != "" {
t.Error("Expected message to be empty")
}
if fmt.Sprint(response.Data) != fmt.Sprint(data) {
t.Error("Expected the data to match our definitions, was", response.Data, data)
}
}