-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint16_test.go
120 lines (112 loc) · 2.48 KB
/
int16_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
package nullable
// Do not modify. Generated by nullable-generate.
import (
"database/sql/driver"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInt16(t *testing.T) {
testCases := []struct {
ScanValue interface{}
ExpectedError bool
ExpectedValid bool
ExpectedValue int16
JSONText string
}{
{
ScanValue: int64(11),
ExpectedValid: true,
ExpectedValue: 11,
JSONText: `11`,
},
{
ScanValue: uint64(12),
ExpectedValid: true,
ExpectedValue: 12,
JSONText: `12`,
},
{
ScanValue: int32(13),
ExpectedValid: true,
ExpectedValue: 13,
JSONText: `13`,
},
{
ScanValue: uint32(14),
ExpectedValid: true,
ExpectedValue: 14,
JSONText: `14`,
},
{
ScanValue: []byte("string value"),
ExpectedError: true,
ExpectedValid: false,
ExpectedValue: 0,
JSONText: `null`,
},
{
ScanValue: nil,
ExpectedValid: false,
ExpectedValue: 0,
JSONText: "null",
},
}
assert := assert.New(t)
for i, tc := range testCases {
tcName := fmt.Sprintf("test case %d", i)
var nv Int16
err := nv.Scan(tc.ScanValue)
if tc.ExpectedError {
assert.Error(err, tcName)
continue
} else {
assert.NoError(err, tcName)
assert.Equal(tc.ExpectedValid, nv.Valid, tcName)
assert.Equal(tc.ExpectedValue, nv.Int16, tcName)
}
v, err := nv.Value()
assert.NoError(err)
if tc.ExpectedValid {
assert.Equal(driver.Value(int64(tc.ExpectedValue)), v, tcName)
assert.NotNil(nv.Ptr(), tcName)
assert.Equal(nv.Int16, *(nv.Ptr()), tcName)
nv2 := Int16FromPtr(nv.Ptr())
assert.Equal(nv, nv2, tcName)
} else {
assert.Nil(v, tcName)
assert.Nil(nv.Ptr(), tcName)
nv2 := Int16FromPtr(nv.Ptr())
assert.Equal(nv, nv2, tcName)
}
jsonText, err := nv.MarshalJSON()
assert.NoError(err)
assert.Equal(tc.JSONText, string(jsonText), tcName)
var nt2 Int16
err = nt2.UnmarshalJSON(jsonText)
assert.NoError(err)
assert.Equal(nv.Valid, nt2.Valid, tcName)
// invalid JSON for any type
err = nt2.UnmarshalJSON([]byte("00 this is not valid xx"))
assert.Error(err)
// test Normalized comparison
{
n1 := Int16{
Int16: 1,
}
n2 := Int16{
Int16: 0,
}
n3 := Int16{
Int16: 1,
Valid: true,
}
if n1.Normalized() != n2.Normalized() {
t.Errorf("expected equal, got not equal: %v != %v", n1, n2)
}
if n3.Normalized() != n3.Normalized() {
t.Errorf("expected equal, got not equal: %v != %v", n3, n3)
}
}
}
}