Skip to content

Commit 4038378

Browse files
committed
Add test for Game.run()
1 parent b385b35 commit 4038378

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package typinggame
2+
3+
var ExportGameRun = (*Game).run

kadai3-1/lfcd85/typinggame/typinggame.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ type Game struct {
1818
}
1919

2020
func Execute(g Game) error {
21-
g.run(os.Stdin, os.Stdout)
21+
g.run(inputChannel(os.Stdin), os.Stdout)
2222
return nil
2323
}
2424

25-
func (g *Game) run(r io.Reader, w io.Writer) {
26-
ch := input(r)
25+
func (g *Game) run(ch <-chan string, w io.Writer) {
2726
bc := context.Background()
2827
ctx, cancel := context.WithTimeout(bc, g.TimeLimit)
2928
defer cancel()
@@ -40,10 +39,10 @@ LOOP:
4039
case input := <-ch:
4140
if input == word {
4241
score++
43-
fmt.Fprintln(w, "ok! current score:", score)
42+
fmt.Fprintln(w, input, "... OK! current score:", score)
4443
word = g.Words[rand.Intn(len(g.Words))]
4544
} else {
46-
fmt.Fprintln(w, "ng")
45+
fmt.Fprintln(w, input, "... NG: try again.")
4746
}
4847
case <-ctx.Done():
4948
fmt.Fprintln(w)
@@ -53,7 +52,7 @@ LOOP:
5352
}
5453
}
5554

56-
func input(r io.Reader) <-chan string {
55+
func inputChannel(r io.Reader) <-chan string {
5756
ch := make(chan string)
5857

5958
go func() {
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package typinggame_test
22

33
import (
4+
"bytes"
5+
"regexp"
46
"testing"
57
"time"
68

@@ -9,11 +11,56 @@ import (
911

1012
func TestExecute(t *testing.T) {
1113
g := typinggame.Game{
12-
typinggame.Words{"hoge", "fuga", "piyo"},
14+
typinggame.Words{"hoge"},
1315
1 * time.Second,
1416
}
1517

1618
if err := typinggame.Execute(g); err != nil {
1719
t.Errorf("failed to execute new game: %v", err)
1820
}
1921
}
22+
23+
func TestGame_run(t *testing.T) {
24+
ch := make(chan string)
25+
go func() {
26+
time.Sleep(100 * time.Millisecond)
27+
ch <- "hoga"
28+
time.Sleep(100 * time.Millisecond)
29+
ch <- "hoge"
30+
}()
31+
32+
g := typinggame.Game{
33+
typinggame.Words{"hoge"},
34+
1 * time.Second,
35+
}
36+
37+
var output bytes.Buffer
38+
typinggame.ExportGameRun(&g, ch, &output)
39+
40+
cases := []struct {
41+
output string
42+
expected bool
43+
}{
44+
{"hoga ... NG", true},
45+
{"hoga ... OK", false},
46+
{"hoge ... OK", true},
47+
{"hoge ... NG", false},
48+
}
49+
50+
for _, c := range cases {
51+
c := c
52+
t.Run(c.output, func(t *testing.T) {
53+
t.Parallel()
54+
55+
actual := regexp.MustCompile(c.output).MatchString(output.String())
56+
if actual != c.expected {
57+
switch c.expected {
58+
case true:
59+
t.Errorf("%v should be outputted but actually was not", c.output)
60+
case false:
61+
t.Errorf("%v should not be outputted but actyally was", c.output)
62+
}
63+
}
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)