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
|
package json
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestNumberFloatValue(t *testing.T) {
Convey("When unmarshaling JSON with float values", t, func() {
Convey("converts to a JSON NumberFloat value", func() {
var jsonMap map[string]interface{}
key := "key"
value := "5.5"
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue, ok := jsonMap[key].(float64)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldEqual, NumberFloat(5.5))
})
})
Convey("When unmarshaling and marshaling NumberFloat values", t, func() {
key := "key"
Convey("maintains decimal point with trailing zero", func() {
var jsonMap map[string]interface{}
value := "5.0"
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue, ok := jsonMap[key].(float64)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldEqual, NumberFloat(5.0))
numFloat := NumberFloat(jsonValue)
byteValue, err := numFloat.MarshalJSON()
So(err, ShouldBeNil)
So(string(byteValue), ShouldEqual, "5.0")
})
Convey("maintains precision with large decimals", func() {
var jsonMap map[string]interface{}
value := "5.52342123"
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue, ok := jsonMap[key].(float64)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldEqual, NumberFloat(5.52342123))
numFloat := NumberFloat(jsonValue)
byteValue, err := numFloat.MarshalJSON()
So(err, ShouldBeNil)
So(string(byteValue), ShouldEqual, "5.52342123")
})
Convey("maintains exponent values", func() {
var jsonMap map[string]interface{}
value := "5e+32"
data := fmt.Sprintf(`{"%v":%v}`, key, value)
err := Unmarshal([]byte(data), &jsonMap)
So(err, ShouldBeNil)
jsonValue, ok := jsonMap[key].(float64)
So(ok, ShouldBeTrue)
So(jsonValue, ShouldEqual, NumberFloat(5e32))
numFloat := NumberFloat(jsonValue)
byteValue, err := numFloat.MarshalJSON()
So(err, ShouldBeNil)
So(string(byteValue), ShouldEqual, "5e+32")
})
})
}
|