-
Notifications
You must be signed in to change notification settings - Fork 1
/
raw_message_test.go
133 lines (113 loc) · 3.11 KB
/
raw_message_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
132
133
package traversal_test
import (
"encoding/json"
"fmt"
"testing"
tr "github.com/dougfort/traversal"
"github.com/pkg/errors"
)
func TestGetStringFromRawMessage(t *testing.T) {
var err error
const testString = "test string"
testJSON := fmt.Sprintf("\"%s\"", testString)
var rawMessage json.RawMessage
err = rawMessage.UnmarshalJSON([]byte(testJSON))
if err != nil {
t.Fatalf("rawMessage.UnmarshalJSON() failed: %s", err)
}
result, err := tr.GetStringFromRawMessage(rawMessage)
if err != nil {
t.Fatalf("GetStringFromRawMessage(%s) failed: %s", rawMessage, err)
}
if result != testString {
t.Fatalf("expected '%s', got '%s", testString, result)
}
}
func TestGetBoolFromRawMessage(t *testing.T) {
var err error
testJSON := "true"
var rawMessage json.RawMessage
err = rawMessage.UnmarshalJSON([]byte(testJSON))
if err != nil {
t.Fatalf("rawMessage.UnmarshalJSON() failed: %s", err)
}
result, err := tr.GetBoolFromRawMessage(rawMessage)
if err != nil {
t.Fatalf("GetBoolFromRawMessage(%s) failed: %s", rawMessage, err)
}
if !result {
t.Fatalf("expected 'true', got %t", result)
}
}
func TestGetInt32FromRawMessage(t *testing.T) {
var err error
var testNumber int32 = 1423
testJSON := fmt.Sprintf("%d", testNumber)
var rawMessage json.RawMessage
err = rawMessage.UnmarshalJSON([]byte(testJSON))
if err != nil {
t.Fatalf("rawMessage.UnmarshalJSON() failed: %s", err)
}
result, err := tr.GetInt32FromRawMessage(rawMessage)
if err != nil {
t.Fatalf("GetInt32FromRawMessage(%s) failed: %s", rawMessage, err)
}
if result != testNumber {
t.Fatalf("expected %d, got %d", testNumber, result)
}
}
func TestGetSliceFromRawMessage(t *testing.T) {
var err error
data := `[
{"a": "a1"},
{"b": "b1"}
]`
var rawMessage json.RawMessage
err = rawMessage.UnmarshalJSON([]byte(data))
if err != nil {
t.Fatalf("rawMessage.UnmarshalJSON() failed: %s", err)
}
result, err := tr.GetSliceFromRawMessage(rawMessage)
if err != nil {
t.Fatalf("GetSliceFromRawMessage(%s) failed: %s", rawMessage, err)
}
if len(result) != 2 {
t.Fatalf("expected %d, got %d", 2, len(result))
}
result0, err := result[0].MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON failed: %s", err)
}
if string(result0) != "{\"a\": \"a1\"}" {
t.Fatalf("result0 = %s", result0)
}
result1, err := result[1].MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON failed: %s", err)
}
if string(result1) != "{\"b\": \"b1\"}" {
t.Fatalf("result1 = %s", result1)
}
}
func TestGetMsgFromRawMessage(t *testing.T) {
var err error
const testString = "test string"
testJSON := fmt.Sprintf("\"%s\"", testString)
var rawMessage json.RawMessage
err = rawMessage.UnmarshalJSON([]byte(testJSON))
if err != nil {
t.Fatalf("rawMessage.UnmarshalJSON() failed: %s", err)
}
result, err := tr.GetMsgFromRawMessage(rawMessage)
if err != nil {
t.Fatalf("GetStringFromRawMessage(%s) failed: %s", rawMessage, err)
}
var res string
err = json.Unmarshal(result, &res)
if err != nil {
t.Fatal(errors.Wrap(err, "failed to unmarshal msg from raw message"))
}
if res != testString {
t.Fatalf("expected '%s', got '%s", testString, result)
}
}