From a2664e00736cf614086d237cd1cf1c1754fd45a8 Mon Sep 17 00:00:00 2001 From: Casey Marshall Date: Mon, 23 Mar 2015 17:07:27 -0500 Subject: [PATCH] Add round-trip JSON test. --- decimal_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/decimal_test.go b/decimal_test.go index 7121a5c..c590d0d 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -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