Add JSON unmarshallers for numeric types from util (#83)

Uint160, Uint256, Fixed8 now have UnmarshalJSON method.
This commit is contained in:
Evgenii Stratonikov 2018-05-09 08:20:16 +03:00 committed by Anthony De Meulemeester
parent 35551282b0
commit 1d9045877c
8 changed files with 110 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package util
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
@ -18,7 +19,7 @@ func TestNewFixed8(t *testing.T) {
func TestFixed8DecodeString(t *testing.T) {
// Fixed8DecodeString works correctly with integers
ivalues := []string{"9000", "100000000", "5", "10945"}
for _, val:= range ivalues {
for _, val := range ivalues {
n, err := Fixed8DecodeString(val)
assert.Nil(t, err)
assert.Equal(t, val, n.String())
@ -36,3 +37,21 @@ func TestFixed8DecodeString(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, Fixed8(90123410000), n)
}
func TestFixed8UnmarshalJSON(t *testing.T) {
fl := float64(123.45)
str := "123.45"
expected, _ := Fixed8DecodeString(str)
// UnmarshalJSON should decode floats
var u1 Fixed8
s, _ := json.Marshal(fl)
assert.Nil(t, json.Unmarshal(s, &u1))
assert.Equal(t, expected, u1)
// UnmarshalJSON should decode strings
var u2 Fixed8
s, _ = json.Marshal(str)
assert.Nil(t, json.Unmarshal(s, &u2))
assert.Equal(t, expected, u2)
}