From 0fb3379e01c365464ace475fd94e5febd00e8371 Mon Sep 17 00:00:00 2001 From: Casey Marshall Date: Mon, 23 Mar 2015 19:39:37 -0500 Subject: [PATCH] Test unhappy JSON and XML paths. --- decimal_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/decimal_test.go b/decimal_test.go index a34c7db..7d9bbae 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -173,6 +173,23 @@ func TestJSON(t *testing.T) { } } +func TestBadJSON(t *testing.T) { + for _, testCase := range []string{ + "]o_o[", "{", `{"amount":""`, + `{"amount":""}`, + `{"amount":"nope"}`, + `0.333`, + } { + var doc struct { + Amount Decimal `json:"amount"` + } + err := json.Unmarshal([]byte(testCase), &doc) + if err == nil { + t.Errorf("expected error, got %+v", doc) + } + } +} + func TestXML(t *testing.T) { for _, s := range testTable { var doc struct { @@ -198,6 +215,25 @@ func TestXML(t *testing.T) { } } +func TestBadXML(t *testing.T) { + for _, testCase := range []string{ + "o_o", "7", + ``, + ``, + `nope`, + `0.333`, + } { + var doc struct { + XMLName xml.Name `xml:"account"` + Amount Decimal `xml:"amount"` + } + err := xml.Unmarshal([]byte(testCase), &doc) + if err == nil { + t.Errorf("expected error, got %+v", doc) + } + } +} + func TestDecimal_rescale(t *testing.T) { type Inp struct { int int64