<s> """<STR_LIT>""" <EOL> from hamcrest import assert_that , equal_to , is_ <EOL> from mockito import mock <EOL> from pyherc . data import ( Model , Portal , add_portal , add_character , get_characters , <EOL> get_character ) <EOL> from pyherc . data . constants import Direction <EOL> from pyherc . data . model import ESCAPED_DUNGEON <EOL> from pyherc . ports import is_move_legal , move , set_action_factory <EOL> from pyherc . rules . moving . action import EscapeAction <EOL> from pyherc . test . builders import ( ActionFactoryBuilder , CharacterBuilder , <EOL> LevelBuilder ) <EOL> from pyherc . test . helpers import EventListener <EOL> from pyherc . test . matchers import has_marked_for_redrawing , is_illegal , is_legal , EventType <EOL> from mockito import verify , mock <EOL> class TestEventDispatching ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( ) <EOL> self . model = None <EOL> self . character = None <EOL> self . level = None <EOL> self . listener = None <EOL> self . actions = None <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . model = Model ( ) <EOL> self . character = ( CharacterBuilder ( ) <EOL> . with_model ( self . model ) <EOL> . with_location ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> . build ( ) ) <EOL> self . model . dungeon = ( LevelBuilder ( ) <EOL> . with_character ( self . character ) <EOL> . build ( ) ) <EOL> self . listener = mock ( ) <EOL> self . model . register_event_listener ( self . listener ) <EOL> set_action_factory ( ActionFactoryBuilder ( ) <EOL> . with_move_factory ( ) <EOL> . build ( ) ) <EOL> def test_event_is_relayed ( self ) : <EOL> """<STR_LIT>""" <EOL> move ( character = self . character , <EOL> direction = Direction . east ) <EOL> verify ( self . listener ) . receive_event ( EventType ( '<STR_LIT>' ) ) <EOL> class TestMoving ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( ) <EOL> self . actions = None <EOL> self . character = None <EOL> self . level1 = None <EOL> self . level2 = None <EOL> self . portal1 = None <EOL> self . portal2 = None <EOL> self . model = None <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . character = ( CharacterBuilder ( ) <EOL> . build ( ) ) <EOL> self . level1 = ( LevelBuilder ( ) <EOL> . with_floor_tile ( "<STR_LIT>" ) <EOL> . with_wall_at ( ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> . build ( ) ) <EOL> self . level2 = ( LevelBuilder ( ) <EOL> . with_floor_tile ( "<STR_LIT>" ) <EOL> . build ( ) ) <EOL> self . portal1 = Portal ( ( None , None ) , None ) <EOL> self . portal1 . icon = <NUM_LIT:1> <EOL> self . portal2 = Portal ( ( "<STR_LIT>" , "<STR_LIT>" ) , None ) <EOL> self . portal2 = Portal ( ( "<STR_LIT>" , "<STR_LIT>" ) , None ) <EOL> add_portal ( self . level1 , ( <NUM_LIT:5> , <NUM_LIT:5> ) , self . portal1 ) <EOL> add_portal ( self . level2 , ( <NUM_LIT:10> , <NUM_LIT:10> ) , self . portal2 , self . portal1 ) <EOL> add_character ( self . level1 , ( <NUM_LIT:5> , <NUM_LIT:5> ) , self . character ) <EOL> set_action_factory ( ActionFactoryBuilder ( ) <EOL> . with_move_factory ( ) <EOL> . build ( ) ) <EOL> def test_moving_when_stairs_are_blocked ( self ) : <EOL> """<STR_LIT>""" <EOL> blocker = CharacterBuilder ( ) . build ( ) <EOL> add_character ( self . level2 , ( <NUM_LIT:10> , <NUM_LIT:10> ) , blocker ) <EOL> move ( character = self . character , <EOL> direction = Direction . enter ) <EOL> assert_that ( blocker . level , is_ ( equal_to ( self . level2 ) ) ) <EOL> assert_that ( self . character . level , is_ ( equal_to ( self . level2 ) ) ) <EOL> def check_move_result ( self , start , direction , expected_location ) : <EOL> """<STR_LIT>""" <EOL> self . character . location = start <EOL> move ( character = self . character , <EOL> direction = direction ) <EOL> assert_that ( self . character . location , <EOL> is_ ( equal_to ( expected_location ) ) ) <EOL> def test_simple_move ( self ) : <EOL> """<STR_LIT>""" <EOL> test_data = [ ( <NUM_LIT:1> , ( <NUM_LIT:5> , <NUM_LIT:4> ) ) , <EOL> ( <NUM_LIT:2> , ( <NUM_LIT:6> , <NUM_LIT:4> ) ) , <EOL> ( <NUM_LIT:3> , ( <NUM_LIT:6> , <NUM_LIT:5> ) ) , <EOL> ( <NUM_LIT:4> , ( <NUM_LIT:6> , <NUM_LIT:6> ) ) , <EOL> ( <NUM_LIT:5> , ( <NUM_LIT:5> , <NUM_LIT:6> ) ) , <EOL> ( <NUM_LIT:6> , ( <NUM_LIT:4> , <NUM_LIT:6> ) ) , <EOL> ( <NUM_LIT:7> , ( <NUM_LIT:4> , <NUM_LIT:5> ) ) , <EOL> ( <NUM_LIT:8> , ( <NUM_LIT:4> , <NUM_LIT:4> ) ) ] <EOL> for row in test_data : <EOL> yield ( self . check_move_result , ( <NUM_LIT:5> , <NUM_LIT:5> ) , row [ <NUM_LIT:0> ] , row [ <NUM_LIT:1> ] ) <EOL> def test_walking_to_walls ( self ) : <EOL> """<STR_LIT>""" <EOL> self . character . location = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> move ( character = self . character , <EOL> direction = Direction . north ) <EOL> assert_that ( self . character . location , is_ ( equal_to ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) ) ) <EOL> def test_entering_portal ( self ) : <EOL> """<STR_LIT>""" <EOL> move ( character = self . character , <EOL> direction = Direction . enter ) <EOL> assert_that ( self . character . level , is_ ( equal_to ( self . level2 ) ) ) <EOL> assert_that ( self . character . location , is_ ( equal_to ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) ) ) <EOL> def test_entering_portal_adds_character_to_creatures ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . character . level == self . level1 <EOL> assert self . character in get_characters ( self . level1 ) <EOL> move ( character = self . character , <EOL> direction = Direction . enter ) <EOL> assert self . character . level == self . level2 <EOL> assert self . character in get_characters ( self . level2 ) <EOL> def test_entering_portal_removes_character_from_old_level ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . character . level == self . level1 <EOL> assert self . character in get_characters ( self . level1 ) <EOL> move ( character = self . character , <EOL> direction = Direction . enter ) <EOL> assert self . character not in get_characters ( self . level1 ) <EOL> def test_entering_non_existent_portal ( self ) : <EOL> """<STR_LIT>""" <EOL> self . character . location = ( <NUM_LIT:6> , <NUM_LIT:3> ) <EOL> move ( character = self . character , <EOL> direction = Direction . enter ) <EOL> assert_that ( self . character . location , is_ ( equal_to ( ( <NUM_LIT:6> , <NUM_LIT:3> ) ) ) ) <EOL> assert_that ( self . character . level , is_ ( equal_to ( self . level1 ) ) ) <EOL> def test_moving_uses_time ( self ) : <EOL> """<STR_LIT>""" <EOL> tick = self . character . tick <EOL> move ( character = self . character , <EOL> direction = Direction . east ) <EOL> assert self . character . tick > tick <EOL> def test_taking_escape_stairs_ends_game ( self ) : <EOL> """<STR_LIT>""" <EOL> portal3 = Portal ( ( None , None ) , None ) <EOL> portal3 . exits_dungeon = True <EOL> add_portal ( self . level1 , ( <NUM_LIT:2> , <NUM_LIT:2> ) , portal3 ) <EOL> self . character . location = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> move ( character = self . character , <EOL> direction = Direction . enter ) <EOL> model = self . character . model <EOL> assert_that ( model . end_condition , is_ ( equal_to ( ESCAPED_DUNGEON ) ) ) <EOL> class TestEscapeAction ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( ) <EOL> def test_player_character_can_escape ( self ) : <EOL> """<STR_LIT>""" <EOL> model = mock ( ) <EOL> character = ( CharacterBuilder ( ) <EOL> . as_player_character ( ) <EOL> . with_model ( model ) <EOL> . build ( ) ) <EOL> action = EscapeAction ( character = character ) <EOL> assert_that ( action , is_legal ( ) ) <EOL> def test_non_player_character_can_not_escape ( self ) : <EOL> """<STR_LIT>""" <EOL> model = mock ( ) <EOL> character = ( CharacterBuilder ( ) <EOL> . with_model ( model ) <EOL> . build ( ) ) <EOL> action = EscapeAction ( character = character ) <EOL> assert_that ( action , is_illegal ( ) ) <EOL> class TestSwitchingPlaces ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( ) <EOL> self . level = None <EOL> self . monster_1 = None <EOL> self . monster_2 = None <EOL> self . actions = None <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . level = LevelBuilder ( ) . build ( ) <EOL> self . monster_1 = ( CharacterBuilder ( ) <EOL> . with_name ( '<STR_LIT>' ) <EOL> . build ( ) ) <EOL> self . monster_2 = ( CharacterBuilder ( ) <EOL> . with_name ( '<STR_LIT>' ) <EOL> . build ( ) ) <EOL> self . monster_1 . artificial_intelligence = lambda : None <EOL> self . monster_2 . artificial_intelligence = lambda : None <EOL> add_character ( self . level , ( <NUM_LIT:5> , <NUM_LIT:5> ) , self . monster_1 ) <EOL> add_character ( self . level , ( <NUM_LIT:6> , <NUM_LIT:5> ) , self . monster_2 ) <EOL> set_action_factory ( ActionFactoryBuilder ( ) <EOL> . with_move_factory ( ) <EOL> . build ( ) ) <EOL> def test_switch_places ( self ) : <EOL> """<STR_LIT>""" <EOL> move ( self . monster_1 , Direction . east ) <EOL> assert_that ( self . monster_1 . location , is_ ( equal_to ( ( <NUM_LIT:6> , <NUM_LIT:5> ) ) ) ) <EOL> assert_that ( self . monster_2 . location , is_ ( equal_to ( ( <NUM_LIT:5> , <NUM_LIT:5> ) ) ) ) <EOL> def test_system_consistency ( self ) : <EOL> """<STR_LIT>""" <EOL> move ( self . monster_1 , Direction . east ) <EOL> assert_that ( get_character ( self . level , ( <NUM_LIT:6> , <NUM_LIT:5> ) ) , is_ ( equal_to ( self . monster_1 ) ) ) <EOL> assert_that ( get_character ( self . level , ( <NUM_LIT:5> , <NUM_LIT:5> ) ) , is_ ( equal_to ( self . monster_2 ) ) ) </s>
