Skip to content

Commit 66bdc4a

Browse files
committed
First batch of unit tests added for the lexer.py
Added a tests folder, test_lexer.py file and selected the 'unittest' framework in project settings (.vscode\settings.json).
1 parent 69539ac commit 66bdc4a

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

.vscode/settings.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"python.testing.unittestArgs": [
3+
"-v",
4+
"-s",
5+
"./tests",
6+
"-p",
7+
"test_*.py"
8+
],
9+
"python.testing.pytestEnabled": false,
10+
"python.testing.unittestEnabled": true
11+
}

dictionary.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class Dictionary:
3131

3232
multi_word_operator_parts = ['is', 'not', 'equal', 'to', 'greater', 'or', 'less', 'than']
3333

34-
# TODO: Can this be optimized?
3534
multi_word_operators = {
3635
'is equal to': EQUAL_TO,
3736
'is not equal to': NOT_EQUAL_TO,
@@ -40,7 +39,8 @@ class Dictionary:
4039
'is greater than or equal to': GREATER_THAN_OR_EQUAL_TO,
4140
'is less than or equal to': LESS_THAN_OR_EQUAL_TO
4241
}
43-
42+
43+
# TODO: Tokenization should probably be handled within the method itself in the Lexer
4444
arithmetic_operators = {
4545
'+': Token(PLUS),
4646
'-': Token(MINUS),
@@ -52,7 +52,7 @@ class Dictionary:
5252
'is': Token(ASSIGNMENT)
5353
}
5454

55-
# TODO: Isn't this redundant?
55+
# TODO: Tokenization should probably be handled within the method itself in the Lexer
5656
multi_word_operators_dictionary = {
5757
'is equal to': Token(EQUAL_TO),
5858
'is not equal to': Token(NOT_EQUAL_TO),
@@ -62,6 +62,7 @@ class Dictionary:
6262
'is less than or equal to': Token(LESS_THAN_OR_EQUAL_TO)
6363
}
6464

65+
# TODO: Tokenization should probably be handled within the method itself in the Lexer
6566
escape_characters = {
6667
'\n': Token(NEWLINE),
6768
'\t': Token(INDENTATION),

lexer.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ def handle_excel_cell(self):
180180
cell_reference = cell_reference.strip()
181181

182182
return Token(Dictionary.CELL, cell_reference)
183-
183+
184+
# TODO: We should probably return the token from within this method instead of from inside the dictionary,
185+
# as well as a token value (instead of None)
184186
# Tokenize white spaces and escape characters (newline and tab)
185187
def escape_tokenize(self):
186188
return Dictionary.escape_characters.get(self.current_character)

tests/test_lexer.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# As of right now all tests can be run by either one of the following commands in the terminal:
2+
# python -m unittest discover -s ./tests -p test_*.py
3+
# python -m unittest discover -s tests
4+
5+
import unittest
6+
from unittest import TestCase
7+
8+
import lexer
9+
from lexer import Lexer
10+
11+
class TestLexer(TestCase):
12+
def setUp(self):
13+
self.lexer = Lexer("Test")
14+
15+
def tearDown(self):
16+
self.lexer = None
17+
18+
def test_current_character_in_focus_should_be_T(self):
19+
self.assertEqual(self.lexer.current_character, 'T')
20+
21+
def test_peeked_character_should_be_e(self):
22+
self.assertEqual(self.lexer.peek(), 'e')
23+
24+
def test_next_character_in_focus_should_be_s(self):
25+
self.lexer.next_character()
26+
self.lexer.next_character()
27+
self.assertEqual(self.lexer.current_character, 's')
28+
29+
def test_advance_3_characters_should_be_t(self):
30+
self.lexer.advance_n(3)
31+
self.assertEqual(self.lexer.current_character, 't')
32+
33+
def test_peeked_word_ahead_should_be_Batman(self):
34+
self.lexer = Lexer(" Batman")
35+
self.assertEqual(self.lexer.peek_word_ahead(), "Batman")
36+
37+
def test_digit_tokenize_should_return_integer_42_token(self):
38+
self.lexer = Lexer("42")
39+
token = self.lexer.digit_tokenize()
40+
self.assertTrue(token.type, "INTEGER")
41+
self.assertEqual(token.value, 42)
42+
43+
def test_digit_tokenize_should_return_float_3_14_token(self):
44+
self.lexer = Lexer("3.14")
45+
token = self.lexer.digit_tokenize()
46+
self.assertTrue(token.type, "FLOAT")
47+
self.assertEqual(token.value, 3.14)
48+
49+
def test_escape_character_token_should_be_newline(self):
50+
self.lexer = Lexer('\n')
51+
token = self.lexer.escape_tokenize()
52+
self.assertTrue(token.type, "NEWLINE")
53+
self.assertEqual(token.value, None)
54+
55+
if __name__ == '__main__':
56+
unittest.main()

0 commit comments

Comments
 (0)