Commit 727fcdf

mo khan <mo.khan@gmail.com>
2019-10-27 21:39:32
Add test for Triangle
1 parent bf1fbf7
shapes.go
@@ -28,3 +28,12 @@ type Circle struct {
 func (c Circle) Area() float64 {
 	return math.Pi * c.Radius * c.Radius
 }
+
+type Triangle struct {
+	Base   float64
+	Height float64
+}
+
+func (t Triangle) Area() float64 {
+	return (t.Base * t.Height) * 0.5
+}
shapes_test.go
@@ -16,19 +16,20 @@ func TestPerimeter(t *testing.T) {
 
 func TestArea(t *testing.T) {
 	tests := []struct {
-		name  string
-		shape Shape
-		want  float64
+		name     string
+		shape    Shape
+		expected float64
 	}{
-		{"rectangles", Rectangle{12, 6}, 72.0},
-		{"circles", Circle{10}, 314.1592653589793},
+		{name: "rectangles", shape: Rectangle{Width: 12, Height: 6}, expected: 72.0},
+		{name: "circles", shape: Circle{Radius: 10}, expected: 314.1592653589793},
+		{name: "triangles", shape: Triangle{Base: 12, Height: 6}, expected: 36.0},
 	}
 
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {
 			got := test.shape.Area()
-			if got != test.want {
-				t.Errorf("got %g want %g", got, test.want)
+			if got != test.expected {
+				t.Errorf("got %g want %g", got, test.expected)
 			}
 		})
 	}