-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice_test.go
84 lines (66 loc) · 2.09 KB
/
dice_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"testing"
"github.com/lobocv/randomock"
)
func TestDice(t *testing.T) {
t.Run("Only One Return Value", func(t *testing.T) {
// Expect the result to be 4
expected := 4
rand := randomock.NewRandoMock().Add("roll", float64(expected))
dice := NewDice(6, rand)
for roll := 0; roll < 10; roll++ {
result := dice.Roll()
if result != expected {
t.Fatalf("expected to roll %d, got %d", expected, result)
}
}
})
t.Run("Many Different Return Values", func(t *testing.T) {
// Expect the result to be 4
expectedList := []float64{4, 2, 3, 1}
rand := randomock.NewRandoMock().Add("roll", expectedList...)
dice := NewDice(6, rand)
// Note we are only calling as many times as we have provided return values
// If we called more than that, we would get a panic due to the default Randomock policy
for roll := 0; roll < len(expectedList); roll++ {
result := dice.Roll()
expected := int(expectedList[roll])
if result != expected {
t.Fatalf("expected to roll %d, got %d", expected, result)
}
}
})
t.Run("More calls than Return Values", func(t *testing.T) {
defer func() {
r := recover()
fmt.Printf("We got a panic, as expected! Panic: %v\n", r)
}()
// Expect the result to be 4
expectedList := []float64{4, 2, 3, 1}
rand := randomock.NewRandoMock().Add("roll", expectedList...)
dice := NewDice(6, rand)
for roll := 0; roll < 10; roll++ {
result := dice.Roll()
expected := int(expectedList[roll])
if result != expected {
t.Fatalf("expected to roll %d, got %d", expected, result)
}
}
})
t.Run("Different Randomock Policy", func(t *testing.T) {
// Expect the result to be 4
expectedList := []float64{4, 2, 3, 1}
randomock.SetDefaultPolicy(randomock.WrapAroundPolicy)
rand := randomock.NewRandoMock().Add("roll", expectedList...)
dice := NewDice(6, rand)
for roll := 0; roll < 10; roll++ {
result := dice.Roll()
expected := int(expectedList[roll%len(expectedList)]) // Note we wrap around here
if result != expected {
t.Fatalf("expected to roll %d, got %d", expected, result)
}
}
})
}