-
Notifications
You must be signed in to change notification settings - Fork 32
/
bytes_test.go
95 lines (90 loc) · 2.33 KB
/
bytes_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
package core_test
import (
"github.com/synapsecns/sanguine/core"
"reflect"
"strings"
"testing"
)
func TestBytesToSlice(t *testing.T) {
tests := []struct {
name string
bytes [32]byte
want []byte
}{
{
name: "all zeros",
bytes: [32]byte{},
want: make([]byte, 32),
},
{
name: "random bytes",
bytes: [32]byte{0, 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},
want: []byte{0, 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},
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
if got := core.BytesToSlice(tt.bytes); !reflect.DeepEqual(got, tt.want) {
t.Errorf("BytesToSlice() = %v, want %v", got, tt.want)
}
})
}
}
func TestBytesToJSONString(t *testing.T) {
tests := []struct {
name string
input []byte
want string
wantErr bool
errString string
}{
{
name: "Valid JSON",
input: []byte(`{"key1":"value1","key2":2}`),
want: `{"key1":"value1","key2":2}`,
wantErr: false,
},
{
name: "Invalid JSON",
input: []byte(`{"key1":"value1",`),
want: "",
wantErr: true,
errString: "failed to unmarshal JSON",
},
{
name: "Empty JSON",
input: []byte(`{}`),
want: `{}`,
wantErr: false,
},
{
name: "Nested JSON",
input: []byte(`{"key1":{"nestedKey1":"nestedValue1"},"key2":[1,2,3]}`),
want: `{"key1":{"nestedKey1":"nestedValue1"},"key2":[1,2,3]}`,
wantErr: false,
},
{
name: "JSON with special characters",
input: []byte(`{"key1":"value1\nvalue2","key2":"value3\tvalue4"}`),
want: `{"key1":"value1\nvalue2","key2":"value3\tvalue4"}`,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got, err := core.BytesToJSONString(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("BytesToJSONString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil && tt.wantErr && tt.errString != "" && !strings.Contains(err.Error(), tt.errString) {
t.Errorf("BytesToJSONString() error = %v, expected error to contain %v", err, tt.errString)
}
if got != tt.want {
t.Errorf("BytesToJSONString() = %v, want %v", got, tt.want)
}
})
}
}