shapes: add Shape interface

This commit is contained in:
vinchent 2024-09-11 09:10:19 +02:00
parent ff89c50710
commit d708e70352
2 changed files with 14 additions and 9 deletions

View File

@ -2,6 +2,10 @@ package shapes
import "math" import "math"
type Shape interface {
Area() float64
}
type Rectangle struct { type Rectangle struct {
Width float64 Width float64
Height float64 Height float64

View File

@ -13,23 +13,24 @@ func TestPerimeter(t *testing.T) {
} }
func TestArea(t *testing.T) { func TestArea(t *testing.T) {
t.Run("rectangles", func(t *testing.T) { checkArea := func(t testing.TB, shape Shape, exp float64) {
rectangle := Rectangle{30.5, 20.5} t.Helper()
got := rectangle.Area() got := shape.Area()
exp := 625.25
if got != exp { if got != exp {
t.Errorf("got %.2f expected %.2f", got, exp) t.Errorf("got %.2f expected %.2f", got, exp)
} }
}
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) { t.Run("circles", func(t *testing.T) {
circle := Circle{10} circle := Circle{10}
got := circle.Area()
exp := 314.1592653589793 exp := 314.1592653589793
checkArea(t, circle, exp)
if got != exp {
t.Errorf("got %.2f expected %.2f", got, exp)
}
}) })
} }