|
package main |
|
|
|
import ( |
|
"fmt" |
|
"github.com/stretchr/testify/assert" |
|
"testing" |
|
) |
|
|
|
func TestSquareGetArea(t *testing.T) { |
|
sq := Square{sideLength: 5} |
|
a := sq.getArea() |
|
expected := float64(25) |
|
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestSquareGetAreaWithFloats(t *testing.T) { |
|
sq := Square{sideLength: 5.5} |
|
a := sq.getArea() |
|
expected := float64(30.25) |
|
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestTriangleGetArea(t *testing.T) { |
|
tri := Triangle{base: 5, height: 10} |
|
a := tri.getArea() |
|
expected := float64(25) |
|
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestTriangleGetAreaWithFloats(t *testing.T) { |
|
tri := Triangle{base: 5.2, height: 10.2} |
|
a := tri.getArea() |
|
expected := float64(26.52) |
|
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestNewSquareAcceptsIntegers(t *testing.T) { |
|
sq, _ := NewSquare(2) |
|
a := sq.getArea() |
|
expected := float64(4) |
|
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestNewSquareAcceptsFloats(t *testing.T) { |
|
sq, _ := NewSquare(5.5) |
|
a := sq.getArea() |
|
expected := float64(30.25) |
|
fm := fmt.Sprintf("Expected area of square to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestNewSquareDoesNotAcceptNegativeValues(t *testing.T) { |
|
_, err := NewSquare(-2) |
|
expected := "squares cannot have negative values" |
|
assert.Equal(t, expected, err.Error(), "Unexpected exception") |
|
} |
|
|
|
func TestNewTriangleAcceptsBaseInteger(t *testing.T) { |
|
tri, _ := NewTriangle(5, 10.5) |
|
a := tri.getArea() |
|
expected := float64(26.25) |
|
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestNewTriangleAcceptsHeightInteger(t *testing.T) { |
|
tri, _ := NewTriangle(5.5, 10) |
|
a := tri.getArea() |
|
expected := float64(27.5) |
|
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestNewTriangleAcceptsFloats(t *testing.T) { |
|
tri, _ := NewTriangle(5.5, 10.5) |
|
a := tri.getArea() |
|
expected := float64(28.875) |
|
fm := fmt.Sprintf("Expected area of triangle to be %f, but got %f instead", expected, a) |
|
assert.Equal(t, expected, a, fm) |
|
} |
|
|
|
func TestNewTriangleDoesNotAcceptNegativeBaseValues(t *testing.T) { |
|
_, err := NewTriangle(-2, 5) |
|
expected := "triangles cannot have negative values" |
|
assert.Equal(t, expected, err.Error(), "Unexpected exception") |
|
} |
|
|
|
func TestNewTriangleDoesNotAcceptNegativeHeightValues(t *testing.T) { |
|
_, err := NewTriangle(2, -5) |
|
expected := "triangles cannot have negative values" |
|
assert.Equal(t, expected, err.Error(), "Unexpected exception") |
|
} |
|
|
|
func TestNewTriangleDoesNotAcceptNegativeValues(t *testing.T) { |
|
_, err := NewTriangle(-2, -5) |
|
expected := "triangles cannot have negative values" |
|
assert.Equal(t, expected, err.Error(), "Unexpected exception") |
|
} |