-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_test.go
More file actions
91 lines (86 loc) · 1.71 KB
/
buffer_test.go
File metadata and controls
91 lines (86 loc) · 1.71 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
85
86
87
88
89
90
91
package converter
import (
"bytes"
"errors"
"testing"
)
func TestToBuffer(t *testing.T) {
type args struct {
a any
}
tests := []struct {
name string
args args
want *bytes.Buffer
wantPanic bool
}{
{
name: "ValidInputCase",
args: args{
a: "test string",
},
want: bytes.NewBufferString("test string"),
wantPanic: false,
},
{
name: "InvalidNilInputCase",
args: args{
a: nil,
},
want: nil,
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); (r != nil) != tt.wantPanic {
t.Errorf("ToBuffer() recover() = %v, wantPanic = %v", r, tt.wantPanic)
}
}()
if got := ToBuffer(tt.args.a); !bytes.Equal(got.Bytes(), tt.want.Bytes()) {
t.Errorf("ToBuffer() = %v, want %v", got, tt.want)
}
})
}
}
func TestToBufferWithErr(t *testing.T) {
type args struct {
a any
}
tests := []struct {
name string
args args
want *bytes.Buffer
wantErr error
}{
{
name: "ValidInputCase",
args: args{
a: "Another test string",
},
want: bytes.NewBufferString("Another test string"),
wantErr: nil,
},
{
name: "InvalidNilInputCase",
args: args{
a: nil,
},
want: nil,
wantErr: errors.New("Invalid input: nil"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToBufferWithErr(tt.args.a)
if (err != nil) != (tt.wantErr != nil) {
t.Errorf("ToBufferWithErr() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && tt.want != nil && !bytes.Equal(got.Bytes(), tt.want.Bytes()) {
t.Errorf("ToBufferWithErr() = %v, want %v", got, tt.want)
}
})
}
}