-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
178 lines (149 loc) · 4.85 KB
/
encode_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package snbt
import (
"testing"
)
func TestSnbtEncode(t *testing.T) {
assertVal := func(v any, out string) {
get, err := Encode(v, DefaultCaster)
if err != nil {
t.Fatalf("when encode %v, expect: %v, get err: %v", v, out, err)
} else if string(get) != out {
t.Fatalf("when encode %v, expect: %v, get: %v", v, out, string(get))
}
}
assertVal(`abc\abc"`, `"abc\\abc\""`)
assertVal(int32(0), `0`)
assertVal(int32(3), `3`)
assertVal(int32(-3), `-3`)
assertVal(int32(2401), `2401`)
assertVal(int32(-2401), `-2401`)
assertVal(int8(0), `0b`)
assertVal(int8(3), `3b`)
assertVal(int8(-3), `-3b`)
assertVal(int8(123), `123b`)
assertVal(int8(-123), `-123b`)
assertVal(int16(0), `0s`)
assertVal(int16(3), `3s`)
assertVal(int16(-3), `-3s`)
assertVal(int16(2401), `2401s`)
assertVal(int16(-2401), `-2401s`)
assertVal(int64(0), `0l`)
assertVal(int64(3), `3l`)
assertVal(int64(-3), `-3l`)
assertVal(int64(2401), `2401l`)
assertVal(int64(-2401), `-2401l`)
assertVal(float32(123.4567e20), `12345670000000000000000f`)
assertVal(float32(123.4567e-20), `0.000000000000000001234567f`)
assertVal(float64(123.4567e20), `12345670000000000000000d`)
assertVal(float64(123.4567e-20), `0.000000000000000001234567d`)
assertVal([]int32{-123, 456, 7, -8, 0, 9}, `[I; -123, 456, 7, -8, 0, 9]`)
assertVal([]int64{-123, 456, 7, -8, 0, 9}, `[L; -123, 456, 7, -8, 0, 9]`)
assertVal([]int8{-123, 45, 7, -8, 0, 9}, `[B; -123, 45, 7, -8, 0, 9]`)
assertVal([]any{int32(123), int8(-4), "abc\\\"", []any{int8(1), int32(-2), "bc"}}, `[ 123, -4b, "abc\\\"", [ 1b, -2, "bc"]]`)
assertVal(
map[string]any{
"abc": int32(123),
"测试": int8(-4),
"abc\\\"": "abc\\\"",
"@{}<>!-=()[]*&^%$#/+~.\";": []any{
int8(1),
int32(-2),
"bc",
map[string]any{
"abc": int32(123)},
},
},
`{"@{}<>!-=()[]*&^%$#/+~.\";": [ 1b, -2, "bc", {"abc": 123}], "abc": 123, "abc\\\"": "abc\\\"", "测试": -4b}`,
)
}
type MyString string
type MyTypeInt8 int8
type MyTypeInt16 int16
type MyTypeInt32 int32
type MyTypeInt64 int64
type MyTypeFloat32 float32
type MyTypeFloat64 float64
type MyBool bool
type MyArrInt32 [3]MyTypeInt32
type MyArrInt8 [3]MyTypeInt8
type MyArrInt64 [3]MyTypeInt64
type MySliceInt32 []MyTypeInt32
type MySliceInt8 []MyTypeInt8
type MySliceInt64 []MyTypeInt64
type MyAny any
type MySliceAny []MyAny
type MyMapAny map[string]MyAny
type MyStructSub struct {
SA MyString `nbt:"sa"`
}
type MyStruct struct {
SB MyTypeInt32 `nbt:"sb"`
MyStructSub
}
func TestSnbtEncodeWithCast(t *testing.T) {
assertVal := func(v any, out string) {
get, err := Encode(v, DefaultCaster)
if err != nil {
t.Fatalf("when encode %v, expect: %v, get err: %v", v, out, err)
} else if string(get) != out {
t.Fatalf("when encode %v, expect: %v, get: %v", v, out, string(get))
}
}
assertVal(MyString(`abc\abc"`), `"abc\\abc\""`)
assertVal(MyTypeInt32(0), `0`)
assertVal(MyTypeInt32(3), `3`)
assertVal(MyTypeInt32(-3), `-3`)
assertVal(MyTypeInt32(2401), `2401`)
assertVal(MyTypeInt32(-2401), `-2401`)
assertVal(MyTypeInt8(0), `0b`)
assertVal(MyTypeInt8(3), `3b`)
assertVal(MyTypeInt8(-3), `-3b`)
assertVal(MyTypeInt8(123), `123b`)
assertVal(MyTypeInt8(-123), `-123b`)
assertVal(MyTypeInt16(0), `0s`)
assertVal(MyTypeInt16(3), `3s`)
assertVal(MyTypeInt16(-3), `-3s`)
assertVal(MyTypeInt16(2401), `2401s`)
assertVal(MyTypeInt16(-2401), `-2401s`)
assertVal(MyTypeInt64(0), `0l`)
assertVal(MyTypeInt64(3), `3l`)
assertVal(MyTypeInt64(-3), `-3l`)
assertVal(MyTypeInt64(2401), `2401l`)
assertVal(MyTypeInt64(-2401), `-2401l`)
assertVal(MyTypeFloat32(123.4567e20), `12345670000000000000000f`)
assertVal(MyTypeFloat32(123.4567e-20), `0.000000000000000001234567f`)
assertVal(MyTypeFloat64(123.4567e20), `12345670000000000000000d`)
assertVal(MyTypeFloat64(123.4567e-20), `0.000000000000000001234567d`)
assertVal(MyBool(true), "1b")
assertVal(MyBool(false), "0b")
assertVal(MyArrInt32{-123, 456, 7}, `[I; -123, 456, 7]`)
assertVal(MyArrInt64{-123, 456, 7}, `[L; -123, 456, 7]`)
assertVal(MyArrInt8{-123, 45, 7}, `[B; -123, 45, 7]`)
assertVal(MySliceInt32{-123, 456, 7, -8, 0, 9}, `[I; -123, 456, 7, -8, 0, 9]`)
assertVal(MySliceInt64{-123, 456, 7, -8, 0, 9}, `[L; -123, 456, 7, -8, 0, 9]`)
assertVal(MySliceInt8{-123, 45, 7, -8, 0, 9}, `[B; -123, 45, 7, -8, 0, 9]`)
assertVal(MySliceAny{int32(123), int8(-4), "abc\\\"", []any{int8(1), int32(-2), "bc"}}, `[ 123, -4b, "abc\\\"", [ 1b, -2, "bc"]]`)
assertVal(
MyMapAny{
"abc": int32(123),
"测试": int8(-4),
"abc\\\"": "abc\\\"",
"@{}<>!-=()[]*&^%$#/+~.\";": []any{
int8(1),
int32(-2),
"bc",
map[string]any{
"abc": int32(123)},
},
},
`{"@{}<>!-=()[]*&^%$#/+~.\";": [ 1b, -2, "bc", {"abc": 123}], "abc": 123, "abc\\\"": "abc\\\"", "测试": -4b}`,
)
assertVal(
MyStruct{
MyStructSub: MyStructSub{
SA: "abc",
},
SB: MyTypeInt32(34),
}, `{"sa": "abc", "sb": 34}`,
)
}