99import os
1010import sys
1111from unittest import TextTestRunner
12- from exam_test_case import ExamTestCase
13- from exam_test_result import ExamTestResult
14- from helper_functions import import_module
15- from helper_functions import find_path_to_assignment
12+ from examiner . exam_test_case import ExamTestCase
13+ from examiner . exam_test_result import ExamTestResult
14+ from examiner . helper_functions import import_module
15+ from examiner . helper_functions import find_path_to_assignment
1616
1717
1818FILE_DIR = os .path .dirname (os .path .realpath (__file__ ))
@@ -31,53 +31,57 @@ class Test1Files(ExamTestCase):
3131 The different asserts https://docs.python.org/3.6/library/unittest.html#test-cases
3232 """
3333
34- @classmethod
35- def setUpClass (cls ):
36- # Otherwise the .txt files will not be found
37- os .chdir (REPO_PATH )
3834
39- def test_a_modules_exist (self ):
40- """
41- Testar att din kod är uppdelad i rätt moduler.
42- |G|Förväntar att följande modul finns men hittades inte:|/RE|
43- {arguments}
44- """
45- self ._argument = "main"
46- self .assertIsNotNone (util .find_spec (self ._argument ))
47- self ._argument = "menu"
48- self .assertIsNotNone (util .find_spec (self ._argument ))
49- self ._argument = "analyzer"
50- self .assertIsNotNone (util .find_spec (self ._argument ))
35+
36+
5137
5238class Test2Counters (ExamTestCase ):
5339 """
5440 Meny options for counting
5541 """
42+ @classmethod
43+ def setUpClass (cls ):
44+ # Otherwise the .txt files will not be found
45+ os .chdir (REPO_PATH )
46+
47+
48+
5649 def test_b_lines (self ):
5750 """
58- Testar "lines" kommandot.
59- Förväntar sig att följande finns i utskriften:
51+ Testar att anropa menyval 'lines' i main.py.
52+ Använder följande som input:
53+ {arguments}
54+ Förväntar att följande finns med i utskrift:
6055 {correct}
61- Fick utskriften :
56+ Fick följande :
6257 {student}
6358 """
59+ self .tags = ["count" , "lines" ]
6460 self .norepr = True
65- with patch ('builtins.input' , side_effect = ["lines" , "" , "q" ]):
61+ self ._multi_arguments = ["lines" , "" , "q" ]
62+ with patch ('builtins.input' , side_effect = self ._multi_arguments ):
6663 with patch ('sys.stdout' , new = StringIO ()) as fake_out :
6764 main .main ()
6865 str_data = fake_out .getvalue ()
6966 self .assertIn ("17" , str_data )
7067
68+
69+
7170 def test_c_words (self ):
7271 """
73- Testar "words" kommandot.
74- Förväntar sig att följande finns i utskriften:
72+ Testar att anropa menyval 'words' i main.py.
73+ Använder följande som input:
74+ {arguments}
75+ Förväntar att följande finns med i utskrift:
7576 {correct}
76- Fick utskriften :
77+ Fick följande :
7778 {student}
7879 """
80+ self .tags = ["count" , "words" ]
7981 self .norepr = True
80- with patch ('builtins.input' , side_effect = ["words" , "" , "q" ]):
82+ self ._multi_arguments = ["words" , "" , "q" ]
83+
84+ with patch ('builtins.input' , side_effect = self ._multi_arguments ):
8185 with patch ('sys.stdout' , new = StringIO ()) as fake_out :
8286 main .main ()
8387 str_data = fake_out .getvalue ()
@@ -87,14 +91,19 @@ def test_c_words(self):
8791
8892 def test_d_letters (self ):
8993 """
90- Testar "letters" kommandot.
91- Förväntar sig att följande finns i utskriften:
94+ Testar att anropa menyval 'letters' i main.py.
95+ Använder följande som input:
96+ {arguments}
97+ Förväntar att följande finns med i utskrift:
9298 {correct}
93- Fick utskriften :
99+ Fick följande :
94100 {student}
95101 """
102+ self .tags = ["count" , "letters" ]
96103 self .norepr = True
97- with patch ('builtins.input' , side_effect = ["letters" , "" , "q" ]):
104+ self ._multi_arguments = ["letters" , "" , "q" ]
105+ self .norepr = True
106+ with patch ('builtins.input' , side_effect = self ._multi_arguments ):
98107 with patch ('sys.stdout' , new = StringIO ()) as fake_out :
99108 main .main ()
100109 str_data = fake_out .getvalue ()
@@ -106,63 +115,68 @@ class Test3Frequencies(ExamTestCase):
106115 """
107116 Meny options for frequency
108117 """
118+
119+ def check_print_contain (self , inp , correct ):
120+ """
121+ One function for testing print input functions.
122+ """
123+ with patch ("builtins.input" , side_effect = inp ):
124+ with patch ("sys.stdout" , new = StringIO ()) as fake_out :
125+ main .main ()
126+ for val in correct :
127+ str_data = fake_out .getvalue ()
128+ self .assertIn (val , str_data )
129+
130+
109131 def test_a_word_frequency (self ):
110132 """
111- Testar "word_frequency" kommandot.
112- Förväntar sig att följande finns i utskriften:
133+ Testar att anropa menyval 'word_frequency' i main.py.
134+ Använder följande som input:
135+ {arguments}
136+ Förväntar att följande finns med i utskrift:
113137 {correct}
114- Fick utskriften :
138+ Fick följande :
115139 {student}
116140 """
141+ self .tags = ["freq" , "word_frequency" ]
117142 self .norepr = True
118- with patch ('builtins.input' , side_effect = ["word_frequency" , "" , "q" ]):
119- with patch ('sys.stdout' , new = StringIO ()) as fake_out :
120- main .main ()
121- str_data = fake_out .getvalue ()
122- self .assertIn ("the" , str_data )
123- self .assertIn ("6.0%" , str_data )
124- self .assertIn ("to" , str_data )
125- self .assertIn ("4.0%" , str_data )
126- self .assertIn ("and" , str_data )
127- self .assertIn ("3.5%" , str_data )
128- self .assertIn ("of" , str_data )
129- self .assertIn ("3.0%" , str_data )
130- self .assertIn ("street" , str_data )
131- self .assertIn ("2.5%" , str_data )
132- self .assertIn ("him" , str_data )
133- self .assertIn ("2.5%" , str_data )
134- self .assertIn ("he" , str_data )
135- self .assertIn ("2.5%" , str_data )
143+ self ._multi_arguments = ["word_frequency" , "" , "q" ]
144+ self .check_print_contain (self ._multi_arguments , [
145+ "the: 12 | 6.0%" ,
146+ "to: 8 | 4.0%" ,
147+ "and: 7 | 3.5%" ,
148+ "of: 6 | 3.0%" ,
149+ "street: 5 | 2.5%" ,
150+ "him: 5 | 2.5%" ,
151+ "he: 5 | 2.5%" ,
152+ ])
153+
136154
137155
138156
139157 def test_b_letter_frequency (self ):
140158 """
141- Testar "letter_frequency" kommandot.
142- Förväntar sig att följande finns i utskriften:
159+ Testar att anropa menyval 'letter_frequency' i main.py.
160+ Använder följande som input:
161+ {arguments}
162+ Förväntar att följande finns med i utskrift:
143163 {correct}
144- Fick utskriften :
164+ Fick följande :
145165 {student}
146166 """
167+ self .tags = ["freq" , "letter_frequency" ]
147168 self .norepr = True
148- with patch ('builtins.input' , side_effect = ["letter_frequency" , "" , "q" ]):
149- with patch ('sys.stdout' , new = StringIO ()) as fake_out :
150- main .main ()
151- str_data = fake_out .getvalue ()
152- self .assertIn ("e" , str_data )
153- self .assertIn ("11.9%" , str_data )
154- self .assertIn ("t" , str_data )
155- self .assertIn ("10.0%" , str_data )
156- self .assertIn ("o" , str_data )
157- self .assertIn ("8.5%" , str_data )
158- self .assertIn ("h" , str_data )
159- self .assertIn ("7.4%" , str_data )
160- self .assertIn ("n" , str_data )
161- self .assertIn ("7.3%" , str_data )
162- self .assertIn ("i" , str_data )
163- self .assertIn ("7.1%" , str_data )
164- self .assertIn ("a" , str_data )
165- self .assertIn ("7.1%" , str_data )
169+ self ._multi_arguments = ["letter_frequency" , "" , "q" ]
170+ self .check_print_contain (self ._multi_arguments , [
171+ "e: 108 | 11.9%" ,
172+ "t: 91 | 10.0%" ,
173+ "o: 77 | 8.5%" ,
174+ "h: 67 | 7.4%" ,
175+ "n: 66 | 7.3%" ,
176+ "i: 64 | 7.1%" ,
177+ "a: 64 | 7.1%" ,
178+ ])
179+
166180
167181
168182
@@ -171,30 +185,95 @@ class Test4All(ExamTestCase):
171185 Meny options for frequency
172186 """
173187
188+ def check_print_contain (self , inp , correct ):
189+ """
190+ One function for testing print input functions.
191+ """
192+ with patch ("builtins.input" , side_effect = inp ):
193+ with patch ("sys.stdout" , new = StringIO ()) as fake_out :
194+ main .main ()
195+ for val in correct :
196+ str_data = fake_out .getvalue ()
197+ self .assertIn (val , str_data )
198+
174199 def test_a_all (self ):
175200 """
176- Testar "all" kommandot.
177- Förväntar sig att följande finns i utskriften:
201+ Testar att anropa menyval 'all' i main.py.
202+ Använder följande som input:
203+ {arguments}
204+ Förväntar att följande finns med i utskrift:
205+ {correct}
206+ Fick följande:
207+ {student}
208+ """
209+ self .tags = ["all" ]
210+ self .norepr = True
211+ self ._multi_arguments = ["all" , "" , "q" ]
212+
213+ self .check_print_contain (self ._multi_arguments , [
214+ "17" ,
215+ "199" ,
216+ "907" ,
217+ "the: 12 | 6.0%" ,
218+ "to: 8 | 4.0%" ,
219+ "and: 7 | 3.5%" ,
220+ "of: 6 | 3.0%" ,
221+ "street: 5 | 2.5%" ,
222+ "him: 5 | 2.5%" ,
223+ "he: 5 | 2.5%" ,
224+ "e: 108 | 11.9%" ,
225+ "t: 91 | 10.0%" ,
226+ "o: 77 | 8.5%" ,
227+ "h: 67 | 7.4%" ,
228+ "n: 66 | 7.3%" ,
229+ "i: 64 | 7.1%" ,
230+ "a: 64 | 7.1%" ,
231+ ])
232+
233+
234+
235+ class Test4Change (ExamTestCase ):
236+ """
237+ Meny options for frequency
238+ """
239+
240+ def test_a_change (self ):
241+ """
242+ Testar att anropa menyval 'all' i main.py.
243+ Använder följande som input:
244+ {arguments}
245+ Förväntar att följande finns med i utskrift:
178246 {correct}
179- Fick utskriften :
247+ Fick följande :
180248 {student}
181249 """
250+ self .tags = ["change" ]
182251 self .norepr = True
183- with patch ('builtins.input' , side_effect = ["all" , "" , "q" ]):
252+ self ._multi_arguments = ["change" , "lorum.txt" , "" , "all" , "" , "q" ]
253+ with patch ('builtins.input' , side_effect = self ._multi_arguments ):
184254 with patch ('sys.stdout' , new = StringIO ()) as fake_out :
185255 main .main ()
186256 str_data = fake_out .getvalue ()
187- self .assertIn ("17" , str_data )
188- self .assertIn ("199" , str_data )
189- self .assertIn ("907" , str_data )
190- self .assertIn ("the" , str_data )
191- self .assertIn ("6.0%" , str_data )
192- self .assertIn ("he" , str_data )
193- self .assertIn ("2.5%" , str_data )
194- self .assertIn ("e" , str_data )
195- self .assertIn ("11.9%" , str_data )
196- self .assertIn ("i" , str_data )
197- self .assertIn ("7.1%" , str_data )
257+
258+ self .assertIn ("23" , str_data )
259+ self .assertIn ("3" , str_data )
260+ self .assertIn ("140" , str_data )
261+
262+ self .assertIn ("dolor: 2 | 8.0%" , str_data )
263+ self .assertIn ("vivamus: 1 | 4.0%" , str_data )
264+ self .assertIn ("vitae: 1 | 4.0%" , str_data )
265+ self .assertIn ("varius: 1 | 4.0%" , str_data )
266+ self .assertIn ("urna: 1 | 4.0%" , str_data )
267+ self .assertIn ("sit: 1 | 4.0%" , str_data )
268+ self .assertIn ("pellentesque: 1 | 4.0%" , str_data )
269+
270+ self .assertIn ("i: 18 | 12.9%" , str_data )
271+ self .assertIn ("e: 16 | 11.4%" , str_data )
272+ self .assertIn ("u: 12 | 8.6%" , str_data )
273+ self .assertIn ("a: 12 | 8.6%" , str_data )
274+ self .assertIn ("t: 10 | 7.1%" , str_data )
275+ self .assertIn ("l: 10 | 7.1%" , str_data )
276+ self .assertIn ("s: 9 | 6.4%" , str_data )
198277
199278
200279
0 commit comments