<s> from nose . tools import assert_equals , assert_raises_regexp <EOL> from diylisp . types import LispError , Environment <EOL> from diylisp . evaluator import evaluate <EOL> from diylisp . parser import parse <EOL> """<STR_LIT>""" <EOL> def test_simple_lookup ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( { "<STR_LIT>" : <NUM_LIT> } ) <EOL> assert_equals ( <NUM_LIT> , env . lookup ( "<STR_LIT>" ) ) <EOL> def test_lookup_on_missing_raises_exception ( ) : <EOL> """<STR_LIT>""" <EOL> with assert_raises_regexp ( LispError , "<STR_LIT>" ) : <EOL> empty_env = Environment ( ) <EOL> empty_env . lookup ( "<STR_LIT>" ) <EOL> def test_lookup_from_inner_env ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( { "<STR_LIT:foo>" : <NUM_LIT> } ) <EOL> env = env . extend ( { "<STR_LIT:bar>" : True } ) <EOL> assert_equals ( <NUM_LIT> , env . lookup ( "<STR_LIT:foo>" ) ) <EOL> assert_equals ( True , env . lookup ( "<STR_LIT:bar>" ) ) <EOL> def test_lookup_deeply_nested_var ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( { "<STR_LIT:a>" : <NUM_LIT:1> } ) . extend ( { "<STR_LIT:b>" : <NUM_LIT:2> } ) . extend ( { "<STR_LIT:c>" : <NUM_LIT:3> } ) . extend ( { "<STR_LIT:foo>" : <NUM_LIT:100> } ) <EOL> assert_equals ( <NUM_LIT:100> , env . lookup ( "<STR_LIT:foo>" ) ) <EOL> def test_extend_returns_new_environment ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( { "<STR_LIT:foo>" : <NUM_LIT:1> } ) <EOL> extended = env . extend ( { "<STR_LIT:foo>" : <NUM_LIT:2> } ) <EOL> assert_equals ( <NUM_LIT:1> , env . lookup ( "<STR_LIT:foo>" ) ) <EOL> assert_equals ( <NUM_LIT:2> , extended . lookup ( "<STR_LIT:foo>" ) ) <EOL> def test_set_changes_environment_in_place ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( ) <EOL> env . set ( "<STR_LIT:foo>" , <NUM_LIT:2> ) <EOL> assert_equals ( <NUM_LIT:2> , env . lookup ( "<STR_LIT:foo>" ) ) <EOL> def test_redefine_variables_illegal ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( { "<STR_LIT:foo>" : <NUM_LIT:1> } ) <EOL> with assert_raises_regexp ( LispError , "<STR_LIT>" ) : <EOL> env . set ( "<STR_LIT:foo>" , <NUM_LIT:2> ) <EOL> """<STR_LIT>""" <EOL> def test_evaluating_symbol ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( { "<STR_LIT:foo>" : <NUM_LIT> } ) <EOL> assert_equals ( <NUM_LIT> , evaluate ( "<STR_LIT:foo>" , env ) ) <EOL> def test_lookup_missing_variable ( ) : <EOL> """<STR_LIT>""" <EOL> with assert_raises_regexp ( LispError , "<STR_LIT>" ) : <EOL> evaluate ( "<STR_LIT>" , Environment ( ) ) <EOL> def test_define ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( ) <EOL> evaluate ( parse ( "<STR_LIT>" ) , env ) <EOL> assert_equals ( <NUM_LIT:1000> , env . lookup ( "<STR_LIT:x>" ) ) <EOL> def test_define_with_wrong_number_of_arguments ( ) : <EOL> """<STR_LIT>""" <EOL> with assert_raises_regexp ( LispError , "<STR_LIT>" ) : <EOL> evaluate ( parse ( "<STR_LIT>" ) , Environment ( ) ) <EOL> with assert_raises_regexp ( LispError , "<STR_LIT>" ) : <EOL> evaluate ( parse ( "<STR_LIT>" ) , Environment ( ) ) <EOL> def test_define_with_nonsymbol_as_variable ( ) : <EOL> """<STR_LIT>""" <EOL> with assert_raises_regexp ( LispError , "<STR_LIT>" ) : <EOL> evaluate ( parse ( "<STR_LIT>" ) , Environment ( ) ) <EOL> def test_variable_lookup_after_define ( ) : <EOL> """<STR_LIT>""" <EOL> env = Environment ( ) <EOL> evaluate ( parse ( "<STR_LIT>" ) , env ) <EOL> assert_equals ( <NUM_LIT:4> , evaluate ( "<STR_LIT:foo>" , env ) ) </s>
