Пример #1
0
def test_exception_raised_when_invoking_non_existent_function():
    data = [
        (['reset']),
    ]
    for contents in data:
        invocation = Types.Invocation(contents, Environment.create_empty())
        yield assert_raises, Errors.UndefinedSymbol, invocation.evaluate
Пример #2
0
def test_redefining_variables_raises_an_exception():
    data = ['foo', 'bar', 'baz']
    for name in data:
        value = '"does not matter"'
        definition = Types.Definition(['define', name, value],
                                      Environment.create_empty())
        definition.evaluate()
        yield assert_raises, Errors.RedefinedSymbol, definition.evaluate
Пример #3
0
def test_defining_symbols():
    environment = Environment.create_empty()
    data = [
        ('x', 0),
        ('y', 1),
        ('z', 2),
    ]
    for name, expected_value in data:
        environment.define(name, expected_value)
        value = environment.resolve(name)
        yield assert_equal, expected_value, value
Пример #4
0
def test_that_environment_can_be_provided():
    data = [
        ('(define x 10)', 'x', 10),
        ('(define y 20)', 'y', 20),
        ('(define z 30)', 'z', 30),
    ]
    environment = Environment.create_empty()
    run = lambda source_code: _EVALUATE(source_code, environment)
    for definition_code, access_code, expected_result in data:
        run(definition_code)
        result = run(access_code)
        yield assert_equals, expected_result, result
Пример #5
0
def test_defining_variables():
    data = [
        (['define', 'age', '20'], 'age', 20),
        (['define', 'abc', '52'], 'abc', 52),
    ]
    for definition_contents, symbol_contents, expected_value in data:
        environment = Environment.create_empty()
        definition = Types.Definition(definition_contents, environment)
        symbol = Types.Symbol(symbol_contents, environment)
        definition.evaluate()
        value = symbol.evaluate()
        yield assert_equals, expected_value, value
Пример #6
0
def test_creating_child_environments():
    name = 'x'
    parent_value = "parent"
    child_value = "child"

    parent = Environment.create_empty()
    parent.define(name, parent_value)

    child = parent.new_child()
    yield assert_equal, parent_value, child.resolve(name)
    child.define(name, child_value)
    yield assert_equal, child_value, child.resolve(name)
    yield assert_equal, parent_value, parent.resolve(name)
Пример #7
0
def test_name_shadowing_is_handled_for_function_parameters():
    variable_name = 'n'
    original_value = 0
    function_name = "number"
    function_definition = f"(define {function_name} (function ({variable_name}) {variable_name}))"

    environment = Environment.create_empty()
    environment.define(variable_name, original_value)

    run = lambda code: limp.evaluate(code, environment)
    variable_is_unchanged = lambda: (assert_equal, original_value,
                                     run(variable_name))
    run(function_definition)

    run(f"({function_name} 1)")

    yield variable_is_unchanged()
Пример #8
0
import limp
import limp.environment as Environment
from nose.tools import assert_equal
from tests.syntax import *

ENVIRONMENT = Environment.create_empty()
EVALUATE = lambda code: limp.evaluate(code, ENVIRONMENT)

ARBITRARY_NAME = "arbitrary"
ARBITRARY_VALUES = [0, 1, "value"]


def test_function_parameters_can_shadow_globally_defined_variables():
    REUSABLE_NAME = ARBITRARY_NAME
    OUTER_VALUE, INNER_VALUE = ARBITRARY_VALUES[:2]

    ENVIRONMENT.define(REUSABLE_NAME, OUTER_VALUE)

    SHADOWING_FUNCTION = function([REUSABLE_NAME], REUSABLE_NAME)
    EVALUATE(invoke(SHADOWING_FUNCTION, string(INNER_VALUE)))

    yield (assert_equal, OUTER_VALUE, EVALUATE(REUSABLE_NAME))
Пример #9
0
def test_exception_raised_when_accessing_non_existent_variable():
    data = ['year', 'boy', 'bones', 'dyliams', 'antichav', 'sesh']
    for contents in data:
        symbol = Types.Symbol(contents, Environment.create_empty())
        yield assert_raises, Errors.UndefinedSymbol, symbol.evaluate