MCPcopy
hub / github.com/shopspring/decimal / TestJSON

Function TestJSON

decimal_test.go:645–679  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

643}
644
645func TestJSON(t *testing.T) {
646 for _, x := range testTable {
647 s := x.short
648 var doc struct {
649 Amount Decimal `json:"amount"`
650 }
651 docStr := `{"amount":"` + s + `"}`
652 docStrNumber := `{"amount":` + s + `}`
653 err := json.Unmarshal([]byte(docStr), &doc)
654 if err != nil {
655 t.Errorf("error unmarshaling %s: %v", docStr, err)
656 } else if doc.Amount.String() != s {
657 t.Errorf("expected %s, got %s (%s, %d)",
658 s, doc.Amount.String(),
659 doc.Amount.value.String(), doc.Amount.exp)
660 }
661
662 out, err := json.Marshal(&doc)
663 if err != nil {
664 t.Errorf("error marshaling %+v: %v", doc, err)
665 } else if string(out) != docStr {
666 t.Errorf("expected %s, got %s", docStr, string(out))
667 }
668
669 // make sure unquoted marshalling works too
670 MarshalJSONWithoutQuotes = true
671 out, err = json.Marshal(&doc)
672 if err != nil {
673 t.Errorf("error marshaling %+v: %v", doc, err)
674 } else if string(out) != docStrNumber {
675 t.Errorf("expected %s, got %s", docStrNumber, string(out))
676 }
677 MarshalJSONWithoutQuotes = false
678 }
679}
680
681func TestUnmarshalJSONNull(t *testing.T) {
682 var doc struct {

Callers

nothing calls this directly

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…