master
1package main
2
3import (
4 "testing"
5)
6
7func TestPerimeter(t *testing.T) {
8 rectangle := Rectangle{10.0, 10.0}
9 got := Perimeter(rectangle)
10 want := 40.0
11
12 if got != want {
13 t.Errorf("got %.2f want %.2f", got, want)
14 }
15}
16
17func TestArea(t *testing.T) {
18 tests := []struct {
19 name string
20 shape Shape
21 expected float64
22 }{
23 {name: "rectangles", shape: Rectangle{Width: 12, Height: 6}, expected: 72.0},
24 {name: "circles", shape: Circle{Radius: 10}, expected: 314.1592653589793},
25 {name: "triangles", shape: Triangle{Base: 12, Height: 6}, expected: 36.0},
26 }
27
28 for _, test := range tests {
29 t.Run(test.name, func(t *testing.T) {
30 got := test.shape.Area()
31 if got != test.expected {
32 t.Errorf("got %g want %g", got, test.expected)
33 }
34 })
35 }
36}