diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-04-26 21:13:49 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-04-26 21:13:49 +0800 |
commit | 5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d (patch) | |
tree | 6894e7c69ccf01249f29e5a7490399062e8bf8ce | |
parent | 3d6d828caf2dbf2e7ceeeb6f0d38c3b654aefcd2 (diff) | |
parent | a20d3fc3625c8ccdd025e0dff96cde5d48586b08 (diff) | |
download | dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.tar dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.tar.gz dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.tar.bz2 dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.tar.lz dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.tar.xz dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.tar.zst dexon-5137c04ccfff5a4ffd522eed5fbc06cf4b6bfc6d.zip |
Merge pull request #2484 from pcasaretto/address-test-coverage
Improve Address test coverage
-rw-r--r-- | common/types.go | 2 | ||||
-rw-r--r-- | common/types_test.go | 38 |
2 files changed, 37 insertions, 3 deletions
diff --git a/common/types.go b/common/types.go index fec986164..d00884484 100644 --- a/common/types.go +++ b/common/types.go @@ -167,7 +167,7 @@ func (a Address) MarshalJSON() ([]byte, error) { // Parse address from raw json data func (a *Address) UnmarshalJSON(data []byte) error { if len(data) > 2 && data[0] == '"' && data[len(data)-1] == '"' { - data = data[:len(data)-1][1:] + data = data[1 : len(data)-1] } if len(data) > 2 && data[0] == '0' && data[1] == 'x' { 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) + } } } } |