-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
102 lines (99 loc) · 2.01 KB
/
string_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
package zfmt
import (
"bytes"
"reflect"
"testing"
)
func TestStringFormatter_Marshall(t *testing.T) {
type args struct {
i any
}
tests := []struct {
name string
f *StringFormatter
args args
want []byte
wantErr bool
}{
{
name: "string",
f: &StringFormatter{},
args: args{i: "test"},
want: []byte("test"),
wantErr: false,
},
{
name: "byte array",
f: &StringFormatter{},
args: args{i: []byte("test")},
want: []byte("test"),
wantErr: false,
},
{
name: "bytes buffer",
f: &StringFormatter{},
args: args{i: bytes.NewBufferString("test")},
want: []byte("test"),
wantErr: false,
},
{
name: "invalid type",
f: &StringFormatter{},
args: args{i: 123},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &StringFormatter{}
got, err := f.Marshall(tt.args.i)
if (err != nil) != tt.wantErr {
t.Errorf("StringFormatter.Marshall() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringFormatter.Marshall() = %v, want %v", got, tt.want)
}
})
}
}
func TestStringFormatter_Unmarshal(t *testing.T) {
type args struct {
b []byte
i any
}
tests := []struct {
name string
f *StringFormatter
args args
wantErr bool
}{
{
name: "string is immutable so that doesn't work",
f: &StringFormatter{},
args: args{
b: []byte("test"),
i: func(str string) *string { return &str }(""),
},
wantErr: true,
},
{
name: "supply io.Writer",
f: &StringFormatter{},
args: args{
b: []byte("test"),
i: new(bytes.Buffer),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &StringFormatter{}
if err := f.Unmarshal(tt.args.b, tt.args.i); (err != nil) != tt.wantErr {
t.Errorf("StringFormatter.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}