Skip to content

Commit 73c537d

Browse files
committed
[type] add testing for load() and add time.Time mock
1 parent 95e2cc7 commit 73c537d

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

kadai3/imura81gt/typ/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package main
33
import "github.com/gopherdojo/dojo7/kadai3/imura81gt/typ/typing"
44

55
func main() {
6-
typing.Run()
6+
g := typing.Game{}
7+
g.Run()
78
}

kadai3/imura81gt/typ/typing/typing.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ import (
1010
"time"
1111
)
1212

13+
// ----- for time
14+
type Clock interface {
15+
Now() time.Time
16+
}
17+
18+
type ClockFunc func() time.Time
19+
20+
func (f ClockFunc) Now() time.Time {
21+
return f()
22+
}
23+
24+
type Game struct {
25+
Clock Clock
26+
}
27+
28+
func (g *Game) now() time.Time {
29+
if g.Clock == nil {
30+
return time.Now()
31+
}
32+
return g.Clock.Now()
33+
}
34+
35+
// -----------
36+
1337
func input(r io.Reader) <-chan string {
1438
ch := make(chan string)
1539
go func() {
@@ -26,9 +50,9 @@ func input(r io.Reader) <-chan string {
2650
return ch
2751
}
2852

29-
func load() string {
53+
func (g *Game) load() string {
3054
var ws = []string{"すもも", "もも"}
31-
rand.Seed(time.Now().UnixNano())
55+
rand.Seed(g.now().UnixNano())
3256
w1 := ws[rand.Intn(len(ws))]
3357
w2 := ws[rand.Intn(len(ws))]
3458
w3 := ws[rand.Intn(len(ws))]
@@ -41,7 +65,7 @@ func show(score int, chars int, txt string, out io.Writer) {
4165
}
4266

4367
// Run is a function to start typing-game.
44-
func Run() {
68+
func (g *Game) Run() {
4569

4670
ctx := context.Background()
4771
ctx, cancel := context.WithCancel(ctx)
@@ -56,7 +80,7 @@ func Run() {
5680
var score int
5781
var chars int
5882

59-
txt := load()
83+
txt := g.load()
6084

6185
show(score, chars, txt, os.Stdout)
6286

@@ -68,7 +92,7 @@ B:
6892
fmt.Println("GOOD!!!")
6993
score++
7094
chars = chars + len([]rune(txt))
71-
txt = load()
95+
txt = g.load()
7296
show(score, chars, txt, os.Stdout)
7397
} else {
7498
fmt.Println("BAD....")

kadai3/imura81gt/typ/typing/typing_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"testing"
7+
"time"
78
)
89

910
func TestInput(t *testing.T) {
@@ -38,7 +39,23 @@ func TestInput(t *testing.T) {
3839

3940
}
4041

41-
func TestLoad(t *testing.T) {}
42+
func TestLoad(t *testing.T) {
43+
g := Game{
44+
Clock: ClockFunc(func() time.Time {
45+
return time.Date(2019, 11, 04, 02, 0, 0, 0, time.UTC)
46+
}),
47+
}
48+
49+
expected := "すもももももももものうち"
50+
actual := g.load()
51+
if actual != expected {
52+
t.Errorf("\ncaseName:%s\nactual:%+v\nExpected:%+v\n",
53+
"load test",
54+
actual,
55+
expected,
56+
)
57+
}
58+
}
4259

4360
func TestShow(t *testing.T) {
4461
testcases := []struct {

0 commit comments

Comments
 (0)