add unit test

This commit is contained in:
2019-08-01 01:52:00 +02:00
parent 41b2d8ebc0
commit b5be9fd2ac
4 changed files with 232 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
package hamster_tycoon
import (
"errors"
"testing"
)
func TestDie(t *testing.T) {
testCases := []struct {
caseName string
hamster *Hamster
expectedAlive bool
}{
{
caseName: "Should die",
hamster: &Hamster{
Alive: true,
},
expectedAlive: false,
},
{
caseName: "Already die",
hamster: &Hamster{
Alive: false,
},
expectedAlive: false,
},
}
for _, tc := range testCases {
t.Run(tc.caseName, func(t *testing.T) {
tc.hamster.Die()
if tc.hamster.Alive != tc.expectedAlive {
t.Errorf("Die result does not match expectation. \n Got : %t \n Get : %t", tc.expectedAlive, tc.hamster.Alive)
}
})
}
}
func TestFuck(t *testing.T) {
testCases := []struct {
caseName string
hamster1 *Hamster
hamster2 *Hamster
expectedResult bool
expectedError error
}{
{
caseName: "Hamster 1 too young",
hamster1: &Hamster{
Alive: true,
Sexe: MALE,
Age: GestationMinAge - 1,
},
hamster2: &Hamster{
Alive: true,
Sexe: FEMALE,
Age: GestationMinAge + 1,
},
expectedResult: false,
expectedError: errors.New("one of the hamster is too young"),
},
{
caseName: "Hamster 2 too young",
hamster1: &Hamster{
Alive: false,
Sexe: MALE,
Age: GestationMinAge + 1,
},
hamster2: &Hamster{
Alive: true,
Sexe: FEMALE,
Age: GestationMinAge - 1,
},
expectedResult: false,
expectedError: errors.New("one of the hamster is too young"),
},
}
for _, tc := range testCases {
t.Run(tc.caseName, func(t *testing.T) {
bool, err := tc.hamster1.Fuck(tc.hamster2)
if tc.expectedResult != bool {
t.Errorf("Fuck result does not match expectation. \n Got : %t \n Get : %t", tc.expectedResult, bool)
}
if tc.expectedError.Error() != err.Error() {
t.Errorf("Fuck result does not match expectation. \n Got : %s \n Get : %s", tc.expectedError, err)
}
})
}
}