89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
import unittest
|
|
from calc.lexer import tokenize, Token, LexError
|
|
|
|
|
|
def kinds(src):
|
|
return [t.kind for t in tokenize(src)]
|
|
|
|
|
|
def tok(src):
|
|
return [(t.kind, t.value) for t in tokenize(src)]
|
|
|
|
|
|
class TestNumbers(unittest.TestCase):
|
|
def test_integer(self):
|
|
result = tokenize("42")
|
|
self.assertEqual(result[0], Token('NUMBER', 42))
|
|
self.assertEqual(result[1].kind, 'EOF')
|
|
self.assertIsInstance(result[0].value, int)
|
|
|
|
def test_float_standard(self):
|
|
result = tokenize("3.14")
|
|
self.assertAlmostEqual(result[0].value, 3.14)
|
|
self.assertEqual(result[0].kind, 'NUMBER')
|
|
|
|
def test_float_leading_dot(self):
|
|
result = tokenize(".5")
|
|
self.assertAlmostEqual(result[0].value, 0.5)
|
|
|
|
def test_float_trailing_dot(self):
|
|
result = tokenize("10.")
|
|
self.assertAlmostEqual(result[0].value, 10.0)
|
|
self.assertIsInstance(result[0].value, float)
|
|
|
|
|
|
class TestOperatorsAndParens(unittest.TestCase):
|
|
def test_operators(self):
|
|
self.assertEqual(kinds("1+2*3"), ['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'])
|
|
|
|
def test_minus_slash(self):
|
|
ks = kinds("4-2/1")
|
|
self.assertIn('MINUS', ks)
|
|
self.assertIn('SLASH', ks)
|
|
|
|
def test_parens(self):
|
|
ks = kinds("(1)")
|
|
self.assertIn('LPAREN', ks)
|
|
self.assertIn('RPAREN', ks)
|
|
|
|
def test_complex_expr(self):
|
|
result = tok("3.5*(1-2)")
|
|
expected_kinds = ['NUMBER', 'STAR', 'LPAREN', 'NUMBER', 'MINUS', 'NUMBER', 'RPAREN', 'EOF']
|
|
self.assertEqual([k for k, v in result], expected_kinds)
|
|
self.assertAlmostEqual(result[0][1], 3.5)
|
|
|
|
|
|
class TestWhitespaceAndErrors(unittest.TestCase):
|
|
def test_whitespace_skipped(self):
|
|
result = tok(" 12 + 3 ")
|
|
self.assertEqual([k for k, v in result], ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
|
|
self.assertEqual(result[0][1], 12)
|
|
self.assertEqual(result[2][1], 3)
|
|
|
|
def test_tab_whitespace(self):
|
|
result = kinds("\t1\t+\t2\t")
|
|
self.assertEqual(result, ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
|
|
|
|
def test_at_raises_lex_error(self):
|
|
with self.assertRaises(LexError) as ctx:
|
|
tokenize("1 @ 2")
|
|
self.assertIn('@', str(ctx.exception))
|
|
|
|
def test_dollar_raises_lex_error(self):
|
|
with self.assertRaises(LexError):
|
|
tokenize("$10")
|
|
|
|
def test_letter_raises_lex_error(self):
|
|
with self.assertRaises(LexError):
|
|
tokenize("abc")
|
|
|
|
def test_error_reports_position(self):
|
|
with self.assertRaises(LexError) as ctx:
|
|
tokenize("1 @ 2")
|
|
msg = str(ctx.exception)
|
|
self.assertIn('2', msg)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|