Skip to content

Commit beb5c4f

Browse files
author
en-ken
committed
test: add test for main module.
1 parent 8a63697 commit beb5c4f

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

kadai3/en-ken/kadai3/export_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
var Execute = execute
4+
5+
var InputRoutine = inputRoutine
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main_test
2+
3+
import (
4+
"io"
5+
)
6+
7+
//Stdin
8+
type StdinMock struct {
9+
i int
10+
input []string
11+
}
12+
13+
func (stdin *StdinMock) Read(p []byte) (n int, err error) {
14+
if stdin.i >= len(stdin.input) {
15+
return 0, io.EOF
16+
}
17+
b := []byte(stdin.input[stdin.i] + "\n") //Scanが回るようにLF追加
18+
copy(p, b)
19+
stdin.i++
20+
return len(b), nil
21+
}
22+
23+
//Stdout
24+
type StdoutMock struct {
25+
output []string
26+
}
27+
28+
func (stdout *StdoutMock) Write(p []byte) (n int, err error) {
29+
str := string(p)
30+
stdout.output = append(stdout.output, str)
31+
return len(str), nil
32+
}
33+
34+
//Typing
35+
type TypingMock struct {
36+
}
37+
38+
func (typ *TypingMock) GetNextText() string {
39+
return "FOO"
40+
}
41+
42+
func (typ *TypingMock) IsCorrect(input string) bool {
43+
return "FOO" == input
44+
}

kadai3/en-ken/kadai3/main_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
"time"
7+
8+
main "github.com/gopherdojo/dojo6/kadai3/en-ken/kadai3"
9+
)
10+
11+
func TestInputRoutine(t *testing.T) {
12+
input := []string{"foo", "bar", "baz", "qux"}
13+
stdin := &StdinMock{
14+
i: 0,
15+
input: input,
16+
}
17+
18+
ch := main.InputRoutine(stdin)
19+
20+
for _, expected := range input {
21+
actual := <-ch
22+
if actual != expected {
23+
t.Errorf("expected:%v, actual:%v", expected, actual)
24+
}
25+
}
26+
}
27+
28+
func TestExecute(t *testing.T) {
29+
chInput := make(chan string, 3)
30+
chFinish := make(chan time.Time, 1)
31+
32+
scenario := []struct {
33+
inputText string
34+
time time.Time
35+
}{
36+
{
37+
inputText: "FOO",
38+
},
39+
{
40+
inputText: "BAR",
41+
},
42+
{
43+
inputText: "FOO",
44+
},
45+
{
46+
time: time.Now(),
47+
},
48+
}
49+
50+
buf := bytes.NewBufferString("")
51+
typ := &TypingMock{}
52+
53+
go func() {
54+
for _, s := range scenario {
55+
time.Sleep(100 * time.Millisecond) //DASAI
56+
if s.inputText != "" {
57+
chInput <- s.inputText
58+
}
59+
if !s.time.IsZero() {
60+
chFinish <- s.time
61+
}
62+
}
63+
}()
64+
main.Execute(chInput, chFinish, buf, typ)
65+
66+
expected := []byte("" +
67+
"[001]: FOO\n" + "type>>" + "Correct!\n" +
68+
"[002]: FOO\n" + "type>>" + "Miss!\n" +
69+
"[003]: FOO\n" + "type>>" + "Correct!\n" +
70+
"[004]: FOO\n" + "type>>" +
71+
"\nTime's up!!\n" +
72+
"You Scored: 2\n")
73+
74+
if bytes.Compare(buf.Bytes(), expected) != 0 {
75+
t.Errorf("[expected]:\n%s\n[actual]:\n%s", expected, buf.Bytes())
76+
}
77+
}

0 commit comments

Comments
 (0)