-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathutest_MVC.py
More file actions
162 lines (137 loc) · 4.52 KB
/
Copy pathutest_MVC.py
File metadata and controls
162 lines (137 loc) · 4.52 KB
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
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# Written by: DGC
# python imports
import unittest
# local imports
import MVC
#==============================================================================
class utest_MVC(unittest.TestCase):
def test_model(self):
model = MVC.Model()
question, possible_answers = model.question_and_answers()
# can't test what they are because they're random
self.assertTrue(
isinstance(question, str),
"Question should be a string"
)
self.assertTrue(
isinstance(possible_answers, list),
"Answers should be a list"
)
for item in possible_answers:
self.assertTrue(
isinstance(item[0], str),
"Elements of possible answer list should be strings"
)
def test_controller(self):
model = ModelMock()
view = ViewMock()
controller = MVC.Controller(model, view)
controller.new_question()
self.assertEqual(
view.question,
"Question",
"Controller should pass the question to the view."
)
controller.answer("Question", "correct")
self.assertEqual(
controller.view.mock_feedback,
"That is correct.",
"The feedback for a correct answer is wrong."
)
controller.answer("Question", "incorrect")
self.assertEqual(
controller.view.mock_feedback,
"Sorry that's wrong.",
"The feedback for an incorrect answer is wrong."
)
def test_view(self):
view = MVC.View()
controller = ControllerMock(view)
view.register(controller)
self.assertIs(
view.answer_button,
None,
"The answer button should not be set."
)
self.assertIs(
view.continue_button,
None,
"The continue button should not be set."
)
view.new_question("Test", ["correct", "incorrect"])
self.assertIsNot(
view.answer_button,
None,
"The answer button should be set."
)
self.assertIs(
view.continue_button,
None,
"The continue button should not be set."
)
# simulate a button press
view.answer_button.invoke()
self.assertIs(
view.answer_button,
None,
"The answer button should not be set."
)
self.assertIsNot(
view.continue_button,
None,
"The continue button should be set."
)
self.assertEqual(
controller.question,
"Test",
"The question asked should be \"Test\"."
)
self.assertEqual(
controller.answer,
"correct",
"The answer given should be \"correct\"."
)
# continue
view.continue_button.invoke()
self.assertIsNot(
view.answer_button,
None,
"The answer button should be set."
)
self.assertIs(
view.continue_button,
None,
"The continue button should not be set."
)
#==============================================================================
class ViewMock(object):
def new_question(self, question, answers):
self.question = question
self.answers = answers
def register(self, controller):
pass
def feedback(self, feedback):
self.mock_feedback = feedback
#==============================================================================
class ModelMock(object):
def question_and_answers(self):
return ("Question", ["correct", "incorrect"])
def is_correct(self, question, answer):
correct = False
if (answer == "correct"):
correct = True
return correct
#==============================================================================
class ControllerMock(object):
def __init__(self, view):
self.view = view
def answer(self, question, answer):
self.question = question
self.answer = answer
self.view.feedback("test")
def next_question(self):
self.view.new_question("Test", ["correct", "incorrect"])
#==============================================================================
if (__name__ == "__main__"):
unittest.main(verbosity=2)