From f68312ee8400fcfcc6ebc4f8ac9b4f7e346a7f80 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 2 Mar 2026 12:34:46 -0600 Subject: [PATCH] standardize layout of bad tests --- internal/processor/test/float-parse_test.go | 68 ++++----------- internal/processor/test/int-parse_test.go | 97 ++++++--------------- internal/processor/test/uint-parse_test.go | 97 ++++++--------------- 3 files changed, 65 insertions(+), 197 deletions(-) diff --git a/internal/processor/test/float-parse_test.go b/internal/processor/test/float-parse_test.go index 001fef6..7af5a62 100644 --- a/internal/processor/test/float-parse_test.go +++ b/internal/processor/test/float-parse_test.go @@ -26,59 +26,6 @@ func TestFloatParseFromRegistry(t *testing.T) { } } -func TestFloatParseBadConfigBitSizeString(t *testing.T) { - registration, ok := processor.ProcessorRegistry["float.parse"] - if !ok { - t.Fatalf("float.parse processor not registered") - } - - _, err := registration.New(config.ProcessorConfig{ - Type: "float.parse", - Params: map[string]any{ - "bitSize": "64", - }, - }) - - if err == nil { - t.Fatalf("float.parse should have returned an error for bad bitSize config") - } -} - -func TestFloatParseGoodConfig(t *testing.T) { - registration, ok := processor.ProcessorRegistry["float.parse"] - if !ok { - t.Fatalf("float.parse processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "float.parse", - Params: map[string]any{ - "bitSize": 64.0, - }, - }) - - if err != nil { - t.Fatalf("float.parse should have created processor but got error: %s", err) - } - - payload := "12345.0" - expected := float64(12345.0) - - got, err := processorInstance.Process(t.Context(), payload) - if err != nil { - t.Fatalf("float.parse processing failed: %s", err) - } - - gotFloat, ok := got.(float64) - if !ok { - t.Fatalf("float.parse returned a %T payload: %s", got, got) - } - - if gotFloat != expected { - t.Fatalf("float.parse got %f, expected %f", gotFloat, expected) - } -} - func TestGoodFloatParse(t *testing.T) { tests := []struct { name string @@ -151,6 +98,14 @@ func TestBadFloatParse(t *testing.T) { payload any errorString string }{ + { + name: "non-string bitSize", + params: map[string]any{ + "bitSize": "32", + }, + payload: "1.23", + errorString: "float.parse bitSize error: not a number", + }, { name: "non-string input", params: map[string]any{ @@ -189,6 +144,13 @@ func TestBadFloatParse(t *testing.T) { Params: test.params, }) + if err != nil { + if err.Error() != test.errorString { + t.Fatalf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + got, err := processorInstance.Process(t.Context(), test.payload) if err == nil { diff --git a/internal/processor/test/int-parse_test.go b/internal/processor/test/int-parse_test.go index d36ce91..31b7071 100644 --- a/internal/processor/test/int-parse_test.go +++ b/internal/processor/test/int-parse_test.go @@ -26,78 +26,6 @@ func TestIntParseFromRegistry(t *testing.T) { } } -func TestIntParseBadConfigBaseString(t *testing.T) { - registration, ok := processor.ProcessorRegistry["int.parse"] - if !ok { - t.Fatalf("int.parse processor not registered") - } - - _, err := registration.New(config.ProcessorConfig{ - Type: "int.parse", - Params: map[string]any{ - "base": "10", - }, - }) - - if err == nil { - t.Fatalf("int.parse should have returned an error for bad base config") - } -} - -func TestIntParseBadConfigBitSizeString(t *testing.T) { - registration, ok := processor.ProcessorRegistry["int.parse"] - if !ok { - t.Fatalf("int.parse processor not registered") - } - - _, err := registration.New(config.ProcessorConfig{ - Type: "int.parse", - Params: map[string]any{ - "bitSize": "64", - }, - }) - - if err == nil { - t.Fatalf("int.parse should have returned an error for bad bitSize config") - } -} - -func TestIntParseGoodConfig(t *testing.T) { - registration, ok := processor.ProcessorRegistry["int.parse"] - if !ok { - t.Fatalf("int.parse processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "int.parse", - Params: map[string]any{ - "base": 10.0, - "bitSize": 64.0, - }, - }) - - if err != nil { - t.Fatalf("int.parse should have created processor but got error: %s", err) - } - - payload := "12345" - expected := int64(12345) - - got, err := processorInstance.Process(t.Context(), payload) - if err != nil { - t.Fatalf("int.parse processing failed: %s", err) - } - - gotInt, ok := got.(int64) - if !ok { - t.Fatalf("int.parse returned a %T payload: %s", got, got) - } - - if gotInt != expected { - t.Fatalf("int.parse got %d, expected %d", gotInt, expected) - } -} - func TestGoodIntParse(t *testing.T) { tests := []struct { name string @@ -191,6 +119,24 @@ func TestBadIntParse(t *testing.T) { payload any errorString string }{ + { + name: "non-string base", + params: map[string]any{ + "base": "10", + "bitSize": 64, + }, + payload: "12345", + errorString: "int.parse base error: not a number", + }, + { + name: "non-string bitSize", + params: map[string]any{ + "base": 10, + "bitSize": "64", + }, + payload: "12345", + errorString: "int.parse bitSize error: not a number", + }, { name: "non-string input", params: map[string]any{ @@ -232,6 +178,13 @@ func TestBadIntParse(t *testing.T) { Params: test.params, }) + if err != nil { + if err.Error() != test.errorString { + t.Fatalf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + got, err := processorInstance.Process(t.Context(), test.payload) if err == nil { diff --git a/internal/processor/test/uint-parse_test.go b/internal/processor/test/uint-parse_test.go index d88dfa4..162c48c 100644 --- a/internal/processor/test/uint-parse_test.go +++ b/internal/processor/test/uint-parse_test.go @@ -26,78 +26,6 @@ func TestUintParseFromRegistry(t *testing.T) { } } -func TestUintParseBadConfigBaseString(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.parse"] - if !ok { - t.Fatalf("uint.parse processor not registered") - } - - _, err := registration.New(config.ProcessorConfig{ - Type: "uint.parse", - Params: map[string]any{ - "base": "10", - }, - }) - - if err == nil { - t.Fatalf("uint.parse should have returned an error for bad base config") - } -} - -func TestUintParseBadConfigBitSizeString(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.parse"] - if !ok { - t.Fatalf("uint.parse processor not registered") - } - - _, err := registration.New(config.ProcessorConfig{ - Type: "uint.parse", - Params: map[string]any{ - "bitSize": "64", - }, - }) - - if err == nil { - t.Fatalf("uint.parse should have returned an error for bad bitSize config") - } -} - -func TestUintParseGoodConfig(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.parse"] - if !ok { - t.Fatalf("uint.parse processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.parse", - Params: map[string]any{ - "base": 10.0, - "bitSize": 64.0, - }, - }) - - if err != nil { - t.Fatalf("uint.parse should have created processor but got error: %s", err) - } - - payload := "12345" - expected := uint64(12345) - - got, err := processorInstance.Process(t.Context(), payload) - if err != nil { - t.Fatalf("uint.parse processing failed: %s", err) - } - - gotUint, ok := got.(uint64) - if !ok { - t.Fatalf("uint.parse returned a %T payload: %s", got, got) - } - - if gotUint != expected { - t.Fatalf("uint.parse got %d, expected %d", gotUint, expected) - } -} - func TestGoodUintParse(t *testing.T) { tests := []struct { @@ -171,6 +99,24 @@ func TestBadUintParse(t *testing.T) { payload any errorString string }{ + { + name: "non-string base", + params: map[string]any{ + "base": "10", + "bitSize": 64, + }, + payload: "12345", + errorString: "uint.parse base error: not a number", + }, + { + name: "non-string bitSize", + params: map[string]any{ + "base": 10, + "bitSize": "64", + }, + payload: "12345", + errorString: "uint.parse bitSize error: not a number", + }, { name: "non-string input", params: map[string]any{"base": 10, "bitSize": 64}, @@ -203,6 +149,13 @@ func TestBadUintParse(t *testing.T) { Params: test.params, }) + if err != nil { + if err.Error() != test.errorString { + t.Fatalf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + got, err := processorInstance.Process(t.Context(), test.payload) if err == nil {