-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtypeconv_test.go
113 lines (98 loc) · 2.64 KB
/
typeconv_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
package ogórek
import (
"fmt"
"reflect"
"testing"
)
func TestAsInt64(t *testing.T) {
Etype := func(typename string) error {
return fmt.Errorf("expect int64|long; got %s", typename)
}
Erange := fmt.Errorf("long outside of int64 range")
testv := []struct {
in any
outOK any
}{
{int64(0), int64(0)},
{int64(1), int64(1)},
{int64(2), int64(2)},
{int64(123), int64(123)},
{int64(0x7fffffffffffffff), int64(0x7fffffffffffffff)},
{int64(-0x8000000000000000), int64(-0x8000000000000000)},
{bigInt("0"), int64(0)},
{bigInt("1"), int64(1)},
{bigInt("2"), int64(2)},
{bigInt("123"), int64(123)},
{bigInt("9223372036854775807"), int64(0x7fffffffffffffff)},
{bigInt("9223372036854775808"), Erange},
{bigInt("-9223372036854775808"), int64(-0x8000000000000000)},
{bigInt("-9223372036854775809"), Erange},
{1.0, Etype("float64")},
{"a", Etype("string")},
}
for _, tt := range testv {
iout, err := AsInt64(tt.in)
var out any = iout
if err != nil {
out = err
if iout != 0 {
t.Errorf("%T %#v -> err, but ret int64 = %d ; want 0",
tt.in, tt.in, iout)
}
}
if !deepEqual(out, tt.outOK) {
t.Errorf("%T %#v -> %T %#v ; want %T %#v",
tt.in, tt.in, out, out, tt.outOK, tt.outOK)
}
}
}
func TestAsBytesString(t *testing.T) {
Ebytes := func(x any) error {
return fmt.Errorf("expect bytes|bytestr; got %T", x)
}
Estring := func(x any) error {
return fmt.Errorf("expect unicode|bytestr; got %T", x)
}
const y = true
const n = false
testv := []struct {
in any
bok bool // AsBytes succeeds
sok bool // AsString succeeds
}{
{"мир", n, y},
{Bytes("мир"), y, n},
{ByteString("мир"), y, y},
{1.0, n, n},
{None{}, n, n},
}
for _, tt := range testv {
bout, berr := AsBytes(tt.in)
sout, serr := AsString(tt.in)
sin := ""
xin := reflect.ValueOf(tt.in)
if xin.Kind() == reflect.String {
sin = xin.String()
}
boutOK := Bytes(sin)
var berrOK error
if !tt.bok {
boutOK = ""
berrOK = Ebytes(tt.in)
}
soutOK := sin
var serrOK error
if !tt.sok {
soutOK = ""
serrOK = Estring(tt.in)
}
if !(bout == boutOK && deepEqual(berr, berrOK)) {
t.Errorf("%#v: AsBytes:\nhave %#v %#v\nwant %#v %#v",
tt.in, bout, berr, boutOK, berrOK)
}
if !(sout == soutOK && deepEqual(serr, serrOK)) {
t.Errorf("%#v: AsString:\nhave %#v %#v\nwant %#v %#v",
tt.in, sout, serr, soutOK, serrOK)
}
}
}