-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain_test.go
79 lines (69 loc) · 1.56 KB
/
main_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
package main_test
import (
"bytes"
"testing"
"time"
main "github.com/gopherdojo/dojo7/kadai3-1/shinta"
)
func TestInputRoutine(t *testing.T) {
input := []string{"apple", "bake", "cup", "dog"}
// StdinMockのinputに、標準入力で送信されたと仮定した文字列が入る
stdin := &StdinMock{
i: 0,
input: input,
}
ch := main.InputRoutine(stdin)
for _, expected := range input {
actual := <-ch
if actual != expected {
t.Errorf("expected:%v, actual:%v", expected, actual)
}
}
}
func TestExecute(t *testing.T) {
chInput := make(chan string, 3)
chFinish := make(chan time.Time, 1)
scenario := []struct {
inputText string
time time.Time
}{
{
inputText: "apple",
},
{
inputText: "bbaak",
},
{
inputText: "cup",
},
{
time: time.Now(),
},
}
// 新しく文字列を格納するbufferを確保
buf := bytes.NewBufferString("")
typ := &TypingMock{}
go func() {
for _, s := range scenario {
time.Sleep(100 * time.Millisecond)
if s.inputText != "" {
chInput <- s.inputText
}
if !s.time.IsZero() {
chFinish <- s.time
}
}
}()
main.Execute(chInput, chFinish, buf, typ)
expected := []byte("" +
"[001]: apple\n" + "type>>" + "Correct!\n" +
"[002]: bake\n" + "type>>" + "Miss!\n" +
"[003]: cup\n" + "type>>" + "Correct!\n" +
"[004]: dog\n" + "type>>" +
"\nTime's up!!\n" +
"You Scored: 2\n")
// byteスライスの比較、a == bの場合は0を返す
if bytes.Compare(buf.Bytes(), expected) != 0 {
t.Errorf("[expected]:\n%s\n[actual]:\n%s", expected, buf.Bytes())
}
}