forked from um-software-testing/shunting_yard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tokenize.py
More file actions
307 lines (247 loc) · 10.5 KB
/
test_tokenize.py
File metadata and controls
307 lines (247 loc) · 10.5 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import unittest
import shunting_yard as sy
class TokenizeTest(unittest.TestCase):
def test_single_operator(self):
tokens = list(sy.tokenize('1+2'))
self.assertListEqual(tokens, ['1', '+', '2'])
def test_multiple_operators(self):
tokens = list(sy.tokenize('1+2-3'))
self.assertListEqual(tokens, ['1', '+', '2', '-', '3'])
'''
IsOperatorTest tests the helper function 'isOperator(arg1)' in shunting_yard.py.
'isOperator(arg1)' should return true when testing the following math operators:
(plus [+], minus [-], multiplication [*], and division operators [/]).
-------------------------------------------------------------------------------------
:param: single string-character mathematics operator accepted.
'''
class IsOperatorTest(unittest.TestCase):
#Pass an operator
def test_an_operator(self):
isOperator = sy.isOperator('-')
self.assertTrue(isOperator)
#Pass a numeric character
def test_non_operator(self):
isOperator = sy.isOperator('0')
self.assertFalse(isOperator)
#Pass a string
def test_a_string(self):
with self.assertRaises(ValueError):
sy.isOperator('21')
#Pass an empty string
def test_empty_char(self):
with self.assertRaises(ValueError):
sy.isOperator('')
#Pass an integer type
def test_wrong_input_type(self):
with self.assertRaises(TypeError):
sy.isOperator(1)
'''
IsDigitTest tests the helper function 'isDigit(arg1)' in shunting_yard.py.
'isDigit(arg1)' should return true when testing for single-digit numeric characters.
-------------------------------------------------------------------------------------
:param: single string-character digit accepted.
'''
class IsDigitTest(unittest.TestCase):
#Pass a single digit character.
def test_single_digit(self):
isDigit = sy.isDigit('0')
self.assertTrue(isDigit)
#Pass a multiple-digit character.
def test_large_digit(self):
with self.assertRaises(ValueError):
sy.isDigit('666')
#Pass a non-digit
def test_non_digit(self):
sy.isDigit('a')
self.assertRaises(Exception)
#Pass an empty char
def test_empty_char(self):
with self.assertRaises(ValueError):
sy.isDigit('')
#Pass an integer type
def test_wrong_input_type(self):
with self.assertRaises(TypeError):
sy.isDigit(1)
'''
IsLeftBracketTest tests the helper function 'isLeftBracket(arg1)' in shunting_yard.py.
'isLeftBracket(arg1)' should return true when testing for a single opening-bracket.
Accepted opening brackets are: (left parenthesis [(], left square bracket [[])
-------------------------------------------------------------------------------------
:param: single character opening-bracket or opening-parenthesis accepted.
'''
class IsLeftBracketTest(unittest.TestCase):
#Pass a single opening bracket
def test_single_left_bracket(self):
self.assertTrue(sy.isLeftBracket('['))
#Pass a single closing parenthesis
def test_single_right_parenthesis(self):
self.assertFalse(sy.isLeftBracket(']'))
#Pass a single closing bracket
def test_single_right_bracket(self):
self.assertFalse(sy.isLeftBracket(')'))
#Pass multiple opening brackets
def test_multiple_left_brackets(self):
with self.assertRaises(ValueError):
sy.isLeftBracket('[(')
#Pass a non-opening bracket
def test_non_left_bracket(self):
self.assertFalse(sy.isLeftBracket('a'))
#Pass an empty char
def test_empty_char(self):
with self.assertRaises(ValueError):
sy.isLeftBracket('')
#Pass an integer type
def test_wrong_input_type(self):
with self.assertRaises(TypeError):
sy.isLeftBracket(1)
'''
IsRightBracketTest tests the helper function 'isRightBracket(arg1)' in shunting_yard.py.
'isRightBracket(arg1)' should return true when testing for a single closing-bracket.
Accepted closing brackets are: (right parenthesis [)], right square bracket []])
-------------------------------------------------------------------------------------
:param: single character closing-bracket or closing-parenthesis accepted.
'''
class IsRightBracketTest(unittest.TestCase):
#Pass a single closing bracket
def test_single_right_bracket(self):
self.assertTrue(sy.isRightBracket(']'))
#Pass a single opening parenthesis
def test_single_left_parenthesis(self):
self.assertFalse(sy.isRightBracket('('))
#Pass a single opening bracket
def test_single_left_bracket(self):
self.assertFalse(sy.isRightBracket('['))
#Pass multiple closing brackets
def test_multiple_right_brackets(self):
with self.assertRaises(ValueError):
sy.isRightBracket(')]')
#Pass a non-closing bracket
def test_non_left_bracket(self):
self.assertFalse(sy.isRightBracket('a'))
#Pass an empty char
def test_empty_char(self):
with self.assertRaises(ValueError):
sy.isRightBracket('')
#Pass an integer type
def test_wrong_input_type(self):
with self.assertRaises(TypeError):
sy.isRightBracket(1)
'''
IsNumberTest tests the helper function 'isNumber(arg1)' in shunting_yard.py.
'isNumber(arg1)' should return true if passed arguments are valid integer numbers.
-------------------------------------------------------------------------------------
:param: single or multi-digit numbers string-characters accepted
'''
class IsNumberTest(unittest.TestCase):
#Pass a single digit
def test_single_digit(self):
self.assertTrue(sy.isNumber('0'))
#Pass a large number
def test_large_digit(self):
self.assertTrue(sy.isNumber('420'))
#Pass a non-digit
def test_non_digit(self):
self.assertFalse(sy.isNumber('A113'))
#Pass an empty char
def test_empty_char(self):
with self.assertRaises(ValueError):
sy.isNumber('')
#Pass an integer type
def test_wrong_input_type(self):
with self.assertRaises(TypeError):
sy.isNumber(1)
'''
PeekAtStackTest tests the helper function 'peekAtStack(arg1)' in shunting_yard.py.
'peekAtStack(arg1)' should return a copy of the top-most (most recently added) item
from the given stack, arg1.
-------------------------------------------------------------------------------------
:param: python list
'''
class PeekAtStackTest(unittest.TestCase):
#Get the most recently added item from a stack containing already three items.
def test_get_from_non_empty_stack(self):
testStack = []
testStack.insert(0, 'WALL-E')
testStack.insert(0, '2001: A Space Odyssey')
testStack.insert(0, 'Monsters Inc.')
mostRecentItem = sy.peekAtStack(testStack)
self.assertEqual(mostRecentItem, 'Monsters Inc.', 'Top-most item not copied from stack')
#Get the most recently added item from an empty stack.
def test_get_from_empty_stack(self):
testStack = []
with self.assertRaises(IndexError):
sy.peekAtStack(testStack)
#Send a wrong type into the arguments.
def test_pass_wrong_arg(self):
with self.assertRaises(TypeError):
sy.peekAtStack('Hello, world!')
'''
PopFromStackTest tests the helper function 'popFromStack(arg1)' in shunting_yard.py.
'popFromStack(arg1)' should remove and return the top-most (most recently added) item
from the given stack, arg1.
-------------------------------------------------------------------------------------
:param: python list
'''
class PopFromStackTest(unittest.TestCase):
#Pop the most recently added item from a stack containing already three items.
def test_pop_from_non_empty_stack(self):
testStack = []
testStack.insert(0, 'Things you should know for the exam: ')
testStack.insert(0, 'Everything')
testStack.insert(0, 'Everything + 1')
self.assertEqual(sy.popFromStack(testStack), 'Everything + 1', 'Top-most item not copied from stack')
self.assertEqual(len(testStack), 2, 'Top-most item not popped (removed) from stack')
#Pop from an empty stack
def test_pop_from_empty_stack(self):
testStack = []
with self.assertRaises(IndexError):
sy.popFromStack(testStack)
#Send wrong type into arguments
def test_pass_wrong_arg(self):
with self.assertRaises(TypeError):
sy.peekAtStack('Hello, World')
'''
PushToStackTest tests the helper function 'pushToStack(arg1, arg2)' in shunting_yard.py.
'pushToStack(arg1, arg2)' should add an item to the top of the stack
-------------------------------------------------------------------------------------
:param: python list
'''
class PushToStackTest(unittest.TestCase):
#Push (add) an item to an empty stack
def test_push_to_empty_stack(self):
testStack = []
sy.pushToStack(testStack, 'Quotes')
self.assertEqual(len(testStack), 1, 'Stack does not contain 1 item.')
#Push (add) an item to a non-empty stack
def test_push_to_non_empty_stack(self):
testStack = ['Minas Tirith', 'Minas Ithil', 'Barad-dûr']
sy.pushToStack(testStack, 'Isengard')
self.assertEqual(len(testStack), 4, 'Stack does not contain 4 items')
self.assertEqual(testStack[0], 'Isengard')
self.assertEqual(testStack[1], 'Minas Tirith')
#Pass wrong argument types
def test_pass_wrong_arg(self):
with self.assertRaises(AttributeError):
sy.pushToStack(0, 'World')
'''
StackIsEmptyTest tests the helper function 'stackIsEmpty(arg1)' in shunting_yard.py
'stackIsEmpty(arg1)' should return true if the stack is empty, and false otherwise.
-------------------------------------------------------------------------------------
:param: python list
'''
class StackIsEmptyTest(unittest.TestCase):
#Test an empty stack
def test_empty_stack(self):
testStack = []
self.assertTrue(sy.stackIsEmpty(testStack))
#Test non-empty stack
def test_non_empty_stack(self):
testStack = ['definently not empty']
self.assertFalse(sy.stackIsEmpty(testStack))
#Send wrong types into arguments
def test_pass_wrong_arg(self):
testStack = []
with self.assertRaises(TypeError):
sy.stackIsEmpty()
with self.assertRaises(TypeError):
sy.stackIsEmpty('')