diff --git a/go.mod b/go.mod index b58e472..7feed86 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module hamster-tycoon go 1.13 -require github.com/DATA-DOG/godog v0.7.13 +require ( + github.com/DATA-DOG/godog v0.7.13 + github.com/cweill/gotests v1.5.3 // indirect + golang.org/x/tools v0.0.0-20190919180025-928b73f71f9b // indirect +) diff --git a/go.sum b/go.sum index a398a5a..4d16e54 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,12 @@ github.com/DATA-DOG/godog v0.7.13 h1:JmgpKcra7Vf3yzI9vPsWyoQRx13tyKziHtXWDCUUgok= github.com/DATA-DOG/godog v0.7.13/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0= +github.com/cweill/gotests v1.5.3 h1:k3t4wW/x/YNixWZJhUIn+mivmK5iV1tJVOwVYkx0UcU= +github.com/cweill/gotests v1.5.3/go.mod h1:XZYOJkGVkCRoymaIzmp9Wyi3rUgfA3oOnkuljYrjFV8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190919180025-928b73f71f9b h1:9AAzeQvkIceaqgDSYbwjqLDhy3sMXirFnNCGFOH8hhQ= +golang.org/x/tools v0.0.0-20190919180025-928b73f71f9b/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/hamster_tycoon/hamster.go b/hamster_tycoon/hamster.go index e907494..fdcde1b 100644 --- a/hamster_tycoon/hamster.go +++ b/hamster_tycoon/hamster.go @@ -3,7 +3,7 @@ package hamster_tycoon import ( "errors" "fmt" - "math/rand" + "hamster-tycoon/randomizer" ) const ( @@ -56,7 +56,7 @@ func (h *Hamster) Fuck(another *Hamster) (bool, error) { return false, errors.New("can't fuck together") } - if h.Age <= GestationMinAge || another.Age <= GestationMinAge { + if h.Age < GestationMinAge || another.Age < GestationMinAge { return false, errors.New("one of the hamster is too young") } @@ -128,5 +128,5 @@ func randSexe() string { } func randNumber(min int, max int) int { - return min + rand.Intn(max-min) + return min + randomizer.Rand(max-min) } diff --git a/hamster_tycoon/hamster_test.go b/hamster_tycoon/hamster_test.go index 5a57876..3069aca 100644 --- a/hamster_tycoon/hamster_test.go +++ b/hamster_tycoon/hamster_test.go @@ -2,6 +2,8 @@ package hamster_tycoon import ( "errors" + "hamster-tycoon/randomizer" + "reflect" "testing" ) @@ -45,6 +47,21 @@ func TestFuck(t *testing.T) { expectedResult bool expectedError error }{ + { + caseName: "Same sexe", + hamster1: &Hamster{ + Alive: true, + Sexe: MALE, + Age: GestationMinAge - 1, + }, + hamster2: &Hamster{ + Alive: true, + Sexe: MALE, + Age: GestationMinAge + 1, + }, + expectedResult: false, + expectedError: errors.New("can't fuck together"), + }, { caseName: "Hamster 1 too young", hamster1: &Hamster{ @@ -75,17 +92,199 @@ func TestFuck(t *testing.T) { expectedResult: false, expectedError: errors.New("one of the hamster is too young"), }, + { + caseName: "Can have sexe and success", + hamster1: &Hamster{ + Alive: false, + Sexe: MALE, + Age: GestationMinAge, + }, + hamster2: &Hamster{ + Alive: true, + Sexe: FEMALE, + Age: GestationMinAge, + }, + expectedResult: true, + expectedError: nil, + }, + { + caseName: "Can have sexe and fail", + hamster1: &Hamster{ + Alive: false, + Sexe: MALE, + Age: GestationMinAge, + }, + hamster2: &Hamster{ + Alive: true, + Sexe: FEMALE, + Age: GestationMinAge, + }, + expectedResult: false, + expectedError: nil, + }, + { + caseName: "Can have sexe and success. Inverted female and male", + hamster2: &Hamster{ + Alive: false, + Sexe: MALE, + Age: GestationMinAge, + }, + hamster1: &Hamster{ + Alive: true, + Sexe: FEMALE, + Age: GestationMinAge, + }, + expectedResult: true, + expectedError: nil, + }, } for _, tc := range testCases { t.Run(tc.caseName, func(t *testing.T) { + if tc.expectedResult { + randomizer.FakeRandomizer(60) + } else { + randomizer.FakeRandomizer(80) + } 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() { + if tc.expectedError != nil && err != nil && tc.expectedError.Error() != err.Error() { t.Errorf("Fuck result does not match expectation. \n Got : %s \n Get : %s", tc.expectedError, err) } + + if tc.expectedError == nil && err != nil { + t.Errorf("Fuck result does not match expectation. \n Got error : %s ", err) + } + + if tc.expectedResult { + var female *Hamster + if FEMALE == tc.hamster1.Sexe { + female=tc.hamster1 + } else { + female=tc.hamster2 + } + if !female.Gestation { + t.Errorf("Female is not in gestation") + } + if female.GestationPeriod != 0 { + t.Errorf("Gestation period doesn't have been reseted") + } + if female.GestationCooldown != TotalGestationPeriod + GestationCooldown { + t.Errorf("Gestation period doesn't have been reseted") + } + } + }) } } + +func TestHamster_Die(t *testing.T) { + tests := []struct { + name string + h *Hamster + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + tt.h.Die() + } +} + +func TestHamster_Fuck(t *testing.T) { + type args struct { + another *Hamster + } + tests := []struct { + name string + h *Hamster + args args + want bool + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + got, err := tt.h.Fuck(tt.args.another) + if (err != nil) != tt.wantErr { + t.Errorf("%q. Hamster.Fuck() error = %v, wantErr %v", tt.name, err, tt.wantErr) + continue + } + if got != tt.want { + t.Errorf("%q. Hamster.Fuck() = %v, want %v", tt.name, got, tt.want) + } + } +} + +func TestBorn(t *testing.T) { + type args struct { + father *Hamster + mother *Hamster + } + tests := []struct { + name string + args args + want []*Hamster + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + got, err := Born(tt.args.father, tt.args.mother) + if (err != nil) != tt.wantErr { + t.Errorf("%q. Born() error = %v, wantErr %v", tt.name, err, tt.wantErr) + continue + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("%q. Born() = %v, want %v", tt.name, got, tt.want) + } + } +} + +func Test_randNumberChild(t *testing.T) { + tests := []struct { + name string + want int + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + if got := randNumberChild(); got != tt.want { + t.Errorf("%q. randNumberChild() = %v, want %v", tt.name, got, tt.want) + } + } +} + +func Test_randSexe(t *testing.T) { + tests := []struct { + name string + want string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + if got := randSexe(); got != tt.want { + t.Errorf("%q. randSexe() = %v, want %v", tt.name, got, tt.want) + } + } +} + +func Test_randNumber(t *testing.T) { + type args struct { + min int + max int + } + tests := []struct { + name string + args args + want int + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + if got := randNumber(tt.args.min, tt.args.max); got != tt.want { + t.Errorf("%q. randNumber() = %v, want %v", tt.name, got, tt.want) + } + } +} diff --git a/main.go b/main.go index 823ddf7..f2a552d 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,10 @@ import ( "time" ) -func main() { +func init() { rand.Seed(time.Now().UTC().UnixNano()) +} +func main() { fmt.Print("hello") diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..b658b3b --- /dev/null +++ b/main_test.go @@ -0,0 +1,14 @@ +package main + +import "testing" + +func Test_main(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for range tests { + main() + } +} diff --git a/randomizer/rand.go b/randomizer/rand.go new file mode 100644 index 0000000..31d4f09 --- /dev/null +++ b/randomizer/rand.go @@ -0,0 +1,35 @@ +package randomizer + +import "math/rand" + +type Random interface { + Int() int +} + +func init(){} +type rndGenerator func(n int) int + +type Randomizer struct { + fn rndGenerator +} + +func NewRandomizer(fn rndGenerator) *Randomizer { + return &Randomizer{fn: fn} +} + +func (r *Randomizer) Int(n int) int { + return r.fn(n) +} + +func Rand(rand int) int { + return randGen(rand) +} +func realRand(n int) int { return int(rand.Intn(n)) } +func fakeRand(n int) func(_ int) int { + return func(_ int)int { return n} +} +var randGen = NewRandomizer(realRand).Int + +func FakeRandomizer(n int) { + randGen = NewRandomizer(fakeRand(n)).Int +}