diff options
author | Paulo L F Casaretto <pcasaretto@gmail.com> | 2016-04-22 10:33:24 +0800 |
---|---|---|
committer | Paulo L F Casaretto <pcasaretto@gmail.com> | 2016-04-26 20:12:36 +0800 |
commit | a20d3fc3625c8ccdd025e0dff96cde5d48586b08 (patch) | |
tree | 6e4f9f2c78abeba790b7940d175ffb79a17c04f3 /common/types_test.go | |
parent | 18580e152c1a2480b6245ebba4c62c202ed20ac6 (diff) | |
download | dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.tar dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.tar.gz dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.tar.bz2 dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.tar.lz dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.tar.xz dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.tar.zst dexon-a20d3fc3625c8ccdd025e0dff96cde5d48586b08.zip |
common: Add tests for Address#UnmarshalJSON
Diffstat (limited to 'common/types_test.go')
-rw-r--r-- | common/types_test.go | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/common/types_test.go b/common/types_test.go index f2dfbf0c9..de67cfcb5 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -16,7 +16,10 @@ package common -import "testing" +import ( + "math/big" + "testing" +) func TestBytesConversion(t *testing.T) { bytes := []byte{5} @@ -47,7 +50,38 @@ func TestHashJsonValidation(t *testing.T) { } for i, test := range tests { if err := h.UnmarshalJSON(append([]byte(test.Prefix), make([]byte, test.Size)...)); err != test.Error { - t.Error(i, "expected", test.Error, "got", err) + t.Errorf("test #%d: error mismatch: have %v, want %v", i, err, test.Error) + } + } +} + +func TestAddressUnmarshalJSON(t *testing.T) { + var a Address + var tests = []struct { + Input string + ShouldErr bool + Output *big.Int + }{ + {"", true, nil}, + {`""`, true, nil}, + {`"0x"`, true, nil}, + {`"0x00"`, true, nil}, + {`"0xG000000000000000000000000000000000000000"`, true, nil}, + {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, + {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, + } + for i, test := range tests { + err := a.UnmarshalJSON([]byte(test.Input)) + if err != nil && !test.ShouldErr { + t.Errorf("test #%d: unexpected error: %v", i, err) + } + if err == nil { + if test.ShouldErr { + t.Errorf("test #%d: expected error, got none", i) + } + if a.Big().Cmp(test.Output) != 0 { + t.Errorf("test #%d: address mismatch: have %v, want %v", i, a.Big(), test.Output) + } } } } |