This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_operatorToken.py
443 lines (365 loc) · 20.9 KB
/
test_operatorToken.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import unittest
import operatorToken as operatorT
import myGlobal as dictConstant
import stack
class TestCaseIsOperatorStrValid(unittest.TestCase):
def test_a_isOperatorStrValid_given_PREFIX_MINUS_expect_true(self):
# verifyOperatorStr(operatorStr)
self.assertEqual(True, operatorT.isOperatorStrValid("PREFIX_MINUS"))
def test_b_isOperatorStrValid_given_INFIX_MULTIPLY_expect_true(self):
# verifyOperatorStr(operatorStr)
self.assertEqual(True, operatorT.isOperatorStrValid("INFIX_MULTIPLY"))
def test_c_isOperatorStrValid_given_INFIX_OR_expect_false(self):
# verifyOperatorStr(operatorStr)
self.assertEqual(False, operatorT.isOperatorStrValid("INFIX_OR"))
class TestCaseGetOperatorSymbol(unittest.TestCase):
def test_a_getOperatorSymbol_given_INFIX_PLUS_expect_plus_sign(self):
# getOperatorSymbol(operatorStr)
self.assertEqual('+', operatorT.getOperatorSymbol("INFIX_PLUS"))
def test_b_getOperatorSymbol_given_CLOSE_BRACKET_expect_close_bracket_sign(self):
# getOperatorSymbol(operatorStr)
self.assertEqual(')', operatorT.getOperatorSymbol("CLOSE_BRACKET"))
def test_c_getOperatorSymbol_given_AND_expect_error_message(self):
# getOperatorSymbol(operatorStr)
self.assertEqual("Error: getOperatorSymbol() received invalid operator string.",
operatorT.getOperatorSymbol("AND"))
class TestCaseGetOperatorTokenInfo(unittest.TestCase):
def test_a_getOperatorTokenInfo_given_INFIX_PLUS_expect_LEFT_TO_RIGHT_WEAK(self):
"""
From myGlobal.py
Associativity: LEFT_TO_RIGHT = 4
Precedence: WEAK = 10
"""
# getOperatorTokenInfo(operatorStr)
tokenInfo = operatorT.getOperatorTokenInfo("INFIX_PLUS")
self.assertEqual(dictConstant.LEFT_TO_RIGHT, tokenInfo[dictConstant.Associativity])
self.assertEqual(dictConstant.WEAK, tokenInfo[dictConstant.Precedence])
def test_b_getOperatorTokenInfo_given_PREFIX_MINUS_expect_RIGHT_TO_LEFT_STRONG(self):
"""
From myGlobal.py
Associativity: RIGHT_TO_LEFT = 5
Precedence: STRONG = 12
"""
# getOperatorTokenInfo(operatorStr)
tokenInfo = operatorT.getOperatorTokenInfo("PREFIX_MINUS")
self.assertEqual(dictConstant.RIGHT_TO_LEFT, tokenInfo[dictConstant.Associativity])
self.assertEqual(dictConstant.STRONG, tokenInfo[dictConstant.Precedence])
def test_c_getOperatorTokenInfo_given_invalid_operator_expect_None(self):
# getOperatorTokenInfo(operatorStr)
tokenInfo = operatorT.getOperatorTokenInfo("!")
self.assertEqual(None, tokenInfo)
class TestCaseCreateOperatorToken(unittest.TestCase):
def test_a_createOperatorToken_given_INFIX_DIVIDE_expect_return_token_with_correct_info(self):
"""
From myGlobal.py
Associativity: LEFT_TO_RIGHT = 4
Precedence: MEDIUM = 11
"""
# createOperatorToken(operatorStr)
operatorToken = operatorT.createOperatorToken("INFIX_DIVIDE")
self.assertEqual(dictConstant.LEFT_TO_RIGHT, operatorToken.associativity)
self.assertEqual(dictConstant.MEDIUM, operatorToken.precedence)
self.assertEqual('/', operatorToken.symbol)
self.assertEqual("OPERATOR_TOKEN", operatorToken.tokenType)
def test_b_createOperatorToken_given_OPEN_BRACKET_expect_return_token_with_correct_info(self):
"""
From myGlobal.py
Associativity: LEFT_TO_RIGHT = 4
Precedence: WEAK = 10
"""
# createOperatorToken(operatorStr)
operatorToken = operatorT.createOperatorToken("OPEN_BRACKET")
self.assertEqual(dictConstant.LEFT_TO_RIGHT, operatorToken.associativity)
self.assertEqual(dictConstant.WEAK, operatorToken.precedence)
self.assertEqual('(', operatorToken.symbol)
self.assertEqual("OPERATOR_TOKEN", operatorToken.tokenType)
def test_c_createOperatorToken_given_CURLY_BRACKET_expect_exception_raised(self):
try: # createOperatorToken(operatorStr)
operatorToken = operatorT.createOperatorToken("CURLY_BRACKET")
self.assertEqual(None, operatorToken.associativity)
self.assertEqual(None, operatorToken.precedence)
self.assertEqual(None, operatorToken.symbol)
self.assertEqual("OPERATOR_TOKEN", operatorToken.tokenType)
except Exception as e:
print(e)
class TestCaseGetOperatorSignAffixStr(unittest.TestCase):
def test_a_getPlusSignOperatorStr_given_prev_None_next_minus_expect_PREFIX_PLUS(self):
# getPlusSignOperatorStr(previousChar, nextChar)
self.assertEqual("PREFIX_PLUS", operatorT.getPlusSignOperatorStr(None, "-"))
def test_b_getPlusSignOperatorStr_given_prev_1_next_4_expect_INFIX_PLUS(self):
# getPlusSignOperatorStr(previousChar, nextChar)
self.assertEqual("INFIX_PLUS", operatorT.getPlusSignOperatorStr("1", "4"))
def test_c_getPlusSignOperatorStr_given_prev_None_next_1_expect_PREFIX_PLUS(self):
# getPlusSignOperatorStr(previousChar, nextChar)
self.assertEqual("PREFIX_PLUS", operatorT.getPlusSignOperatorStr(None, "1"))
# Didn't test for getMinusSignOperatorStr() because the code are same as getPlusSignOperatorStr()
def test_d_getOperatorStrAffix_given_prev_None_next_1_minus_expect_PREFIX_MINUS(self):
# getOperatorStrAffix(previousChar, nextChar, operatorSymbol)
self.assertEqual("PREFIX_MINUS", operatorT.getOperatorStrAffix(None, "1", "-"))
def test_e_getOperatorStrAffix_given_prev_1_next_6point11_plus_expect_INFIX_PLUS(self):
# getOperatorStrAffix(previousChar, nextChar, operatorSymbol)
self.assertEqual("INFIX_PLUS", operatorT.getOperatorStrAffix("1", "6.11", "+"))
def test_f_getOperatorStrAffix_given_prev_None_next_None_multiply_expect_INFIX_MULTIPLY(self):
# getOperatorStrAffix(previousChar, nextChar, operatorSymbol)
self.assertEqual("INFIX_MULTIPLY", operatorT.getOperatorStrAffix(None, None, "*"))
def test_g_getOperatorStrAffix_given_prev_10_next_6point11_divide_expect_INFIX_DIVIDE(self):
# getOperatorStrAffix(previousChar, nextChar, operatorSymbol)
self.assertEqual("INFIX_DIVIDE", operatorT.getOperatorStrAffix("10", "6.11", "/"))
def test_h_getOperatorStrAffix_given_prev_None_next_None_open_bracket_expect_OPEN_BRACKET(self):
# getOperatorStrAffix(previousChar, nextChar, operatorSymbol)
self.assertEqual("OPEN_BRACKET", operatorT.getOperatorStrAffix(None, None, "("))
def test_i_getOperatorStrAffix_given_prev_10_next_6point11_close_bracket_expect_CLOSE_BRACKET(self):
# getOperatorStrAffix(previousChar, nextChar, operatorSymbol)
self.assertEqual("CLOSE_BRACKET", operatorT.getOperatorStrAffix("10", "6.11", ")"))
def test_j_getAffixFromOperatorStr_given_PREFIX_MINUS_expect_PREFIX(self):
# getAffixFromOperatorStr(operatorStr)
self.assertEqual("PREFIX", operatorT.getAffixFromOperatorStr("PREFIX_PLUS"))
def test_k_getAffixFromOperatorStr_given_INFIX_DIVIDE_expect_INFIX(self):
# getAffixFromOperatorStr(operatorStr)
self.assertEqual("INFIX", operatorT.getAffixFromOperatorStr("INFIX_DIVIDE"))
"""
class TestIsOperatorInStackHigherPrecedence(unittest.TestCase):
def test_a_isOperatorInStackHigherPrecedence_given_prefix_plus_at_stack_and_infix_plus_as_token_expect_true(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.LEFT_TO_RIGHT
currentToken.precedence = dictConstant.WEAK
currentToken.symbol = '+'
currentToken.affix = "INFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(True, operatorT.isOperatorInStackHigherPrecedence(operatorStack, currentToken))
def test_b_isOperatorInStackHigherPrecedence_given_empty_stack_expect_false(self):
operatorStack = [] # Initialise operator stack
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.LEFT_TO_RIGHT
currentToken.precedence = dictConstant.WEAK
currentToken.symbol = '+'
currentToken.affix = "INFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(False, operatorT.isOperatorInStackHigherPrecedence(operatorStack, currentToken))
def test_c_isOperatorInStackHigherPrecedence_given_infix_plus_at_stack_and_prefix_plus_as_token_expect_false(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.LEFT_TO_RIGHT
currentToken.precedence = dictConstant.WEAK
currentToken.symbol = '+'
currentToken.affix = "INFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(True, operatorT.isOperatorInStackHigherPrecedence(operatorStack, currentToken))
"""
class TestIsOperatorInStackHigherPrecedenceThanCurrentToken(unittest.TestCase):
def test_a_isOperatorInStackHigherPrecedenceThanCurrentToken_given_prefix_plus_at_stack_and_infix_plus_as_token_expect_True(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.LEFT_TO_RIGHT
currentToken.precedence = dictConstant.WEAK
currentToken.symbol = '+'
currentToken.affix = "INFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(True, operatorT.isOperatorInStackHigherPrecedenceThanCurrentToken(operatorStack, currentToken))
def test_b_isOperatorInStackHigherPrecedenceThanCurrentToken_given_prefix_plus_at_stack_and_prefix_minus_as_token_expect_false(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(False, operatorT.isOperatorInStackHigherPrecedenceThanCurrentToken(operatorStack, currentToken))
def test_c_isOperatorInStackHigherPrecedenceThanCurrentToken_given_prefix_plus_at_stack_and_prefix_minus_as_token_expect_false(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(False, operatorT.isOperatorInStackHigherPrecedenceThanCurrentToken(operatorStack, currentToken))
def test_d_isOperatorInStackHigherPrecedenceThanCurrentToken_given_empty_stack_and_valid_operator_token_expect_exception(self):
try:
# isOperatorInStackHigherPrecedence(operatorStack, token)
operatorStack = [] # Initialise operator stack
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
self.assertEqual(False, operatorT.isOperatorInStackHigherPrecedenceThanCurrentToken(operatorStack, currentToken))
except Exception as e:
print(e)
def test_e_isOperatorInStackHigherPrecedenceThanCurrentToken_given_not_empty_stack_and_invalid_operator_token_expect_exception(self):
try:
# isOperatorInStackHigherPrecedence(operatorStack, token)
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "INVALID"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
self.assertEqual(False, operatorT.isOperatorInStackHigherPrecedenceThanCurrentToken(operatorStack, currentToken))
except Exception as e:
print(e)
class TestIsOperatorInStackSamePrecedenceWithCurrentToken(unittest.TestCase):
def test_a_isOperatorInStackSamePrecedenceWithCurrentToken_given_prefix_plus_at_stack_and_infix_plus_as_token_expect_False(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.LEFT_TO_RIGHT
currentToken.precedence = dictConstant.WEAK
currentToken.symbol = '+'
currentToken.affix = "INFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(False, operatorT.isOperatorInStackSamePrecedenceWithCurrentToken(operatorStack, currentToken))
def test_b_isOperatorInStackSamePrecedenceWithCurrentToken_given_prefix_plus_at_stack_and_prefix_minus_as_token_expect_True(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(True, operatorT.isOperatorInStackSamePrecedenceWithCurrentToken(operatorStack, currentToken))
def test_c_isOperatorInStackSamePrecedenceWithCurrentToken_given_prefix_plus_at_stack_and_prefix_minus_as_token_expect_True(self):
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
# isOperatorInStackHigherPrecedence(operatorStack, token)
self.assertEqual(True, operatorT.isOperatorInStackSamePrecedenceWithCurrentToken(operatorStack, currentToken))
def test_d_isOperatorInStackSamePrecedenceWithCurrentToken_given_empty_stack_and_valid_operator_token_expect_exception(self):
# isOperatorInStackHigherPrecedence(operatorStack, token)
operatorStack = [] # Initialise operator stack
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "OPERATOR_TOKEN"
self.assertEqual(False, operatorT.isOperatorInStackSamePrecedenceWithCurrentToken(operatorStack, currentToken))
def test_e_isOperatorInStackSamePrecedenceWithCurrentToken_given_not_empty_stack_and_invalid_operator_token_expect_exception(self):
# isOperatorInStackHigherPrecedence(operatorStack, token)
operatorStack = [] # Initialise operator stack
# Initialise operator token to insert to stack
operatorToken = operatorT.OperatorToken()
operatorToken.associativity = dictConstant.RIGHT_TO_LEFT
operatorToken.precedence = dictConstant.STRONG
operatorToken.symbol = '+'
operatorToken.affix = "PREFIX"
operatorToken.tokenType = "OPERATOR_TOKEN"
# Initialise current token
currentToken = operatorT.OperatorToken()
currentToken.associativity = dictConstant.RIGHT_TO_LEFT
currentToken.precedence = dictConstant.STRONG
currentToken.symbol = '+'
currentToken.affix = "PREFIX"
currentToken.tokenType = "INVALID"
# Push operator token into stack
stack.pushStack(operatorStack, operatorToken)
self.assertEqual(False, operatorT.isOperatorInStackSamePrecedenceWithCurrentToken(operatorStack, currentToken))
if __name__ == '__main__':
unittest.main()