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

@ -2,11 +2,29 @@ package util
import (
"encoding/hex"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUint256UnmarshalJSON(t *testing.T) {
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
expected, _ := Uint256DecodeString(str)
// UnmarshalJSON should decode hex-strings
var u1 Uint256
s, _ := json.Marshal(str)
assert.Nil(t, json.Unmarshal(s, &u1))
assert.True(t, expected.Equals(u1))
// UnmarshalJSON should decode hex-strings prefixed by 0x
var u2 Uint256
s, _ = json.Marshal("0x" + str)
assert.Nil(t, json.Unmarshal(s, &u2))
assert.True(t, expected.Equals(u2))
}
func TestUint256DecodeString(t *testing.T) {
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
val, err := Uint256DecodeString(hexStr)