shapes: transform to a table test

This commit is contained in:
vinchent 2024-09-11 09:14:28 +02:00
parent d708e70352
commit 595ed65745

View File

@ -13,24 +13,18 @@ func TestPerimeter(t *testing.T) {
}
func TestArea(t *testing.T) {
checkArea := func(t testing.TB, shape Shape, exp float64) {
t.Helper()
got := shape.Area()
if got != exp {
t.Errorf("got %.2f expected %.2f", got, exp)
}
areaTests := []struct {
shape Shape
exp float64
}{
{Rectangle{30.5, 20.5}, 625.25},
{Circle{10}, 314.1592653589793},
}
t.Run("rectangles", func(t *testing.T) {
rectangle := Rectangle{30.5, 20.5}
exp := 625.25
checkArea(t, rectangle, exp)
})
t.Run("circles", func(t *testing.T) {
circle := Circle{10}
exp := 314.1592653589793
checkArea(t, circle, exp)
})
for _, test := range areaTests {
got := test.shape.Area()
if got != test.exp {
t.Errorf("got %.2f expected %.2f", got, test.exp)
}
}
}