Skip to content

Commit c0adc02

Browse files
committed
Test types.Float#UnmarshalText()
1 parent e44f615 commit c0adc02

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

types/float_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,32 @@ func TestFloat_MarshalJSON(t *testing.T) {
2828
})
2929
}
3030
}
31+
32+
func TestFloat_UnmarshalText(t *testing.T) {
33+
subtests := []struct {
34+
name string
35+
input string
36+
output sql.NullFloat64
37+
error bool
38+
}{
39+
{"empty", "", sql.NullFloat64{}, true},
40+
{"too_big", "1e309", sql.NullFloat64{}, true},
41+
{"zero", "0", sql.NullFloat64{Float64: 0, Valid: true}, false},
42+
{"negative", "-1", sql.NullFloat64{Float64: -1, Valid: true}, false},
43+
{"fraction", "0.5", sql.NullFloat64{Float64: 0.5, Valid: true}, false},
44+
{"exp", "2e1", sql.NullFloat64{Float64: 20, Valid: true}, false},
45+
{"too_precise", "1e-1337", sql.NullFloat64{Float64: 0, Valid: true}, false},
46+
}
47+
48+
for _, st := range subtests {
49+
t.Run(st.name, func(t *testing.T) {
50+
var actual Float
51+
if err := actual.UnmarshalText([]byte(st.input)); st.error {
52+
require.Error(t, err)
53+
} else {
54+
require.NoError(t, err)
55+
require.Equal(t, Float{NullFloat64: st.output}, actual)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)