Files
agent-orchestrator-benchmark/calculators/builder-adversary/run-01/calc/test_lexer.py

135 lines
4.0 KiB
Python

"""Unit tests for calc.lexer — 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 values(src):
return [(t.kind, t.value) for t in tokenize(src)]
class TestNumbers(unittest.TestCase):
"""D1 — integers and floats."""
def test_integer(self):
toks = tokenize("42")
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(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)
def test_integer_eof(self):
toks = tokenize("42")
self.assertEqual(kinds("42"), ['NUMBER', 'EOF'])
class TestOperatorsAndParens(unittest.TestCase):
"""D2 — operators and parentheses."""
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_expr_kinds(self):
self.assertEqual(
kinds("1+2*3"),
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'],
)
def test_complex_expr(self):
self.assertEqual(
kinds("3.5*(1-2)"),
['NUMBER', 'STAR', 'LPAREN', 'NUMBER', 'MINUS', 'NUMBER', 'RPAREN', 'EOF'],
)
class TestWhitespaceAndErrors(unittest.TestCase):
"""D3 — whitespace is skipped; invalid chars raise LexError."""
def test_whitespace_skipped(self):
self.assertEqual(
kinds(" 12 + 3 "),
['NUMBER', 'PLUS', 'NUMBER', 'EOF'],
)
def test_tab_whitespace(self):
self.assertEqual(kinds("1\t+\t2"), ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
def test_at_raises_lexerror(self):
with self.assertRaises(LexError) as ctx:
tokenize("1 @ 2")
self.assertIn('@', str(ctx.exception))
def test_dollar_raises_lexerror(self):
with self.assertRaises(LexError) as ctx:
tokenize("$")
self.assertIn('$', str(ctx.exception))
def test_letter_raises_lexerror(self):
with self.assertRaises(LexError) as ctx:
tokenize("x")
self.assertIn('x', str(ctx.exception))
def test_lexerror_includes_position(self):
with self.assertRaises(LexError) as ctx:
tokenize("1 @ 2")
# position 2 (0-indexed) where '@' appears
self.assertIn('2', str(ctx.exception))
def test_required_whitespace_expr(self):
# " 12 + 3 " from the DoD
toks = tokenize(" 12 + 3 ")
self.assertEqual(toks[0].value, 12)
self.assertEqual(toks[2].value, 3)
def test_required_complex_expr(self):
# "3.5*(1-2)" from the DoD
toks = tokenize("3.5*(1-2)")
self.assertAlmostEqual(toks[0].value, 3.5)
self.assertEqual(toks[1].kind, 'STAR')
self.assertEqual(toks[2].kind, 'LPAREN')
self.assertEqual(toks[3].value, 1)
self.assertEqual(toks[4].kind, 'MINUS')
self.assertEqual(toks[5].value, 2)
self.assertEqual(toks[6].kind, 'RPAREN')
if __name__ == '__main__':
unittest.main()