Debian Codegraph @

float_test.go

raw symbols SCIP
1 package null
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 invalid, but should be valid")
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, "sq.NullFloat64 json")
43
44 var null Float
45 err = json.Unmarshal(nullJSON, &null)
46 maybePanic(err)
47 assertNullFloat(t, null, "null json")
48
49 var badType Float
50 err = json.Unmarshal(boolJSON, &badType)
51 if err == nil {
52 panic("err should not be nil")
53 }
54 assertNullFloat(t, badType, "wrong type json")
55
56 var invalid Float
57 err = invalid.UnmarshalJSON(invalidJSON)
58 if _, ok := err.(*json.SyntaxError); !ok {
59 t.Errorf("expected json.SyntaxError, not %T", err)
60 }
61 }
62
63 func TestTextUnmarshalFloat(t *testing.T) {
64 var f Float
65 err := f.UnmarshalText([]byte("1.2345"))
66 maybePanic(err)
67 assertFloat(t, f, "UnmarshalText() float")
68
69 var blank Float
70 err = blank.UnmarshalText([]byte(""))
71 maybePanic(err)
72 assertNullFloat(t, blank, "UnmarshalText() empty float")
73
74 var null Float
75 err = null.UnmarshalText([]byte("null"))
76 maybePanic(err)
77 assertNullFloat(t, null, `UnmarshalText() "null"`)
78 }
79
80 func TestMarshalFloat(t *testing.T) {
81 f := FloatFrom(1.2345)
82 data, err := json.Marshal(f)
83 maybePanic(err)
84 assertJSONEquals(t, data, "1.2345", "non-empty json marshal")
85
86 // invalid values should be encoded as null
87 null := NewFloat(0, false)
88 data, err = json.Marshal(null)
89 maybePanic(err)
90 assertJSONEquals(t, data, "null", "null json marshal")
91 }
92
93 func TestMarshalFloatText(t *testing.T) {
94 f := FloatFrom(1.2345)
95 data, err := f.MarshalText()
96 maybePanic(err)
97 assertJSONEquals(t, data, "1.2345", "non-empty text marshal")
98
99 // invalid values should be encoded as null
100 null := NewFloat(0, false)
101 data, err = null.MarshalText()
102 maybePanic(err)
103 assertJSONEquals(t, data, "", "null text marshal")
104 }
105
106 func TestFloatPointer(t *testing.T) {
107 f := FloatFrom(1.2345)
108 ptr := f.Ptr()
109 if *ptr != 1.2345 {
110 t.Errorf("bad %s float: %#v ≠ %v\n", "pointer", ptr, 1.2345)
111 }
112
113 null := NewFloat(0, false)
114 ptr = null.Ptr()
115 if ptr != nil {
116 t.Errorf("bad %s float: %#v ≠ %s\n", "nil pointer", ptr, "nil")
117 }
118 }
119
120 func TestFloatIsZero(t *testing.T) {
121 f := FloatFrom(1.2345)
122 if f.IsZero() {
123 t.Errorf("IsZero() should be false")
124 }
125
126 null := NewFloat(0, false)
127 if !null.IsZero() {
128 t.Errorf("IsZero() should be true")
129 }
130
131 zero := NewFloat(0, true)
132 if zero.IsZero() {
133 t.Errorf("IsZero() should be false")
134 }
135 }
136
137 func TestFloatSetValid(t *testing.T) {
138 change := NewFloat(0, false)
139 assertNullFloat(t, change, "SetValid()")
140 change.SetValid(1.2345)
141 assertFloat(t, change, "SetValid()")
142 }
143
144 func TestFloatScan(t *testing.T) {
145 var f Float
146 err := f.Scan(1.2345)
147 maybePanic(err)
148 assertFloat(t, f, "scanned float")
149
150 var null Float
151 err = null.Scan(nil)
152 maybePanic(err)
153 assertNullFloat(t, null, "scanned null")
154 }
155
156 func assertFloat(t *testing.T, f Float, from string) {
157 if f.Float64 != 1.2345 {
158 t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
159 }
160 if !f.Valid {
161 t.Error(from, "is invalid, but should be valid")
162 }
163 }
164
165 func assertNullFloat(t *testing.T, f Float, from string) {
166 if f.Valid {
167 t.Error(from, "is valid, but should be invalid")
168 }
169 }