add more tests for bad GetAnyAs scenarios

This commit is contained in:
Joel Wetzell
2026-03-22 22:47:43 -05:00
parent a0f3ee3b05
commit 5bb3f08006

View File

@@ -58,8 +58,12 @@ func TestBadGetAnyAsInt(t *testing.T) {
value: "value", value: "value",
}, },
{ {
name: "float with decimal", name: "float32 with decimal",
value: 1.5, 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) { func TestGoodGetAnyAsByteSlice(t *testing.T) {
testCases := []struct { testCases := []struct {
name string 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)
}
})
}
}