/
mastermind_test.go
111 lines (83 loc) · 2.71 KB
/
mastermind_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main_test
import (
"bufio"
"bytes"
"os"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/stretchr/testify/mock"
"github.com/trayo/go_mastermind"
"github.com/trayo/go_mastermind/print"
"github.com/trayo/go_mastermind/test/mocks"
)
var _ = Describe("Main CLI interaction", func() {
var (
stdin *bytes.Buffer
stdinReader *bufio.Reader
buffer *gbytes.Buffer
printer print.Printer
mockGamer *mocks.Gamer
commandSequence = func(args ...string) {
stdin.WriteString(strings.Join(args, "\n"))
}
)
BeforeEach(func() {
mockGamer = &mocks.Gamer{}
stdin = &bytes.Buffer{}
stdinReader = bufio.NewReader(stdin)
buffer = gbytes.NewBuffer()
printer = print.NewPrinter(buffer)
})
AfterEach(func() {
Expect(mockGamer.AssertExpectations(GinkgoT())).To(BeTrue())
})
Context("when providing valid input", func() {
It("can quit the game", func() {
commandSequence("q")
main.Run(mockGamer, stdinReader, printer)
Eventually(buffer).Should(gbytes.Say("Would you like to play"))
Eventually(buffer).Should(gbytes.Say("byeee"))
})
It("can visit the instructions page", func() {
commandSequence("i", "q")
main.Run(mockGamer, stdinReader, printer)
Eventually(buffer).Should(gbytes.Say("Would you like to play"))
Eventually(buffer).Should(gbytes.Say("generate a sequence"))
Eventually(buffer).Should(gbytes.Say("byeee"))
})
It("can visit the instructions page multiple times", func() {
commandSequence("i", "i", "q")
main.Run(mockGamer, stdinReader, printer)
Eventually(buffer).Should(gbytes.Say("Would you like to play"))
Eventually(buffer).Should(gbytes.Say("generate a sequence"))
Eventually(buffer).Should(gbytes.Say("generate a sequence"))
Eventually(buffer).Should(gbytes.Say("byeee"))
})
It("can start a game", func() {
commandSequence("p", "q")
mockGamer.On("Play", mock.Anything).Times(1)
main.Run(mockGamer, stdinReader, printer)
})
})
Context("entering the cheat code", func() {
It("can accept a code", func() {
secret := os.Getenv("MASTERMIND_SECRET")
code := "rrrr"
commandSequence(secret, code, "q")
mockGamer.On("Play", mock.Anything).Times(1)
main.Run(mockGamer, stdinReader, printer)
})
})
Context("when providing invalid input", func() {
It("will ask for valid input", func() {
commandSequence("this wont work", "q")
main.Run(mockGamer, stdinReader, printer)
Eventually(buffer).Should(gbytes.Say("Unknown command"))
Eventually(buffer).Should(gbytes.Say("Valid commands are"))
Eventually(buffer).Should(gbytes.Say("Would you like to play"))
Eventually(buffer).Should(gbytes.Say("byeee"))
})
})
})