go-by-test/shapes/shapes_test.go

35 lines
785 B
Go
Raw Normal View History

package shapes
import "testing"
func TestPerimeter(t *testing.T) {
2024-09-11 06:58:04 +00:00
rectangle := Rectangle{30.5, 20.5}
2024-09-11 07:03:57 +00:00
got := rectangle.Perimeter()
exp := 102.0
if got != exp {
t.Errorf("got %.2f expected %.2f", got, exp)
}
}
func TestArea(t *testing.T) {
2024-09-11 07:14:28 +00:00
areaTests := []struct {
2024-09-11 07:19:05 +00:00
name string
shape Shape
hasArea float64
2024-09-11 07:14:28 +00:00
}{
2024-09-11 07:19:05 +00:00
{name: "Rectangle", shape: Rectangle{Width: 30.5, Height: 20.5}, hasArea: 625.25},
{name: "Circle", shape: Circle{Radius: 10}, hasArea: 314.1592653589793},
{name: "Triangle", shape: Triangle{Width: 12, Height: 6}, hasArea: 36.0},
2024-09-11 07:14:28 +00:00
}
2024-09-11 07:14:28 +00:00
for _, test := range areaTests {
2024-09-11 07:19:05 +00:00
t.Run(test.name, func(t *testing.T) {
got := test.shape.Area()
if got != test.hasArea {
t.Errorf("%#v got %g expected %g", test.shape, got, test.hasArea)
}
})
2024-09-11 07:10:19 +00:00
}
}