From d5d910db5a82ff42ea605aacd7e20cc6d443ef8d Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 11 Sep 2024 09:03:57 +0200 Subject: [PATCH] shapes: use Rectangle as receiver --- shapes/shapes.go | 4 ++-- shapes/shapes_test.go | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/shapes/shapes.go b/shapes/shapes.go index 0ed7c22..6bc9d9a 100644 --- a/shapes/shapes.go +++ b/shapes/shapes.go @@ -5,10 +5,10 @@ type Rectangle struct { Height float64 } -func Perimeter(r Rectangle) float64 { +func (r Rectangle) Perimeter() float64 { return 2 * (r.Width + r.Height) } -func Area(r Rectangle) float64 { +func (r Rectangle) Area() float64 { return r.Width * r.Height } diff --git a/shapes/shapes_test.go b/shapes/shapes_test.go index 2308023..0c47a67 100644 --- a/shapes/shapes_test.go +++ b/shapes/shapes_test.go @@ -4,7 +4,7 @@ import "testing" func TestPerimeter(t *testing.T) { rectangle := Rectangle{30.5, 20.5} - got := Perimeter(rectangle) + got := rectangle.Perimeter() exp := 102.0 if got != exp { @@ -13,11 +13,23 @@ func TestPerimeter(t *testing.T) { } func TestArea(t *testing.T) { - rectangle := Rectangle{30.5, 20.5} - got := Area(rectangle) - exp := 625.25 + t.Run("rectangles", func(t *testing.T) { + rectangle := Rectangle{30.5, 20.5} + got := rectangle.Area() + exp := 625.25 - if got != exp { - t.Errorf("got %.2f expected %.2f", got, exp) - } + if got != exp { + t.Errorf("got %.2f expected %.2f", got, exp) + } + }) + + // t.Run("circles", func(t *testing.T) { + // circle := Circle{10} + // got := Area(circle) + // exp := 314.1592653589793 + // + // if got != exp { + // t.Errorf("got %.2f expected %.2f", got, exp) + // } + // }) }