artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
47
calculators/builder-adversary/run-03/calc/evaluator.py
Normal file
47
calculators/builder-adversary/run-03/calc/evaluator.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""Evaluator for the arithmetic AST produced by calc.parser.
|
||||
|
||||
Result-type rule: if a computation yields a float that is whole-valued
|
||||
(e.g. 4/2 == 2.0), it is coerced to int before returning. Non-whole
|
||||
floats (e.g. 7/2 == 3.5) are returned as float.
|
||||
"""
|
||||
from .parser import BinOp, Num, Unary
|
||||
|
||||
|
||||
class EvalError(Exception):
|
||||
"""Raised on a runtime evaluation error (e.g. division by zero)."""
|
||||
|
||||
|
||||
def _coerce(value):
|
||||
if isinstance(value, float) and value == int(value):
|
||||
return int(value)
|
||||
return value
|
||||
|
||||
|
||||
def evaluate(node):
|
||||
"""Walk an AST node and return int | float.
|
||||
|
||||
Raises:
|
||||
EvalError: on division by zero.
|
||||
"""
|
||||
if isinstance(node, Num):
|
||||
return _coerce(node.value)
|
||||
|
||||
if isinstance(node, Unary):
|
||||
return _coerce(-evaluate(node.operand))
|
||||
|
||||
if isinstance(node, BinOp):
|
||||
left = evaluate(node.left)
|
||||
right = evaluate(node.right)
|
||||
op = node.op
|
||||
if op == '+':
|
||||
return _coerce(left + right)
|
||||
if op == '-':
|
||||
return _coerce(left - right)
|
||||
if op == '*':
|
||||
return _coerce(left * right)
|
||||
if op == '/':
|
||||
if right == 0:
|
||||
raise EvalError("division by zero")
|
||||
return _coerce(left / right)
|
||||
|
||||
raise EvalError(f"unknown node type: {type(node).__name__}")
|
||||
64
calculators/builder-adversary/run-03/calc/lexer.py
Normal file
64
calculators/builder-adversary/run-03/calc/lexer.py
Normal file
@ -0,0 +1,64 @@
|
||||
"""Lexer for arithmetic expressions."""
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
|
||||
class LexError(Exception):
|
||||
"""Raised when the lexer encounters an invalid character."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Token:
|
||||
kind: str
|
||||
value: Union[int, float, None] = None
|
||||
|
||||
def __repr__(self):
|
||||
if self.value is None:
|
||||
return self.kind
|
||||
return f"{self.kind}({self.value})"
|
||||
|
||||
|
||||
_SINGLE = {
|
||||
'+': 'PLUS',
|
||||
'-': 'MINUS',
|
||||
'*': 'STAR',
|
||||
'/': 'SLASH',
|
||||
'(': 'LPAREN',
|
||||
')': 'RPAREN',
|
||||
}
|
||||
|
||||
|
||||
def tokenize(src: str) -> list:
|
||||
tokens = []
|
||||
i = 0
|
||||
while i < len(src):
|
||||
ch = src[i]
|
||||
|
||||
if ch in ' \t':
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if ch in _SINGLE:
|
||||
tokens.append(Token(_SINGLE[ch]))
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if ch.isdigit() or ch == '.':
|
||||
j = i
|
||||
has_dot = False
|
||||
while j < len(src) and (src[j].isdigit() or (src[j] == '.' and not has_dot)):
|
||||
if src[j] == '.':
|
||||
has_dot = True
|
||||
j += 1
|
||||
raw = src[i:j]
|
||||
if raw == '.':
|
||||
raise LexError(f"invalid character '.' at position {i}")
|
||||
value = float(raw) if has_dot else int(raw)
|
||||
tokens.append(Token('NUMBER', value))
|
||||
i = j
|
||||
continue
|
||||
|
||||
raise LexError(f"invalid character {ch!r} at position {i}")
|
||||
|
||||
tokens.append(Token('EOF'))
|
||||
return tokens
|
||||
123
calculators/builder-adversary/run-03/calc/parser.py
Normal file
123
calculators/builder-adversary/run-03/calc/parser.py
Normal file
@ -0,0 +1,123 @@
|
||||
"""Recursive-descent parser for arithmetic expressions.
|
||||
|
||||
AST nodes:
|
||||
Num(value) — numeric literal
|
||||
BinOp(op, left, right) — binary op; op in {'+', '-', '*', '/'}
|
||||
Unary(op, operand) — unary op; op == '-'
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
from .lexer import Token
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
"""Raised on malformed input."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Union[int, float]
|
||||
|
||||
def __repr__(self):
|
||||
return f"Num({self.value!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: object
|
||||
right: object
|
||||
|
||||
def __repr__(self):
|
||||
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: object
|
||||
|
||||
def __repr__(self):
|
||||
return f"Unary({self.op!r}, {self.operand!r})"
|
||||
|
||||
|
||||
_KIND_TO_OP = {
|
||||
'PLUS': '+',
|
||||
'MINUS': '-',
|
||||
'STAR': '*',
|
||||
'SLASH': '/',
|
||||
}
|
||||
|
||||
|
||||
class _Parser:
|
||||
def __init__(self, tokens: list):
|
||||
self._tokens = tokens
|
||||
self._pos = 0
|
||||
|
||||
def _peek(self) -> Token:
|
||||
return self._tokens[self._pos]
|
||||
|
||||
def _consume(self, kind: str = None) -> Token:
|
||||
tok = self._tokens[self._pos]
|
||||
if kind is not None and tok.kind != kind:
|
||||
raise ParseError(f"expected {kind!r}, got {tok.kind!r}")
|
||||
self._pos += 1
|
||||
return tok
|
||||
|
||||
def parse(self):
|
||||
if self._peek().kind == 'EOF':
|
||||
raise ParseError("empty expression")
|
||||
node = self._expr()
|
||||
if self._peek().kind != 'EOF':
|
||||
raise ParseError(f"unexpected token {self._peek().kind!r}")
|
||||
return node
|
||||
|
||||
def _expr(self):
|
||||
node = self._term()
|
||||
while self._peek().kind in ('PLUS', 'MINUS'):
|
||||
op = _KIND_TO_OP[self._consume().kind]
|
||||
right = self._term()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _term(self):
|
||||
node = self._unary()
|
||||
while self._peek().kind in ('STAR', 'SLASH'):
|
||||
op = _KIND_TO_OP[self._consume().kind]
|
||||
right = self._unary()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _unary(self):
|
||||
if self._peek().kind == 'MINUS':
|
||||
self._consume()
|
||||
return Unary('-', self._unary())
|
||||
return self._primary()
|
||||
|
||||
def _primary(self):
|
||||
tok = self._peek()
|
||||
if tok.kind == 'NUMBER':
|
||||
self._consume()
|
||||
return Num(tok.value)
|
||||
if tok.kind == 'LPAREN':
|
||||
self._consume()
|
||||
node = self._expr()
|
||||
if self._peek().kind != 'RPAREN':
|
||||
raise ParseError("unclosed parenthesis, expected ')'")
|
||||
self._consume()
|
||||
return node
|
||||
if tok.kind == 'EOF':
|
||||
raise ParseError("unexpected end of input")
|
||||
raise ParseError(f"unexpected token {tok.kind!r}")
|
||||
|
||||
|
||||
def parse(tokens: list):
|
||||
"""Parse a token list into an AST.
|
||||
|
||||
Returns:
|
||||
Num | BinOp | Unary — root node.
|
||||
|
||||
Raises:
|
||||
ParseError: on any malformed input.
|
||||
"""
|
||||
return _Parser(tokens).parse()
|
||||
140
calculators/builder-adversary/run-03/calc/test_evaluator.py
Normal file
140
calculators/builder-adversary/run-03/calc/test_evaluator.py
Normal file
@ -0,0 +1,140 @@
|
||||
"""Tests for calc.evaluator — covers eval/D1 through eval/D4 (CLI)."""
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from .lexer import tokenize
|
||||
from .parser import parse
|
||||
from .evaluator import evaluate, EvalError
|
||||
|
||||
CLI = str(pathlib.Path(__file__).parent.parent / "calc.py")
|
||||
|
||||
|
||||
def ev(src):
|
||||
return evaluate(parse(tokenize(src)))
|
||||
|
||||
|
||||
class TestArithmetic(unittest.TestCase):
|
||||
"""D1 — correct arithmetic with precedence, parens, unary minus."""
|
||||
|
||||
def test_add_mul_precedence(self):
|
||||
self.assertEqual(ev("2+3*4"), 14)
|
||||
|
||||
def test_paren_overrides(self):
|
||||
self.assertEqual(ev("(2+3)*4"), 20)
|
||||
|
||||
def test_left_associative_sub(self):
|
||||
self.assertEqual(ev("8-3-2"), 3)
|
||||
|
||||
def test_unary_leading(self):
|
||||
self.assertEqual(ev("-2+5"), 3)
|
||||
|
||||
def test_unary_after_mul(self):
|
||||
self.assertEqual(ev("2*-3"), -6)
|
||||
|
||||
def test_simple_add(self):
|
||||
self.assertEqual(ev("1+2"), 3)
|
||||
|
||||
def test_simple_sub(self):
|
||||
self.assertEqual(ev("5-3"), 2)
|
||||
|
||||
def test_double_unary(self):
|
||||
self.assertEqual(ev("--5"), 5)
|
||||
|
||||
def test_nested_parens(self):
|
||||
self.assertEqual(ev("((3+2))*4"), 20)
|
||||
|
||||
|
||||
class TestDivision(unittest.TestCase):
|
||||
"""D2 — true division and EvalError on division by zero."""
|
||||
|
||||
def test_true_division(self):
|
||||
self.assertEqual(ev("7/2"), 3.5)
|
||||
|
||||
def test_division_by_zero(self):
|
||||
with self.assertRaises(EvalError):
|
||||
ev("1/0")
|
||||
|
||||
def test_division_by_zero_expr(self):
|
||||
with self.assertRaises(EvalError):
|
||||
ev("5/(3-3)")
|
||||
|
||||
def test_not_bare_zerodiv(self):
|
||||
try:
|
||||
ev("1/0")
|
||||
except EvalError:
|
||||
pass
|
||||
except ZeroDivisionError:
|
||||
self.fail("bare ZeroDivisionError escaped; expected EvalError")
|
||||
|
||||
|
||||
class TestResultType(unittest.TestCase):
|
||||
"""D3 — whole-valued results are int; non-whole are float."""
|
||||
|
||||
def test_whole_division_is_int(self):
|
||||
result = ev("4/2")
|
||||
self.assertEqual(result, 2)
|
||||
self.assertIsInstance(result, int)
|
||||
|
||||
def test_non_whole_division_is_float(self):
|
||||
result = ev("7/2")
|
||||
self.assertEqual(result, 3.5)
|
||||
self.assertIsInstance(result, float)
|
||||
|
||||
def test_integer_arithmetic_stays_int(self):
|
||||
result = ev("2+3*4")
|
||||
self.assertIsInstance(result, int)
|
||||
|
||||
def test_whole_float_literal(self):
|
||||
result = ev("4.0/2")
|
||||
self.assertIsInstance(result, int)
|
||||
self.assertEqual(result, 2)
|
||||
|
||||
|
||||
class TestCLI(unittest.TestCase):
|
||||
"""D4 — CLI prints correct output, exits correctly, errors to stderr."""
|
||||
|
||||
def _run(self, expr):
|
||||
return subprocess.run(
|
||||
[sys.executable, CLI, expr],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
|
||||
def test_precedence(self):
|
||||
r = self._run("2+3*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "14")
|
||||
|
||||
def test_parens(self):
|
||||
r = self._run("(2+3)*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "20")
|
||||
|
||||
def test_true_division(self):
|
||||
r = self._run("7/2")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "3.5")
|
||||
|
||||
def test_whole_division(self):
|
||||
r = self._run("4/2")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "2")
|
||||
|
||||
def test_invalid_expr_exits_nonzero(self):
|
||||
r = self._run("1 +")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertTrue(r.stderr.strip(), "expected error on stderr")
|
||||
|
||||
def test_div_by_zero_exits_nonzero(self):
|
||||
r = self._run("1/0")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertTrue(r.stderr.strip(), "expected error on stderr")
|
||||
|
||||
def test_no_traceback_on_error(self):
|
||||
r = self._run("1 +")
|
||||
self.assertNotIn("Traceback", r.stderr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
111
calculators/builder-adversary/run-03/calc/test_lexer.py
Normal file
111
calculators/builder-adversary/run-03/calc/test_lexer.py
Normal file
@ -0,0 +1,111 @@
|
||||
"""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()
|
||||
134
calculators/builder-adversary/run-03/calc/test_parser.py
Normal file
134
calculators/builder-adversary/run-03/calc/test_parser.py
Normal file
@ -0,0 +1,134 @@
|
||||
"""Tests for calc.parser — covers D1 through D5."""
|
||||
import unittest
|
||||
from .lexer import tokenize
|
||||
from .parser import parse, ParseError, Num, BinOp, Unary
|
||||
|
||||
|
||||
def p(src):
|
||||
return parse(tokenize(src))
|
||||
|
||||
|
||||
class TestPrecedence(unittest.TestCase):
|
||||
"""D1 — * and / bind tighter than + and -."""
|
||||
|
||||
def test_add_mul(self):
|
||||
# 1+2*3 → BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
|
||||
self.assertEqual(p("1+2*3"), BinOp('+', Num(1), BinOp('*', Num(2), Num(3))))
|
||||
|
||||
def test_mul_add(self):
|
||||
# 2*3+1 → BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))
|
||||
self.assertEqual(p("2*3+1"), BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)))
|
||||
|
||||
def test_sub_div(self):
|
||||
# 10-4/2 → BinOp('-', Num(10), BinOp('/', Num(4), Num(2)))
|
||||
self.assertEqual(p("10-4/2"), BinOp('-', Num(10), BinOp('/', Num(4), Num(2))))
|
||||
|
||||
def test_single_number(self):
|
||||
self.assertEqual(p("42"), Num(42))
|
||||
|
||||
def test_single_float(self):
|
||||
self.assertEqual(p("3.5"), Num(3.5))
|
||||
|
||||
|
||||
class TestLeftAssociativity(unittest.TestCase):
|
||||
"""D2 — same-precedence operators associate left."""
|
||||
|
||||
def test_sub_left(self):
|
||||
# 8-3-2 → BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
|
||||
self.assertEqual(p("8-3-2"), BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)))
|
||||
|
||||
def test_div_left(self):
|
||||
# 8/4/2 → BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
|
||||
self.assertEqual(p("8/4/2"), BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)))
|
||||
|
||||
def test_add_left(self):
|
||||
# 1+2+3 → BinOp('+', BinOp('+', Num(1), Num(2)), Num(3))
|
||||
self.assertEqual(p("1+2+3"), BinOp('+', BinOp('+', Num(1), Num(2)), Num(3)))
|
||||
|
||||
def test_mul_left(self):
|
||||
# 2*3*4 → BinOp('*', BinOp('*', Num(2), Num(3)), Num(4))
|
||||
self.assertEqual(p("2*3*4"), BinOp('*', BinOp('*', Num(2), Num(3)), Num(4)))
|
||||
|
||||
|
||||
class TestParentheses(unittest.TestCase):
|
||||
"""D3 — parentheses override precedence."""
|
||||
|
||||
def test_paren_overrides_mul(self):
|
||||
# (1+2)*3 → BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))
|
||||
self.assertEqual(p("(1+2)*3"), BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)))
|
||||
|
||||
def test_paren_overrides_div(self):
|
||||
# 6/(1+2) → BinOp('/', Num(6), BinOp('+', Num(1), Num(2)))
|
||||
self.assertEqual(p("6/(1+2)"), BinOp('/', Num(6), BinOp('+', Num(1), Num(2))))
|
||||
|
||||
def test_nested_parens(self):
|
||||
# ((2+3)) → BinOp('+', Num(2), Num(3))
|
||||
self.assertEqual(p("((2+3))"), BinOp('+', Num(2), Num(3)))
|
||||
|
||||
def test_paren_single(self):
|
||||
self.assertEqual(p("(5)"), Num(5))
|
||||
|
||||
|
||||
class TestUnaryMinus(unittest.TestCase):
|
||||
"""D4 — unary minus."""
|
||||
|
||||
def test_leading(self):
|
||||
self.assertEqual(p("-5"), Unary('-', Num(5)))
|
||||
|
||||
def test_paren_group(self):
|
||||
# -(1+2) → Unary('-', BinOp('+', Num(1), Num(2)))
|
||||
self.assertEqual(p("-(1+2)"), Unary('-', BinOp('+', Num(1), Num(2))))
|
||||
|
||||
def test_after_mul(self):
|
||||
# 3 * -2 → BinOp('*', Num(3), Unary('-', Num(2)))
|
||||
self.assertEqual(p("3 * -2"), BinOp('*', Num(3), Unary('-', Num(2))))
|
||||
|
||||
def test_double_unary(self):
|
||||
# --5 → Unary('-', Unary('-', Num(5)))
|
||||
self.assertEqual(p("--5"), Unary('-', Unary('-', Num(5))))
|
||||
|
||||
def test_unary_in_add(self):
|
||||
# 1 + -2 → BinOp('+', Num(1), Unary('-', Num(2)))
|
||||
self.assertEqual(p("1 + -2"), BinOp('+', Num(1), Unary('-', Num(2))))
|
||||
|
||||
|
||||
class TestErrors(unittest.TestCase):
|
||||
"""D5 — malformed input raises ParseError."""
|
||||
|
||||
def test_trailing_op(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 +")
|
||||
|
||||
def test_unclosed_paren(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("(1")
|
||||
|
||||
def test_two_numbers(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 2")
|
||||
|
||||
def test_close_then_open(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p(")(")
|
||||
|
||||
def test_empty(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("")
|
||||
|
||||
def test_only_op(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("+")
|
||||
|
||||
def test_mismatched_parens(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("(1+2")
|
||||
|
||||
def test_parse_error_not_other(self):
|
||||
for src in ("1 +", "(1", "1 2", ")(", ""):
|
||||
with self.subTest(src=src):
|
||||
with self.assertRaises(ParseError):
|
||||
p(src)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user