Add round-trip JSON test.

This commit is contained in:
Casey Marshall 2015-03-23 17:07:27 -05:00
parent 9fc32a450f
commit a2664e0073

View file

@ -1,6 +1,7 @@
package decimal
import (
"encoding/json"
"math"
"testing"
)
@ -147,6 +148,30 @@ func TestNewFromFloatWithExponent(t *testing.T) {
}
}
func TestJSON(t *testing.T) {
for _, s := range testTable {
var doc struct {
Amount Decimal `json:"amount"`
}
docStr := `{"amount":"` + s + `"}`
err := json.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else if doc.Amount.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, doc.Amount.String(),
doc.Amount.value.String(), doc.Amount.exp)
}
out, err := json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStr {
t.Errorf("expected %s, got %s", docStr, string(out))
}
}
}
func TestDecimal_rescale(t *testing.T) {
type Inp struct {
int int64