-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
98 lines (89 loc) · 1.76 KB
/
types_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
package args
import (
"fmt"
"testing"
)
func TestBadTarget(t *testing.T) {
i := 42
err := convertValue("43", i)
expected := `target for value "43" is not a pointer`
if err == nil {
t.Errorf("error missing")
} else if err.Error() != expected {
t.Errorf("unexpected error: %v", err)
}
}
func TestType(t *testing.T) {
var i int
var i8 int8
var i16 int16
var i32 int32
var i64 int64
var ui uint
var ui8 uint8
var ui16 uint16
var ui32 uint32
var ui64 uint64
var f32 float32
var f64 float64
count := 0
test := func(input string, i interface{}) {
count++
err := convertValue(input, i)
if err != nil {
t.Errorf("unexpected error in test %d: %v", count, err)
}
s := fmt.Sprintf("%v", reflValue(i))
if s != input {
t.Errorf(`difference in test %d: %s != %s`, count, input, s)
}
}
test("-1", &i)
test("-1", &i8)
test("-1", &i16)
test("-1", &i32)
test("-1", &i64)
test("1", &ui)
test("1", &ui8)
test("1", &ui16)
test("1", &ui32)
test("1", &ui64)
test("1.5", &f32)
test("1.5", &f64)
}
// math.MaxInt64 is 9223372036854775807
// math.MaxFloat64 is 1.7976931348623157e+308
func TestTypeError(t *testing.T) {
var i int
var i8 int8
var i16 int16
var i32 int32
var i64 int64
var ui uint
var ui8 uint8
var ui16 uint16
var ui32 uint32
var ui64 uint64
var f32 float32
var f64 float64
count := 0
test := func(input string, i interface{}) {
count++
err := convertValue(input, i)
if err == nil {
t.Errorf("error missing in test %d", count)
}
}
test("abc", &i)
test("9223372036854775807", &i8)
test("9223372036854775807", &i16)
test("9223372036854775807", &i32)
test("abc", &i64)
test("-1", &ui)
test("-1", &ui8)
test("-1", &ui16)
test("-1", &ui32)
test("-1", &ui64)
test("1.7976931348623157e+308", &f32)
test("true", &f64)
}