From afb8154eab2961996b321ac3fe5a21602e3b1aff Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Mon, 4 Dec 2017 19:34:15 +0100 Subject: common: improve IsHexAddress and add tests (#15551) Also unexport isHex, hasHexPrefix because IsHexAddress is the only caller. Fixes #15550 --- common/bytes_test.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'common/bytes_test.go') diff --git a/common/bytes_test.go b/common/bytes_test.go index 71631e6dd..97dd34d15 100644 --- a/common/bytes_test.go +++ b/common/bytes_test.go @@ -34,19 +34,6 @@ func (s *BytesSuite) TestCopyBytes(c *checker.C) { c.Assert(res1, checker.DeepEquals, exp1) } -func (s *BytesSuite) TestIsHex(c *checker.C) { - data1 := "a9e67e" - exp1 := false - res1 := IsHex(data1) - c.Assert(res1, checker.DeepEquals, exp1) - - data2 := "0xa9e67e00" - exp2 := true - res2 := IsHex(data2) - c.Assert(res2, checker.DeepEquals, exp2) - -} - func (s *BytesSuite) TestLeftPadBytes(c *checker.C) { val1 := []byte{1, 2, 3, 4} exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4} @@ -78,6 +65,27 @@ func TestFromHex(t *testing.T) { } } +func TestIsHex(t *testing.T) { + tests := []struct { + input string + ok bool + }{ + {"", true}, + {"0", false}, + {"00", true}, + {"a9e67e", true}, + {"A9E67E", true}, + {"0xa9e67e", false}, + {"a9e67e001", false}, + {"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false}, + } + for _, test := range tests { + if ok := isHex(test.input); ok != test.ok { + t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok) + } + } +} + func TestFromHexOddLength(t *testing.T) { input := "0x1" expected := []byte{1} -- cgit