-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz_test.go
153 lines (121 loc) · 3.09 KB
/
quiz_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
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"sort"
"strings"
"testing"
)
func getTestQuizes() *QuizManager {
qm := MakeQuizManager(QuizDatabaseDirectory{path: "."})
q := question{Answers: make(map[string]answer)}
q.Answers["fish"] = answer{Count: 5}
q.Answers["fowl"] = answer{Count: 27}
testQuiz := quiz{dirty: true,
Questions: nil}
testQuiz.Questions = append(testQuiz.Questions, q)
qm.quizes["quiz1"] = testQuiz
return qm
}
func TestSubmitingAnswers(t *testing.T) {
qm := getTestQuizes()
var tests = []struct {
q string
num int
ans string
output string
err bool
}{
{"quiz1", 0, "fish", "fish {6}fowl {27}", false},
{"dfgdgf", 0, "fish", "<nil>", true},
{"quiz1", 27, "fish", "<nil>", true},
{"quiz1", 0, "apple", "<nil>", true}}
for index, test := range tests {
result, err := qm.SubmitAnswer(test.q, test.num, test.ans)
if (err != nil) && (test.err == false) {
t.Errorf("Test %d had an eror", index)
continue
}
if (test.err == true) && (err == nil) {
t.Errorf("Test %d expected error", index)
continue
}
var outputString = "<nil>"
if result != nil {
var keys []string
for k := range result.Answers {
keys = append(keys, k)
}
sort.Strings(keys)
var builder strings.Builder
for _, v := range keys {
fmt.Fprintf(&builder, "%v %v", v, result.Answers[v])
}
outputString = builder.String()
}
if test.output != outputString {
t.Errorf("Test %d expected %q got %q", index, test.output, outputString)
}
}
}
func TestGetQuizIDs(t *testing.T) {
qm := getTestQuizes()
ids := qm.GetQuizeIDs()
if len(ids) != 1 {
t.Errorf("Could not get quizes")
return
}
if ids[0] != "quiz1" {
t.Errorf("Incorrect quiz id")
return
}
}
func TestGetQuizResults(t *testing.T) {
qm := getTestQuizes()
var tests = []struct {
id string
output string
errExpected bool
}{{"quiz1", "[{map[fish:{5} fowl:{27}]}]", false},
{"quizdgdg1", "[]", true}}
for index, test := range tests {
result, err := qm.GetQuizResults(test.id)
ok := err != nil
if test.errExpected != ok {
t.Errorf("Wrong error on %d", index)
continue
}
if test.output != fmt.Sprintf("%v", result) {
t.Errorf("Expected %s got %v", test.output, result)
continue
}
}
}
func TestSavingAndLoading(t *testing.T) {
qm := getTestQuizes()
qm.saveDirtyQuizes()
qm.quizes = make(map[string]quiz)
_, err := qm.SubmitAnswer("quiz1", 0, "fish")
if err != nil {
t.Errorf("Got error " + err.Error())
}
}
func TestIllegalFiles(t *testing.T) {
db := QuizDatabaseDirectory{path: "validpath"}
var tests = []struct {
name string
fullPathname string
hasError bool
}{{"quiz", "validpath/quiz.json", false},
{"back\\slash", "", true},
{"pipe|pipe", "", true}}
for _, test := range tests {
name, err := db.getFullPathname(test.name)
if err != nil && test.hasError == false {
t.Errorf("Expected no error with %q", test.name)
} else if err == nil && test.hasError == true {
t.Errorf("Expected an error with %q", test.name)
} else if name != test.fullPathname {
t.Errorf("Expect %q == %q", test.name, name)
}
}
}