From 5bb3f080066a0963e53d53b0eba9269ead3abdaa Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 22 Mar 2026 22:47:43 -0500 Subject: [PATCH] add more tests for bad GetAnyAs scenarios --- internal/common/common_test.go | 212 ++++++++++++++++++++++++++++++++- 1 file changed, 210 insertions(+), 2 deletions(-) diff --git a/internal/common/common_test.go b/internal/common/common_test.go index 28286fe..2e196ae 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -58,8 +58,12 @@ func TestBadGetAnyAsInt(t *testing.T) { value: "value", }, { - name: "float with decimal", - value: 1.5, + name: "float32 with decimal", + value: float32(1.5), + }, + { + name: "float64 with decimal", + value: float64(1.5), }, } @@ -73,6 +77,76 @@ func TestBadGetAnyAsInt(t *testing.T) { } } +func TestGoodGetAnyAsByte(t *testing.T) { + testCases := []struct { + name string + value any + typedValue byte + }{ + { + name: "int", + value: int(42), + typedValue: 42, + }, + { + name: "uint", + value: uint(42), + typedValue: 42, + }, + { + name: "float32 without decimal", + value: float32(42.0), + typedValue: 42, + }, + { + name: "float64 without decimal", + value: float64(42.0), + typedValue: 42, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + value, ok := common.GetAnyAsByte(testCase.value) + if !ok { + t.Fatalf("GetAnyAsByte expected to succeed but failed") + } + if value != testCase.typedValue { + t.Fatalf("GetAnyAsByte expected got %d, expected %d", value, testCase.typedValue) + } + }) + } +} + +func TestBadGetAnyAsByte(t *testing.T) { + testCases := []struct { + name string + value any + }{ + { + name: "string", + value: "value", + }, + { + name: "float32 with decimal", + value: float32(1.5), + }, + { + name: "float64 with decimal", + value: float64(1.5), + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + value, ok := common.GetAnyAsByte(testCase.value) + if ok { + t.Fatalf("GetAnyAsByte expected to fail but succeeded, got: %v", value) + } + }) + } +} + func TestGoodGetAnyAsByteSlice(t *testing.T) { testCases := []struct { name string @@ -214,3 +288,137 @@ func TestBadGetAnyAsIntSlice(t *testing.T) { }) } } + +func TestGoodGetAnyAsFloat32(t *testing.T) { + testCases := []struct { + name string + value any + typedValue float32 + }{ + { + name: "int", + value: int(42), + typedValue: 42, + }, + { + name: "uint", + value: uint(42), + typedValue: 42, + }, + { + name: "byte", + value: byte(42), + typedValue: 42, + }, + { + name: "float32", + value: float32(42.3), + typedValue: 42.3, + }, + { + name: "float64", + value: float64(42.3), + typedValue: 42.3, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + value, ok := common.GetAnyAsFloat32(testCase.value) + if !ok { + t.Fatalf("GetAnyAsFloat32 expected to succeed but failed") + } + if value != testCase.typedValue { + t.Fatalf("GetAnyAsFloat32 expected got %f, expected %f", value, testCase.typedValue) + } + }) + } +} + +func TestBadGetAnyAsFloat32(t *testing.T) { + testCases := []struct { + name string + value any + }{ + { + name: "string", + value: "value", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + value, ok := common.GetAnyAsFloat32(testCase.value) + if ok { + t.Fatalf("GetAnyAsFloat32 expected to fail but succeeded, got: %v", value) + } + }) + } +} + +func TestGoodGetAnyAsFloat64(t *testing.T) { + testCases := []struct { + name string + value any + typedValue float64 + }{ + { + name: "int", + value: int(42), + typedValue: 42, + }, + { + name: "uint", + value: uint(42), + typedValue: 42, + }, + { + name: "byte", + value: byte(42), + typedValue: 42, + }, + { + name: "float32", + value: float32(42.5), + typedValue: 42.5, + }, + { + name: "float64", + value: float64(42.5), + typedValue: 42.5, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + value, ok := common.GetAnyAsFloat64(testCase.value) + if !ok { + t.Fatalf("GetAnyAsFloat64 expected to succeed but failed") + } + if value != testCase.typedValue { + t.Fatalf("GetAnyAsFloat64 expected got %f, expected %f", value, testCase.typedValue) + } + }) + } +} + +func TestBadGetAnyAsFloat64(t *testing.T) { + testCases := []struct { + name string + value any + }{ + { + name: "string", + value: "value", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + value, ok := common.GetAnyAsFloat64(testCase.value) + if ok { + t.Fatalf("GetAnyAsFloat64 expected to fail but succeeded, got: %v", value) + } + }) + } +}