def test_lambda(std_env: Environment) -> None: source = '(lambda (a b) (if (>= a b) a b))' func = evaluate(parse(source), std_env) assert func.parms == ['a', 'b'] assert func.body == [['if', ['>=', 'a', 'b'], 'a', 'b']] assert func.env is std_env assert func(1, 2) == 2 assert func(3, 2) == 3
def test_invocation_user_procedure(std_env: Environment) -> None: source = """ (begin (define max (lambda (a b) (if (>= a b) a b))) (max 22 11) ) """ got = evaluate(parse(source), std_env) assert got == 22
def test_begin(std_env: Environment) -> None: source = """ (begin (define x (* 2 3)) (* x 7) ) """ got = evaluate(parse(source), std_env) assert got == 42
def test_define_function(std_env: Environment) -> None: source = '(define (max a b) (if (>= a b) a b))' got = evaluate(parse(source), std_env) assert got is None max_fn = std_env['max'] assert max_fn.parms == ['a', 'b'] assert max_fn.body == [['if', ['>=', 'a', 'b'], 'a', 'b']] assert max_fn.env is std_env assert max_fn(1, 2) == 2 assert max_fn(3, 2) == 3
def test_evaluate_variable() -> None: env: Environment = dict(x=10) source = 'x' expected = 10 got = evaluate(parse(source), env) assert got == expected
def test_evaluate_literal(std_env: Environment) -> None: source = '3.3' expected = 3.3 got = evaluate(parse(source), std_env) assert got == expected
def test_evaluate_variable(): env = dict(x=10) source = 'x' expected = 10 got = evaluate(parse(source), env) assert got == expected
def test_evaluate(source: str, expected: Optional[Expression]) -> None: got = evaluate(parse(source), global_env_for_first_test) assert got == expected
def test_invocation_builtin_append(std_env: Environment) -> None: source = '(append (quote (a b)) (quote (c d)))' got = evaluate(parse(source), std_env) assert got == ['a', 'b', 'c', 'd']
def test_invocation_builtin_map(std_env: Environment) -> None: source = '(map (lambda (x) (* x 2)) (quote (1 2 3))))' got = evaluate(parse(source), std_env) assert got == [2, 4, 6]
def test_evaluate_literal(std_env): source = '3.3' expected = 3.3 got = evaluate(parse(source), std_env) assert got == expected
def test_invocation_builtin_car(std_env: Environment) -> None: source = '(car (quote (11 22 33)))' got = evaluate(parse(source), std_env) assert got == 11
def test_evaluate(source, expected): got = evaluate(parse(source), global_env_for_first_test) assert got == expected
def test_define(std_env: Environment) -> None: source = '(define answer (* 6 7))' got = evaluate(parse(source), std_env) assert got is None assert std_env['answer'] == 42
def test_evaluate_if_false(std_env: Environment) -> None: source = '(if 0 no-such-thing 20)' expected = 20 got = evaluate(parse(source), std_env) assert got == expected
def test_evaluate_if_true(std_env: Environment) -> None: source = '(if 1 10 no-such-thing)' expected = 10 got = evaluate(parse(source), std_env) assert got == expected
def test_evaluate_quote(std_env: Environment) -> None: source = '(quote (1.1 is not 1))' expected = [1.1, 'is', 'not', 1] got = evaluate(parse(source), std_env) assert got == expected