112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
"""Tests for calc/lexer.py — covers D1, D2, D3."""
|
|
import unittest
|
|
from calc.lexer import tokenize, Token, LexError
|
|
|
|
|
|
def kinds(src):
|
|
return [t.kind for t in tokenize(src)]
|
|
|
|
|
|
def vals(src):
|
|
return [(t.kind, t.value) for t in tokenize(src)]
|
|
|
|
|
|
class TestNumbers(unittest.TestCase):
|
|
"""D1 — integers and floats tokenize to NUMBER with numeric value."""
|
|
|
|
def test_integer(self):
|
|
toks = tokenize("42")
|
|
self.assertEqual(len(toks), 2)
|
|
self.assertEqual(toks[0].kind, 'NUMBER')
|
|
self.assertEqual(toks[0].value, 42)
|
|
self.assertIsInstance(toks[0].value, int)
|
|
self.assertEqual(toks[1].kind, 'EOF')
|
|
|
|
def test_float_standard(self):
|
|
toks = tokenize("3.14")
|
|
self.assertEqual(toks[0].kind, 'NUMBER')
|
|
self.assertAlmostEqual(toks[0].value, 3.14)
|
|
self.assertIsInstance(toks[0].value, float)
|
|
|
|
def test_float_leading_dot(self):
|
|
toks = tokenize(".5")
|
|
self.assertEqual(toks[0].kind, 'NUMBER')
|
|
self.assertAlmostEqual(toks[0].value, 0.5)
|
|
self.assertIsInstance(toks[0].value, float)
|
|
|
|
def test_float_trailing_dot(self):
|
|
toks = tokenize("10.")
|
|
self.assertEqual(toks[0].kind, 'NUMBER')
|
|
self.assertAlmostEqual(toks[0].value, 10.0)
|
|
self.assertIsInstance(toks[0].value, float)
|
|
|
|
|
|
class TestOperatorsAndParens(unittest.TestCase):
|
|
"""D2 — operators and parens produce correct kinds."""
|
|
|
|
def test_plus(self):
|
|
self.assertIn('PLUS', kinds('+'))
|
|
|
|
def test_minus(self):
|
|
self.assertIn('MINUS', kinds('-'))
|
|
|
|
def test_star(self):
|
|
self.assertIn('STAR', kinds('*'))
|
|
|
|
def test_slash(self):
|
|
self.assertIn('SLASH', kinds('/'))
|
|
|
|
def test_lparen(self):
|
|
self.assertIn('LPAREN', kinds('('))
|
|
|
|
def test_rparen(self):
|
|
self.assertIn('RPAREN', kinds(')'))
|
|
|
|
def test_expression_1plus2star3(self):
|
|
self.assertEqual(
|
|
kinds("1+2*3"),
|
|
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'],
|
|
)
|
|
|
|
|
|
class TestWhitespaceAndErrors(unittest.TestCase):
|
|
"""D3 — whitespace skipped; invalid chars raise LexError."""
|
|
|
|
def test_whitespace_skipped(self):
|
|
self.assertEqual(
|
|
kinds(" 12 + 3 "),
|
|
['NUMBER', 'PLUS', 'NUMBER', 'EOF'],
|
|
)
|
|
|
|
def test_tab_skipped(self):
|
|
self.assertEqual(kinds("1\t+\t2"), ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
|
|
|
|
def test_complex_expression(self):
|
|
self.assertEqual(
|
|
kinds("3.5*(1-2)"),
|
|
['NUMBER', 'STAR', 'LPAREN', 'NUMBER', 'MINUS', 'NUMBER', 'RPAREN', 'EOF'],
|
|
)
|
|
|
|
def test_invalid_at_sign(self):
|
|
with self.assertRaises(LexError) as ctx:
|
|
tokenize("1 @ 2")
|
|
self.assertIn('@', str(ctx.exception))
|
|
|
|
def test_invalid_dollar(self):
|
|
with self.assertRaises(LexError):
|
|
tokenize("$")
|
|
|
|
def test_invalid_letter(self):
|
|
with self.assertRaises(LexError):
|
|
tokenize("x")
|
|
|
|
def test_error_includes_position(self):
|
|
with self.assertRaises(LexError) as ctx:
|
|
tokenize("1 @ 2")
|
|
# position 2 (0-indexed) is where '@' lives
|
|
self.assertIn('2', str(ctx.exception))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|