From a0f867aadc01ecd739a53c5549e0fd883cfd297d Mon Sep 17 00:00:00 2001 From: blinkthethings Date: Thu, 24 Dec 2020 22:45:15 -0500 Subject: [PATCH] Add error tests --- modhex_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/modhex_test.go b/modhex_test.go index edcd96f..52a79e9 100644 --- a/modhex_test.go +++ b/modhex_test.go @@ -87,3 +87,40 @@ func TestDecodeString(t *testing.T) { } } } + +type errTest struct { + in string + out string + err error +} + +var errTests = []errTest{ + {"", "", nil}, + {"c", "", ErrLength}, + {"zteff", "", InvalidByteError('z')}, + {"dtefz", "\x2d\x34", InvalidByteError('z')}, + {"ecebe", "01", ErrLength}, + {"cq", "", InvalidByteError('q')}, + {"ccqq", "\x00", InvalidByteError('q')}, + {"c\x01", "", InvalidByteError('\x01')}, + {"vvuut", "\xff\xee", ErrLength}, +} + +func TestDecodeErr(t *testing.T) { + for i, test := range errTests { + out := make([]byte, len(test.in)+10) + n, err := Decode(out, []byte(test.in)) + if string(out[:n]) != test.out || err != test.err { + t.Errorf("#%d: Decode(%q) = %q, %v, want %q, %v", i, test.in, string(out[:n]), err, test.out, test.err) + } + } +} + +func TestDecodeStringErr(t *testing.T) { + for i, test := range errTests { + out, err := DecodeString(test.in) + if string(out) != test.out || err != test.err { + t.Errorf("#%d: DecodeString(%q) = %q, %v, want %q, %v", i, test.in, out, err, test.out, test.err) + } + } +}