From 48ac2640d03e89a22b2adb0060c38b66d241f68d Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 7 Dec 2025 14:14:37 -0600 Subject: [PATCH] add tests for error cases --- internal/processor/float-parse_test.go | 35 ++++++++++++++++++++++++ internal/processor/int-parse_test.go | 35 ++++++++++++++++++++++++ internal/processor/string-decode_test.go | 30 ++++++++++++++++++++ internal/processor/string-encode_test.go | 30 ++++++++++++++++++++ internal/processor/uint-parse_test.go | 35 ++++++++++++++++++++++++ 5 files changed, 165 insertions(+) diff --git a/internal/processor/float-parse_test.go b/internal/processor/float-parse_test.go index f8b97ff..99c04a1 100644 --- a/internal/processor/float-parse_test.go +++ b/internal/processor/float-parse_test.go @@ -48,3 +48,38 @@ func TestGoodFloatParse(t *testing.T) { }) } } + +func TestBadFloatParse(t *testing.T) { + floatParser := processor.FloatParse{} + tests := []struct { + processor processor.Processor + name string + payload any + errorString string + }{ + { + name: "non-string input", + payload: []byte{0x01}, + errorString: "float.parse processor only accepts a string", + }, + { + name: "not float string", + payload: "abcd", + errorString: "strconv.ParseFloat: parsing \"abcd\": invalid syntax", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := floatParser.Process(t.Context(), test.payload) + + if err == nil { + t.Errorf("float.parse expected to fail but succeeded, got: %v", got) + + } + if err.Error() != test.errorString { + t.Errorf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/processor/int-parse_test.go b/internal/processor/int-parse_test.go index 9c66b1b..5b76165 100644 --- a/internal/processor/int-parse_test.go +++ b/internal/processor/int-parse_test.go @@ -48,3 +48,38 @@ func TestGoodIntParse(t *testing.T) { }) } } + +func TestBadIntParse(t *testing.T) { + intParser := processor.IntParse{} + tests := []struct { + processor processor.Processor + name string + payload any + errorString string + }{ + { + name: "non-string input", + payload: []byte{0x01}, + errorString: "int.parse processor only accepts a string", + }, + { + name: "not int string", + payload: "123.46", + errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := intParser.Process(t.Context(), test.payload) + + if err == nil { + t.Errorf("int.parse expected to fail but succeeded, got: %v", got) + + } + if err.Error() != test.errorString { + t.Errorf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/processor/string-decode_test.go b/internal/processor/string-decode_test.go index f2aca97..d994b54 100644 --- a/internal/processor/string-decode_test.go +++ b/internal/processor/string-decode_test.go @@ -39,3 +39,33 @@ func TestGoodStringDecode(t *testing.T) { }) } } + +func TestBadStringDecode(t *testing.T) { + stringDecoder := processor.StringDecode{} + tests := []struct { + processor processor.Processor + name string + payload any + errorString string + }{ + { + processor: &stringDecoder, + name: "non-[]byte input", + payload: "hello", + errorString: "string.decode processor only accepts a []byte", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := test.processor.Process(t.Context(), test.payload) + + if err == nil { + t.Errorf("string.decode expected to fail but got payload: %s", got) + } + if err.Error() != test.errorString { + t.Errorf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/processor/string-encode_test.go b/internal/processor/string-encode_test.go index cc34351..90e1647 100644 --- a/internal/processor/string-encode_test.go +++ b/internal/processor/string-encode_test.go @@ -40,3 +40,33 @@ func TestGoodStringEncode(t *testing.T) { }) } } + +func TestBadStringEncode(t *testing.T) { + stringEncoder := processor.StringEncode{} + tests := []struct { + processor processor.Processor + name string + payload any + errorString string + }{ + { + processor: &stringEncoder, + name: "non-string input", + payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f}, + errorString: "string.encode processor only accepts a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := test.processor.Process(t.Context(), test.payload) + + if err == nil { + t.Errorf("string.encode expected to fail but got payload: %s", got) + } + if err.Error() != test.errorString { + t.Errorf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/processor/uint-parse_test.go b/internal/processor/uint-parse_test.go index 475bbf3..7ca4578 100644 --- a/internal/processor/uint-parse_test.go +++ b/internal/processor/uint-parse_test.go @@ -43,3 +43,38 @@ func TestGoodUintParse(t *testing.T) { }) } } + +func TestBadUintParse(t *testing.T) { + uintParser := processor.UintParse{} + tests := []struct { + processor processor.Processor + name string + payload any + errorString string + }{ + { + name: "non-string input", + payload: []byte{0x01}, + errorString: "uint.parse processor only accepts a string", + }, + { + name: "not uint string", + payload: "-1234", + errorString: "strconv.ParseUint: parsing \"-1234\": invalid syntax", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := uintParser.Process(t.Context(), test.payload) + + if err == nil { + t.Errorf("uint.parse expected to fail but succeeded, got: %v", got) + + } + if err.Error() != test.errorString { + t.Errorf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +}