Skip to content

Commit 8b7a9bb

Browse files
committed
[typ] refactor and add test for show()
1 parent e70784f commit 8b7a9bb

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

kadai3/imura81gt/typ/typing/typing.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ func load() string {
3636
return fmt.Sprintf("%sも%sも%sのうち", w1, w2, w3)
3737
}
3838

39-
func show(score int, chars int, txt string) {
40-
fmt.Println(score, chars, ">", txt)
41-
fmt.Print(score, chars, " > ")
39+
func show(score int, chars int, txt string, out io.Writer) {
40+
fmt.Fprintf(out, "%d %d > %s\n%d %d > ", score, chars, txt, score, chars)
4241
}
4342

4443
// Run is a function to start typing-game.
@@ -59,7 +58,7 @@ func Run() {
5958

6059
txt := load()
6160

62-
show(score, chars, txt)
61+
show(score, chars, txt, os.Stdout)
6362

6463
B:
6564
for {
@@ -70,10 +69,10 @@ B:
7069
score++
7170
chars = chars + len([]rune(txt))
7271
txt = load()
73-
show(score, chars, txt)
72+
show(score, chars, txt, os.Stdout)
7473
} else {
7574
fmt.Println("BAD....")
76-
show(score, chars, txt)
75+
show(score, chars, txt, os.Stdout)
7776
}
7877
case <-time.After(limit * time.Second):
7978
fmt.Println()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package typing
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
func TestInput(t *testing.T) {}
10+
11+
func TestLoad(t *testing.T) {}
12+
13+
func TestShow(t *testing.T) {
14+
testcases := []struct {
15+
caseName string
16+
score int
17+
char int
18+
txt string
19+
expected string
20+
}{
21+
{caseName: "score10char300", score: 10, char: 300, txt: "テスト", expected: "10 300 > テスト\n10 300 > "},
22+
}
23+
24+
for _, tc := range testcases {
25+
tc := tc // capture range variable. need to set when run parallel test.
26+
27+
fmt.Println(tc)
28+
t.Run(tc.caseName, func(t *testing.T) {
29+
t.Parallel()
30+
var buf bytes.Buffer
31+
show(tc.score, tc.char, tc.txt, &buf)
32+
actual := buf.String()
33+
if tc.expected != actual {
34+
t.Errorf("\ncaseName:%s\nactual:%+v\nExpected:%+v\n",
35+
tc.caseName,
36+
actual,
37+
tc.expected,
38+
)
39+
}
40+
})
41+
}
42+
43+
}
44+
45+
func TestRun(t *testing.T) {}

0 commit comments

Comments
 (0)