-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_test.go
127 lines (111 loc) · 2.64 KB
/
util_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
package ghttp
import (
"errors"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
errRead = 1 << iota
errClose
)
var (
errAccessDummyBody = errors.New("dummy body is inaccessible")
)
type (
dummyBody struct {
s string
i int64
errFlag int
}
)
func (db *dummyBody) Read(b []byte) (n int, err error) {
if db.errFlag&errRead != 0 {
return 0, errAccessDummyBody
}
if db.i >= int64(len(db.s)) {
return 0, io.EOF
}
n = copy(b, db.s[db.i:])
db.i += int64(n)
return
}
func (db *dummyBody) Close() error {
if db.errFlag&errClose != 0 {
return errAccessDummyBody
}
return nil
}
func TestToString(t *testing.T) {
var (
stringVal = "hi"
bytesVal = []byte{'h', 'e', 'l', 'l', 'o'}
bytesValStr = "hello"
boolValStr = "true"
intVal = -314
intValStr = "-314"
int8Val int8 = -128
int8ValStr = "-128"
int16Val int16 = -32768
int16ValStr = "-32768"
int32Val int32 = -314159
int32ValStr = "-314159"
int64Val int64 = -31415926535
int64ValStr = "-31415926535"
uintVal uint = 314
uintValStr = "314"
uint8Val uint8 = 127
uint8ValStr = "127"
uint16Val uint16 = 32767
uint16ValStr = "32767"
uint32Val uint32 = 314159
uint32ValStr = "314159"
uint64Val uint64 = 31415926535
uint64ValStr = "31415926535"
float32Val float32 = 3.14159
float32ValStr = "3.14159"
float64Val = 3.1415926535
float64ValStr = "3.1415926535"
errVal = errAccessDummyBody
timeVal = time.Now()
)
tests := []struct {
input interface{}
want string
}{
{stringVal, stringVal},
{bytesVal, bytesValStr},
{true, boolValStr},
{intVal, intValStr},
{int8Val, int8ValStr},
{int16Val, int16ValStr},
{int32Val, int32ValStr},
{int64Val, int64ValStr},
{uintVal, uintValStr},
{uint8Val, uint8ValStr},
{uint16Val, uint16ValStr},
{uint32Val, uint32ValStr},
{uint64Val, uint64ValStr},
{float32Val, float32ValStr},
{float64Val, float64ValStr},
{errVal, errVal.Error()},
{timeVal, timeVal.String()},
}
for _, test := range tests {
assert.Equal(t, test.want, toString(test.input))
}
complexVal := 1 + 2i
assert.Panics(t, func() {
toString(complexVal)
})
}
func TestToStrings(t *testing.T) {
vs := []string{"1", "2", "3"}
var v interface{} = []int{1, 2, 3}
assert.Equal(t, vs, toStrings(v))
v = [3]int{1, 2, 3}
assert.Equal(t, vs, toStrings(v))
v = []interface{}{1, 2, 1 + 2i}
assert.Empty(t, toStrings(v))
}