-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes_test.go
More file actions
84 lines (74 loc) · 1.77 KB
/
bytes_test.go
File metadata and controls
84 lines (74 loc) · 1.77 KB
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
package converter
import (
"errors"
"testing"
)
func TestCouldBeBytes(t *testing.T) {
tests := []struct {
name string
arg any
want bool
}{
{"ByteString", "Hello", true},
{"EmptyString", "", true},
{"Number", 123, true},
{"Nil", nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CouldBeBytes(tt.arg); got != tt.want {
t.Errorf("CouldBeBytes() = %v, want %v", got, tt.want)
}
})
}
}
func TestToBytes(t *testing.T) {
tests := []struct {
name string
arg any
want []byte
expectPanic bool
}{
{"ByteString", "Hello", []byte("Hello"), false},
{"EmptyString", "", []byte(""), false},
{"Number", 123, nil, true},
{"Nil", nil, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil && !tt.expectPanic {
t.Errorf("ToBytes panic = %v, want panic = %v", r, tt.expectPanic)
}
}()
if got := ToBytes(tt.arg); !tt.expectPanic && string(got) != string(tt.want) {
t.Errorf("ToBytes() = %v, want %v", got, tt.want)
}
})
}
}
func TestToBytesWithErr(t *testing.T) {
tests := []struct {
name string
arg any
want []byte
wantErr error
}{
{"ByteString", "Hello", []byte("Hello"), nil},
{"EmptyString", "", []byte(""), nil},
{"Number", 123, []byte("123"), nil},
{"Nil", nil, nil, errors.New("Nil cannot be converted to string")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToBytesWithErr(tt.arg)
if (err != nil) != (tt.wantErr != nil) {
t.Errorf("ToBytesWithErr() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(got) != string(tt.want) {
t.Errorf("ToBytesWithErr() = %v, want %v", got, tt.want)
}
})
}
}