-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquestion_utils_test.go
142 lines (129 loc) · 3.28 KB
/
question_utils_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package utils
import (
"bytes"
"fmt"
"io"
"os"
"testing"
)
// テスト問題用のJSONファイル
var test_json_path = "../json/questions.json"
var questions []Question
// テスト用問題の解答
var expect_questions = []Question{
Question{"test1", "テスト1"},
Question{"test2", "テスト2"},
Question{"test3", "テスト3"},
}
// 問題のロードのテスト
func TestLoadQuestions(t *testing.T) {
questions = LoadQuestions(test_json_path)
expect_len := 3
if actual_len := len(questions); expect_len != actual_len {
t.Error(`expect="%s" actual="%s"`, expect_len, actual_len)
}
for i, question := range questions {
if expect_questions[i] != question {
t.Error(`expect="%s" actual="%s"`, expect_questions[i], question)
}
}
}
// 問題の解答が正しいかテスト
func TestGetQuestion(t *testing.T) {
question := GetQuestion(questions)
is_question := false
for _, expect := range expect_questions {
if question.Question == expect.Question {
if question.Answer != expect.Answer {
t.Error(`expect="%s" actual="%s"`, expect.Answer, question.Answer)
}
is_question = true
}
}
if !is_question {
t.Error(`Invalid question : "%v"`, question)
}
}
// 問題の表示が正しいかテスト
func TestPopQuestion(t *testing.T) {
out := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
expect_question := PopQuestion(questions, 5)
expect := fmt.Sprintf("Q%d \"%s\" は日本語で? : ", 5, expect_question.Question)
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = out
actual := <-outC
if actual != expect {
t.Errorf(`expect="%s" actual="%s"`, expect, actual)
}
}
// 回答が正解の場合の表示のテスト
func TestCheckAnswer1(t *testing.T) {
question := GetQuestion(questions)
expect := fmt.Sprintf("正解です!\n")
out := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
CheckAnswer(question, question.Answer)
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = out
actual := <-outC
if actual != expect {
t.Errorf(`expect="%s" actual="%s"`, expect, actual)
}
}
// 回答が不正解の場合のテスト
func TestCheckAnswer2(t *testing.T) {
question := GetQuestion(questions)
expect := fmt.Sprintf("不正解です。 正解は、\"%s\" です。\n", question.Answer)
out := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
CheckAnswer(question, "xxxxxxxxxxxx")
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = out
actual := <-outC
if actual != expect {
t.Errorf(`expect="%s" actual="%s"`, expect, actual)
}
}
// タイムアウト後の結果表示のテスト
func TestShowResult(t *testing.T) {
expect_result := Result{10, 5}
expect := fmt.Sprintf("\n!!! タイムオーバーです。!!!\n問題%d問中、正解は%d問でした。\n", expect_result.Count, expect_result.Correct)
out := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
ShowResult(expect_result)
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
os.Stdout = out
actual := <-outC
if actual != expect {
t.Errorf(`expect="%s" actual="%s"`, expect, actual)
}
}