def test_parse_params(s, expectations): parsed = parse_params(read(s)) assert tuple(sym.name for sym in parsed.required) == expectations['required'] assert tuple(sym.name for sym in parsed.optional) == expectations['optional'] if expectations['rest'] is None: assert parsed.rest is None else: assert parsed.rest.name == expectations['rest']
def test_read_unclosed_list(): with raises(EOF): read("(")
def test_read_special_form(): assert read('def') == Symbol('def')
def test_read_qualified_symbol(): assert read('foo/bar') == Symbol('bar', 'foo')
def test_read_unqualified_symbol(): assert read('foo-bar') == Symbol('foo-bar', 'testing')
def test_read_string_with_invalid_escape_sequence(): with raises(InvalidEscapeSequence): read('"h\\ello"')
def test_unbound_symbol_raises_error(evaluator): with raises(UnboundSymbol): evaluator.evaluate(read('foo'))
def test_read_list(): obj = read('(foo 42 bar)') assert isinstance(obj, List) assert obj[0] == Symbol('foo', 'testing') assert obj[1] == 42 assert obj[2] == Symbol('bar', 'testing')
def test_read_string(): assert read('"hello"') == 'hello'
def test_parse_invalid_lambda(): with raises(ParseError): parse_lambda(read('(lambda ((3)))'))
def test_parse_def(): parsed = parse_def(read('(def foo 42)')) assert parsed.symbol.name == 'foo' assert parsed.value == 42
def test_parse_lambda(): parsed = parse_lambda(read('(lambda (foo) bar)')) assert isinstance(parsed, Lambda) assert isinstance(parsed.params, Params) assert tuple(parsed.body) == ((Symbol('bar', 'testing'), ))
def test_parse_invalid_def(): with raises(ParseError): parse_def(read('(def foo 42 bar)'))
def test_python_literal(evaluator): assert evaluator.evaluate(read('py/Exception')) == Exception
def test_read_unbalanced_delimiter(): with raises(UnbalancedDelimiter): read(")")
def test_read_unterminated_string(): with raises(EOF): read('"hello')
def test_read_empty_list(): obj = read('()') assert isinstance(obj, List)
def test_read_string_with_escaped_quotes(): assert read('"\\"hello\\""') == '"hello"'
def test_read_int(): assert read('42') == 42
def test_lambda_invocation_wrong_arity(evaluator): with raises(WrongArity): evaluator.evaluate(read('((lambda ()) 1)'))