mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Add round-trip JSON test.
This commit is contained in:
parent
9fc32a450f
commit
a2664e0073
1 changed files with 25 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
||||||
package decimal
|
package decimal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"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) {
|
func TestDecimal_rescale(t *testing.T) {
|
||||||
type Inp struct {
|
type Inp struct {
|
||||||
int int64
|
int int64
|
||||||
|
|
Loading…
Reference in a new issue