-
Notifications
You must be signed in to change notification settings - Fork 8
/
encoder_test.go
132 lines (119 loc) · 2.51 KB
/
encoder_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
package fwencoder
import (
"bytes"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMarshalWriter(t *testing.T) {
buf := &bytes.Buffer{}
s := "Test String"
bb := false
i := int8(-2)
ui := uint8(2)
f := float32(1.5)
d := time.Date(2017, 12, 27, 0, 0, 0, 0, time.UTC)
obj := []TestStruct{{
String: "Test String",
Bool: true,
Int: -1,
Int8: -2,
Int16: -3,
Int32: -4,
Int64: -5,
Uint: 1,
Uint8: 2,
Uint16: 3,
Uint32: 4,
Uint64: 5,
Float32: 1.5,
Float64: 2.5,
Date: time.Date(2017, 12, 27, 13, 48, 3, 0, time.UTC),
Birthday: time.Date(2017, 12, 27, 0, 0, 0, 0, time.UTC),
PString: &s,
PBool: &bb,
PInt8: &i,
PUint8: &ui,
PFloat32: &f,
PBirthday: &d,
JSONArr: []int{1, 2, 3},
JSONPtr: &[]int{4, 5, 6},
},
{
String: "Another test string",
JSONPtr: &[]int{4, 5, 6},
}}
if assert.NoError(t, MarshalWriter(buf, &obj)) {
b, err := os.ReadFile("./testdata/marshal.txt")
if b[len(b)-1] == '\n' {
b = b[:len(b)-1]
}
if assert.NoError(t, err) {
assert.Equal(t, string(b), buf.String())
}
}
}
func TestMarshalPtr(t *testing.T) {
buf := &bytes.Buffer{}
s := "Test String"
bb := false
i := int8(-2)
ui := uint8(2)
f := float32(1.5)
d := time.Date(2017, 12, 27, 0, 0, 0, 0, time.UTC)
obj := []*TestStruct{{
String: "Test String",
Bool: true,
Int: -1,
Int8: -2,
Int16: -3,
Int32: -4,
Int64: -5,
Uint: 1,
Uint8: 2,
Uint16: 3,
Uint32: 4,
Uint64: 5,
Float32: 1.5,
Float64: 2.5,
Date: time.Date(2017, 12, 27, 13, 48, 3, 0, time.UTC),
Birthday: time.Date(2017, 12, 27, 0, 0, 0, 0, time.UTC),
PString: &s,
PBool: &bb,
PInt8: &i,
PUint8: &ui,
PFloat32: &f,
PBirthday: &d,
JSONArr: []int{1, 2, 3},
JSONPtr: &[]int{4, 5, 6},
},
{
String: "Another test string",
JSONPtr: &[]int{4, 5, 6},
}}
if assert.NoError(t, MarshalWriter(buf, &obj)) {
b, err := os.ReadFile("./testdata/marshal.txt")
if b[len(b)-1] == '\n' {
b = b[:len(b)-1]
}
if assert.NoError(t, err) {
assert.Equal(t, string(b), buf.String())
}
}
}
func TestMarshal_IncorrectInput(t *testing.T) {
errs := []error{
marshallErr(1),
marshallErr(nil),
marshallErr(new(string)),
marshallErr(&([]int{})),
}
for _, err := range errs {
assert.EqualError(t, err, ErrIncorrectInputValue.Error())
}
}
func marshallErr(i interface{}) error {
_, err := Marshal(i)
return err
}