Repository navigation
Expand file tree
/
Copy pathfloat_test.go
More file actions
115 lines (105 loc) · 2.72 KB
/
Copy pathfloat_test.go
File metadata and controls
115 lines (105 loc) · 2.72 KB
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
package opt
import (
"encoding/json"
"math"
"testing"
)
func TestFloatFrom(t *testing.T) {
f := FloatFrom(3.14)
if f.V != 3.14 || !f.Valid {
t.Errorf("FloatFrom(3.14): got {%v, %v}", f.V, f.Valid)
}
zero := FloatFrom(0)
if !zero.Valid {
t.Error("FloatFrom(0) should be valid")
}
}
func TestFloatMarshalJSON(t *testing.T) {
tests := []struct {
name string
val Float
want string
wantErr bool
}{
{"positive", FloatFrom(3.14), "3.14", false},
{"zero", FloatFrom(0), "0", false},
{"negative", FloatFrom(-1.5), "-1.5", false},
{"null", NewFloat(0, false), "null", false},
{"inf", NewFloat(math.Inf(1), true), "", true},
{"nan", NewFloat(math.NaN(), true), "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.val)
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && string(data) != tt.want {
t.Errorf("got %s, want %s", data, tt.want)
}
})
}
}
func TestFloatUnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
want float64
valid bool
wantErr bool
}{
{"number", "3.14", 3.14, true, false},
{"integer", "42", 42, true, false},
{"zero", "0", 0, true, false},
{"string number", `"3.14"`, 3.14, true, false},
{"empty string", `""`, 0, false, false},
{"null", "null", 0, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var f Float
err := json.Unmarshal([]byte(tt.input), &f)
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr {
if f.Valid != tt.valid || (f.Valid && f.V != tt.want) {
t.Errorf("got {%v, %v}, want {%v, %v}", f.V, f.Valid, tt.want, tt.valid)
}
}
})
}
}
func TestFloatEqual(t *testing.T) {
if !FloatFrom(3.14).Equal(FloatFrom(3.14)) {
t.Error("equal floats should be Equal")
}
if FloatFrom(3.14).Equal(FloatFrom(2.71)) {
t.Error("different floats should not be Equal")
}
if !NewFloat(0, false).Equal(NewFloat(0, false)) {
t.Error("two null floats should be Equal")
}
}
func TestFloatJSONRoundtrip(t *testing.T) {
type measurement struct {
Temp Float `json:"temp"`
Humidity Float `json:"humidity"`
}
original := measurement{Temp: FloatFrom(23.5), Humidity: NewFloat(0, false)}
data, err := json.Marshal(original)
if err != nil {
t.Fatal(err)
}
want := `{"temp":23.5,"humidity":null}`
if string(data) != want {
t.Errorf("marshal: got %s, want %s", data, want)
}
var decoded measurement
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatal(err)
}
if !decoded.Temp.Equal(original.Temp) || !decoded.Humidity.Equal(original.Humidity) {
t.Error("roundtrip failed")
}
}