shapes: use Rectangle as receiver

This commit is contained in:
vinchent 2024-09-11 09:03:57 +02:00
parent 5de98f3ac8
commit d5d910db5a
2 changed files with 21 additions and 9 deletions

View File

@ -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
}

View File

@ -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) {
t.Run("rectangles", func(t *testing.T) {
rectangle := Rectangle{30.5, 20.5}
got := Area(rectangle)
got := rectangle.Area()
exp := 625.25
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)
// }
// })
}