-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverters_test.go
194 lines (187 loc) · 3.76 KB
/
converters_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package goutils
import (
"fmt"
assert2 "github.com/stretchr/testify/assert"
"math"
"testing"
)
func Test_ParseValue(t *testing.T) {
tests := []struct {
arg string
want interface{}
}{
{
arg: "true",
want: true,
},
{
arg: "nil",
want: nil,
},
{
arg: "null ",
want: nil,
},
{
arg: " ",
want: " ",
},
{
arg: "512",
want: int64(512),
},
{
arg: "-512",
want: int64(-512),
},
{
arg: " some text ",
want: " some text ",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("ParseValue_%s", tt.arg), func(t *testing.T) {
got := ParseValue(tt.arg)
if got != tt.want {
t.Errorf("ParseValue() got = %v, want %v", got, tt.want)
}
})
}
t.Run("ParseValue_floats", func(t *testing.T) {
for _, f := range []float64{-19.01, 193.834} {
t.Run(fmt.Sprintf("convert_%f", f), func(t *testing.T) {
str := fmt.Sprint(f)
value := ParseValue(str)
assert2.NotNil(t, value)
if math.Abs(value.(float64)-f) > 0.00001 {
assert2.Equal(t, value, f)
}
})
}
})
}
func Test_ParseBool(t *testing.T) {
tests := []struct {
arg string
want bool
expectError bool
}{
{
arg: "true",
want: true,
},
{
arg: " 0",
want: false,
},
{
arg: "\toff ",
want: false,
},
{
arg: "TrUE",
want: true,
},
{
arg: " on ",
want: true,
},
{
arg: "blabla",
want: false,
expectError: true,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("ParseBool_%s", tt.arg), func(t *testing.T) {
got, err := ParseBool(tt.arg)
assert2.Equal(t, tt.want, got)
if tt.expectError {
if err == nil {
t.Errorf("ParseBool() error expected")
}
} else {
assert2.NoError(t, err)
}
})
}
t.Run("convertValue_floats", func(t *testing.T) {
for _, f := range []float64{-19.01, 193.834} {
t.Run(fmt.Sprintf("convert_%f", f), func(t *testing.T) {
str := fmt.Sprint(f)
value := ParseValue(str)
assert2.NotNil(t, value)
if math.Abs(value.(float64)-f) > 0.00001 {
assert2.Equal(t, value, f)
}
})
}
})
}
func TestAsFloat64(t *testing.T) {
tests := []struct {
arg interface{}
want float64
wantErr bool
}{
{"-17.2", -17.2, false},
{"17.2", 17.2, false},
{" 17.2 ", 17.2, false},
{"17", 17, false},
{17, 17, false},
{float32(17), 17, false},
{int64(17), 17, false},
{int32(17), 17, false},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("converting %v of type %T", tt.arg, tt.arg), func(t *testing.T) {
got, err := AsFloat64(tt.arg)
if tt.wantErr && err == nil {
assert2.Fail(t, "error expected")
}
assert2.Equalf(t, tt.want, got, "AsFloat64(%v)", tt.arg)
})
}
}
func TestRoundFloat64(t *testing.T) {
tests := []struct {
arg float64
precision uint
want float64
}{
{1.0 / 3.0, 9, 0.333333333},
{1.0 / 3.0, 2, 0.33},
{1.0 / 3.0, 0, 0},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("RoundFloat(%f, %d) = %f", tt.arg, tt.precision, tt.want), func(t *testing.T) {
result := RoundFloat(tt.arg, tt.precision)
assert2.Equal(t, tt.want, result)
})
}
}
func TestHexToInt(t *testing.T) {
tests := []struct {
args string
want int
wantErr assert2.ErrorAssertionFunc
}{
{"A", 10, assert2.NoError},
{"0xA", 10, assert2.NoError},
{"0X0A", 10, assert2.NoError},
{"0B", 11, assert2.NoError},
{"-B", -11, assert2.NoError},
{"-b", -11, assert2.NoError},
{"-0xb", -11, assert2.NoError},
{"invalid", 0, assert2.Error},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("HexToInt(\"%s\") is %d", tt.args, tt.want), func(t *testing.T) {
got, err := HexToInt(tt.args)
if !tt.wantErr(t, err, fmt.Sprintf("HexToInt(%v)", tt.args)) {
return
}
assert2.Equalf(t, tt.want, got, "HexToInt(%v)", tt.args)
})
}
}