def test_cond(): """Ensure we evaluate cond properly.""" env = prelude.env() call = wtypes.List([ wtypes.List([ wtypes.String('two'), wtypes.List( [wtypes.Integer(2), wtypes.Integer(2), wtypes.Symbol('eq?')]) ]), wtypes.List([ wtypes.String('one'), wtypes.List( [wtypes.Integer(1), wtypes.Integer(1), wtypes.Symbol('eq?')]) ]), wtypes.List([ wtypes.String('not-one'), wtypes.List( [wtypes.Integer(2), wtypes.Integer(1), wtypes.Symbol('eq?')]) ]), wtypes.Symbol('cond') ]) result = call.eval(env) assert result == wtypes.String('one')
def test_get_nearest(): """Ensure we get bindings past the top frame.""" env = wisp.env.Environment() env.add_binding(wtypes.Symbol('a'), wtypes.String('apple')) env.add_frame() env.add_frame() assert env[wtypes.Symbol('a')] == wtypes.String('apple')
def test_define(): """Ensure define binds values to symbols.""" env = prelude.env() res = env[wtypes.Symbol('define')].call( [wtypes.Symbol('a'), quoted_list([wtypes.String('apple')])], env) assert res == wtypes.Symbol('a') assert env[wtypes.Symbol('a')] == wtypes.List([wtypes.String('apple')])
def test_set_bindings(): """Ensure we set bindings in the current frame.""" env = wisp.env.Environment() env.add_binding(wtypes.Symbol('a'), wtypes.String('apple')) env.add_frame() env.add_binding(wtypes.Symbol('a'), wtypes.String('aardvark')) env[wtypes.Symbol('a')] = wtypes.String('adorno') assert env[wtypes.Symbol('a')] == wtypes.String('adorno') env.pop_frame() assert env[wtypes.Symbol('a')] == wtypes.String('apple')
def test_equal(): """Ensure equal tests equality properly.""" assert prelude.env()[wtypes.Symbol('eq?')].call( [wtypes.Integer(4), wtypes.Integer(4)], {}) == wtypes.Bool(True) assert prelude.env()[wtypes.Symbol('eq?')].call( [wtypes.Integer(4), wtypes.Integer(2)], {}) == wtypes.Bool(False) assert prelude.env()[wtypes.Symbol('eq?')].call( [wtypes.Integer(4), wtypes.String('abc')], {}) == wtypes.Bool(False)
def test_parse_list(): """Ensure we can parse lists.""" assert parser.parse_expr.parse('(1 abc "abc" #t #f +)') == wtypes.List([ wtypes.Integer(1), wtypes.Symbol('abc'), wtypes.String('abc'), wtypes.Bool(True), wtypes.Bool(False), wtypes.Symbol('+'), ])
def test_cond_with_else(): """Ensure we handle else expressions.""" env = prelude.env() call = wtypes.List([ wtypes.List([wtypes.String('else-case'), wtypes.Symbol('else')]), wtypes.List([ wtypes.String('not-two'), wtypes.List( [wtypes.Integer(3), wtypes.Integer(2), wtypes.Symbol('eq?')]) ]), wtypes.List([ wtypes.String('not-one'), wtypes.List( [wtypes.Integer(2), wtypes.Integer(1), wtypes.Symbol('eq?')]) ]), wtypes.Symbol('cond') ]) result = call.eval(env) assert result == wtypes.String('else-case')
def test_pop_and_add(): """Ensure we can add and pop frames to the environment.""" env = wisp.env.Environment() env.add_frame({'a': wtypes.String('apple')}) env.add_frame({'b': wtypes.String('banana')}) env.add_frame({'c': wtypes.String('carrot')}) assert env.pop_frame() == {'c': wtypes.String('carrot')} assert env.pop_frame() == {'b': wtypes.String('banana')} assert env.pop_frame() == {'a': wtypes.String('apple')}
def test_local_global_scope(): """Ensure we return the local and global scope properly.""" env = wisp.env.Environment() env.add_binding(wtypes.Symbol('a'), wtypes.String('apple')) env.add_frame() env.add_binding(wtypes.Symbol('a'), wtypes.String('aardvark')) env.add_frame() env.add_binding(wtypes.Symbol('a'), wtypes.String('adorno')) assert env.global_scope() == {'a': wtypes.String('apple')} assert env.local_scope() == {'a': wtypes.String('adorno')} env.pop_frame() assert env.global_scope() == {'a': wtypes.String('apple')} assert env.local_scope() == {'a': wtypes.String('aardvark')} env.pop_frame() assert env.global_scope() == {'a': wtypes.String('apple')} assert env.local_scope() == {}
def test_get_add_binding(): """Ensure we get the binding from the top frame.""" env = wisp.env.Environment() env.add_binding(wtypes.Symbol('a'), wtypes.String('apple')) env.add_frame() env.add_binding(wtypes.Symbol('a'), wtypes.String('aardvark')) env.add_frame() env.add_binding(wtypes.Symbol('a'), wtypes.String('adorno')) assert env[wtypes.Symbol('a')] == wtypes.String('adorno') env.pop_frame() assert env[wtypes.Symbol('a')] == wtypes.String('aardvark') env.pop_frame() assert env[wtypes.Symbol('a')] == wtypes.String('apple')
def test_eval_string(): """Ensure strings evaluate to themselves.""" assert wtypes.String('abc').eval({}) == wtypes.String('abc')
def test_eval_missing_symbol(): """Ensure an error is raised on missing symbols.""" env = wisp.env.Environment({'a': wtypes.String('def')}) with pytest.raises(exceptions.WispException): wtypes.Symbol('b').eval(env)
def test_eval_symbol(): """Ensure symbols evaluate to their bound value.""" env = wisp.env.Environment({'a': wtypes.String('def')}) assert wtypes.Symbol('a').eval(env) == wtypes.String('def')
def test_parse_string(): """Ensure we can parse strings.""" assert parser.parse_expr.parse('"abc"') == wtypes.String('abc')
def test_set_missing(): """Ensure we raise an exception for missing bindings.""" env = wisp.env.Environment() with pytest.raises(exceptions.WispException): env[wtypes.Symbol('a')] = wtypes.String('apple')
def parse_string(): """Parse a string as any characters enclosed between two "s.""" yield parsec.string('"') chars = yield parsec.many(parsec.none_of('"')) yield parsec.string('"') return wtypes.String(''.join(chars))