]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/hex: improve tests
authorAgniva De Sarker <agnivade@yahoo.co.in>
Sun, 13 Aug 2017 16:40:49 +0000 (22:10 +0530)
committerIan Lance Taylor <iant@golang.org>
Mon, 14 Aug 2017 04:45:52 +0000 (04:45 +0000)
The tests for error scenarios were done by manually checking
error strings. Improved them by checking the actual error type
instead of just the string.

Printing the actual error in case of failure instead of a
generic string.

Also added a new scenario with both an invalid byte and an
invalid length string to verify that the length is checked first
before doing any computation.

Change-Id: Ic2a19a6d6058912632d597590186ee2d8348cb45
Reviewed-on: https://go-review.googlesource.com/55256
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/hex/hex_test.go

index 64dabbd10a20af21e4cd681f09d5cb676b6d8c5d..e6dc765c9578fd40bd67d73a954f94b7a69651f9 100644 (file)
@@ -77,14 +77,15 @@ func TestDecodeString(t *testing.T) {
 
 type errTest struct {
        in  string
-       err string
+       err error
 }
 
 var errTests = []errTest{
-       {"0", "encoding/hex: odd length hex string"},
-       {"0g", "encoding/hex: invalid byte: U+0067 'g'"},
-       {"00gg", "encoding/hex: invalid byte: U+0067 'g'"},
-       {"0\x01", "encoding/hex: invalid byte: U+0001"},
+       {"0", ErrLength},
+       {"zd4aa", ErrLength},
+       {"0g", InvalidByteError('g')},
+       {"00gg", InvalidByteError('g')},
+       {"0\x01", InvalidByteError('\x01')},
 }
 
 func TestInvalidErr(t *testing.T) {
@@ -92,8 +93,8 @@ func TestInvalidErr(t *testing.T) {
                dst := make([]byte, DecodedLen(len(test.in)))
                _, err := Decode(dst, []byte(test.in))
                if err == nil {
-                       t.Errorf("#%d: expected error; got none", i)
-               } else if err.Error() != test.err {
+                       t.Errorf("#%d: expected %v; got none", i, test.err)
+               } else if err != test.err {
                        t.Errorf("#%d: got: %v want: %v", i, err, test.err)
                }
        }
@@ -103,8 +104,8 @@ func TestInvalidStringErr(t *testing.T) {
        for i, test := range errTests {
                _, err := DecodeString(test.in)
                if err == nil {
-                       t.Errorf("#%d: expected error; got none", i)
-               } else if err.Error() != test.err {
+                       t.Errorf("#%d: expected %v; got none", i, test.err)
+               } else if err != test.err {
                        t.Errorf("#%d: got: %v want: %v", i, err, test.err)
                }
        }