Debian Codegraph @

zero/float_test.go

raw symbols SCIP
1 package zero
2
3 import (
4 "encoding/json"
5 "testing"
6 )
7
8 var (
9 floatJSON = []byte(`1.2345`)
10 nullFloatJSON = []byte(`{"Float64":1.2345,"Valid":true}`)
11 )
12
13 func TestFloatFrom(t *testing.T) {
14 f := FloatFrom(1.2345)
15 assertFloat(t, f, "FloatFrom()")
16
17 zero := FloatFrom(0)
18 if zero.Valid {
19 t.Error("FloatFrom(0)", "is valid, but should be invalid")
20 }
21 }
22
23 func TestFloatFromPtr(t *testing.T) {
24 n := float64(1.2345)
25 iptr := &n
26 f := FloatFromPtr(iptr)
27 assertFloat(t, f, "FloatFromPtr()")
28
29 null := FloatFromPtr(nil)
30 assertNullFloat(t, null, "FloatFromPtr(nil)")
31 }
32
33 func TestUnmarshalFloat(t *testing.T) {
34 var f Float
35 err := json.Unmarshal(floatJSON, &f)
36 maybePanic(err)
37 assertFloat(t, f, "float json")
38
39 var nf Float
40 err = json.Unmarshal(nullFloatJSON, &nf)
41 maybePanic(err)
42 assertFloat(t, nf, "sql.NullFloat64 json")
43
44 var zero Float
45 err = json.Unmarshal(zeroJSON, &zero)
46 maybePanic(err)
47 assertNullFloat(t, zero, "zero json")
48
49 var null Float
50 err = json.Unmarshal(nullJSON, &null)
51 maybePanic(err)
52 assertNullFloat(t, null, "null json")
53
54 var badType Float
55 err = json.Unmarshal(boolJSON, &badType)
56 if err == nil {
57 panic("err should not be nil")
58 }
59 assertNullFloat(t, badType, "wrong type json")
60
61 var invalid Float
62 err = invalid.UnmarshalJSON(invalidJSON)
63 if _, ok := err.(*json.SyntaxError); !ok {
64 t.Errorf("expected json.SyntaxError, not %T", err)
65 }
66 assertNullFloat(t, invalid, "invalid json")
67 }
68
69 func TestTextUnmarshalFloat(t *testing.T) {
70 var f Float
71 err := f.UnmarshalText([]byte("1.2345"))
72 maybePanic(err)
73 assertFloat(t, f, "UnmarshalText() float")
74
75 var zero Float
76 err = zero.UnmarshalText([]byte("0"))
77 maybePanic(err)
78 assertNullFloat(t, zero, "UnmarshalText() zero float")
79
80 var blank Float
81 err = blank.UnmarshalText([]byte(""))
82 maybePanic(err)
83 assertNullFloat(t, blank, "UnmarshalText() empty float")
84
85 var null Float
86 err = null.UnmarshalText([]byte("null"))
87 maybePanic(err)
88 assertNullFloat(t, null, `UnmarshalText() "null"`)
89 }
90
91 func TestMarshalFloat(t *testing.T) {
92 f := FloatFrom(1.2345)
93 data, err := json.Marshal(f)
94 maybePanic(err)
95 assertJSONEquals(t, data, "1.2345", "non-empty json marshal")
96
97 // invalid values should be encoded as 0
98 null := NewFloat(0, false)
99 data, err = json.Marshal(null)
100 maybePanic(err)
101 assertJSONEquals(t, data, "0", "null json marshal")
102 }
103
104 func TestMarshalFloatText(t *testing.T) {
105 f := FloatFrom(1.2345)
106 data, err := f.MarshalText()
107 maybePanic(err)
108 assertJSONEquals(t, data, "1.2345", "non-empty text marshal")
109
110 // invalid values should be encoded as zero
111 null := NewFloat(0, false)
112 data, err = null.MarshalText()
113 maybePanic(err)
114 assertJSONEquals(t, data, "0", "null text marshal")
115 }
116
117 func TestFloatPointer(t *testing.T) {
118 f := FloatFrom(1.2345)
119 ptr := f.Ptr()
120 if *ptr != 1.2345 {
121 t.Errorf("bad %s Float: %#v ≠ %v\n", "pointer", ptr, 1.2345)
122 }
123
124 null := NewFloat(0, false)
125 ptr = null.Ptr()
126 if ptr != nil {
127 t.Errorf("bad %s Float: %#v ≠ %s\n", "nil pointer", ptr, "nil")
128 }
129 }
130
131 func TestFloatIsZero(t *testing.T) {
132 f := FloatFrom(1.2345)
133 if f.IsZero() {
134 t.Errorf("IsZero() should be false")
135 }
136
137 null := NewFloat(0, false)
138 if !null.IsZero() {
139 t.Errorf("IsZero() should be true")
140 }
141
142 zero := NewFloat(0, true)
143 if !zero.IsZero() {
144 t.Errorf("IsZero() should be true")
145 }
146 }
147
148 func TestFloatSetValid(t *testing.T) {
149 change := NewFloat(0, false)
150 assertNullFloat(t, change, "SetValid()")
151 change.SetValid(1.2345)
152 assertFloat(t, change, "SetValid()")
153 }
154
155 func TestFloatScan(t *testing.T) {
156 var f Float
157 err := f.Scan(1.2345)
158 maybePanic(err)
159 assertFloat(t, f, "scanned float")
160
161 var null Float
162 err = null.Scan(nil)
163 maybePanic(err)
164 assertNullFloat(t, null, "scanned null")
165 }
166
167 func assertFloat(t *testing.T, f Float, from string) {
168 if f.Float64 != 1.2345 {
169 t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
170 }
171 if !f.Valid {
172 t.Error(from, "is invalid, but should be valid")
173 }
174 }
175
176 func assertNullFloat(t *testing.T, f Float, from string) {
177 if f.Valid {
178 t.Error(from, "is valid, but should be invalid")
179 }
180 }