shapes: add Shape interface
This commit is contained in:
		| @ -2,6 +2,10 @@ package shapes | ||||
|  | ||||
| import "math" | ||||
|  | ||||
| type Shape interface { | ||||
| 	Area() float64 | ||||
| } | ||||
|  | ||||
| type Rectangle struct { | ||||
| 	Width  float64 | ||||
| 	Height float64 | ||||
|  | ||||
| @ -13,23 +13,24 @@ func TestPerimeter(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestArea(t *testing.T) { | ||||
| 	t.Run("rectangles", func(t *testing.T) { | ||||
| 		rectangle := Rectangle{30.5, 20.5} | ||||
| 		got := rectangle.Area() | ||||
| 		exp := 625.25 | ||||
| 	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) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	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} | ||||
| 		got := circle.Area() | ||||
| 		exp := 314.1592653589793 | ||||
|  | ||||
| 		if got != exp { | ||||
| 			t.Errorf("got %.2f expected %.2f", got, exp) | ||||
| 		} | ||||
| 		checkArea(t, circle, exp) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user