Exemplo n.º 1
0
 def testUnregister(self):
     """Test unregistering a listener"""
     event = EventManager()
     event_tester = EventTester()
     event.register_listener(event_tester)
     event.unregister_listener(event_tester)
     self.assertFalse(event_tester in event.listener_groups["default"].listeners)
Exemplo n.º 2
0
 def testPost(self):
     """Test posting an event"""
     event_manager = EventManager()
     event_tester = EventTester()
     event_manager.register_listener(event_tester)
     event = Event()
     event_manager.post(event)
     self.assertEqual(event_tester.last_event(), event)
Exemplo n.º 3
0
class BulletsTests(unittest.TestCase):
    def setUp(self):
        self.event_manager = EventManager()
        self.event_tester = EventTester()
        self.event_manager.register_listener(self.event_tester, ("default"))

    def testInit(self):
        """Test bullets initialisation"""
        bullets = Bullets(self.event_manager)
        self.assertEqual(self.event_manager, bullets.event_manager)
        self.assertEqual([], bullets.bullets)
        self.assertTrue(bullets in self.event_manager.listener_groups["default"].listeners)

    def testCreateBullet(self):
        """Test creating bullet"""
        bullets = Bullets(self.event_manager)
        sector = Sector()
        direction = 0
        bullets.create_bullet(sector, direction)
        self.assertTrue(len(bullets.bullets) == 1)
        self.assertEquals(bullets.bullets[0].sector, sector)
        self.assertEqual(bullets.bullets[0].direction, direction)

    def testDestroyBullet(self):
        """Test destroying bullet"""
        bullets = Bullets(self.event_manager)
        sector = Sector()
        direction = 0
        bullets.create_bullet(sector, direction)
        bullets.destroy_bullet(bullets.bullets[0])
        self.assertTrue(len(bullets.bullets) == 0)
        self.assertTrue(isinstance(self.event_tester.last_event(), BulletDestroyedEvent))

    def testMoveBullets(self):
        """Test moving bullets"""
        bullets = Bullets(self.event_manager)
        bullets.move_bullets()
        self.assertTrue(isinstance(self.event_tester.last_event(), BulletsMoveEvent))

    def testNotifyTickEvent(self):
        """Test notifying bullets about tick event"""
        bullets = Bullets(self.event_manager)

        def register_move_bullets_call():
            register_move_bullets_call.called = True

        bullets.move_bullets = register_move_bullets_call
        bullets.move_bullets.called = False
        event = TickEvent()
        bullets.notify(event)
        # there are no bullets -> False
        self.assertFalse(bullets.move_bullets.called)
        bullets.create_bullet(Sector(), 0)
        bullets.notify(event)
        self.assertTrue(bullets.move_bullets.called)
Exemplo n.º 4
0
 def setUp(self):
     self.event_manager = EventManager()
     self.id_manager = ObjectIdGenerator()
     self.event_tester = EventTester()
     self.event_manager.register_listener(self.event_tester, ("default"))
     class C():
         pass
     self.game = C()
     self.game.map = C()
Exemplo n.º 5
0
class MapStateTests(unittest.TestCase):
    
    def setUp(self):
        self.event_manager = EventManager()
    
    def testInit(self):
        """Test map state initalisation"""
        map_state = MapState(self.event_manager)
        self.assertEqual(map_state.event_manager, self.event_manager)
        self.assertTrue(map_state in self.event_manager.listener_groups["default"].listeners)
        self.assertEqual(map_state.occupied_sectors_by_actor_id, {})
        self.assertEqual(map_state.actors_by_sector_id, {})
        
    def testCharactorPlaceNotification(self):
        """Test charactor place notification"""
        map_state = MapState(self.event_manager)
        charactor = Charactor(self.event_manager)
        charactor.sector = Sector()
        charactor_place_event = CharactorPlaceEvent(charactor)
        self.event_manager.post(charactor_place_event)
        self.assertEqual(map_state.actors_by_sector_id[charactor.sector.sector_id], charactor)
        self.assertEqual(map_state.occupied_sectors_by_actor_id[charactor.charactor_id], charactor.sector)
        
    def testFreeSectorActionNotification(self):
        """Test free section action notification"""
        map_state = MapState(self.event_manager)
        sector = Sector()
        
        self.actionExecuted = False #TODO: this is no good
        def function(sector_is_free):
            if sector_is_free:
                self.actionExecuted = True
        callback_function = function
        free_sector_action = FreeSectorAction(sector, callback_function)
        self.event_manager.post(free_sector_action)
        self.assertTrue(self.actionExecuted)
        
        self.actionExecuted = False
        charactor = Charactor(self.event_manager)
        charactor.sector = sector
        map_state.occupied_sectors_by_actor_id[charactor.charactor_id] = charactor.sector
        self.event_manager.post(free_sector_action)
        self.assertFalse(self.actionExecuted)
Exemplo n.º 6
0
 def setUp(self):
     self.event_manager = EventManager()
     self.event_tester = EventTester()
     self.event_manager.register_listener(self.event_tester, ("default"))
Exemplo n.º 7
0
 def setUp(self):
     self.event_manager = EventManager()
Exemplo n.º 8
0
class PlayerTests(unittest.TestCase):
    
    def setUp(self):
        self.event_manager = EventManager()
        self.id_manager = ObjectIdGenerator()
        self.event_tester = EventTester()
        self.event_manager.register_listener(self.event_tester, ("default"))
        class C():
            pass
        self.game = C()
        self.game.map = C()

    def testInit(self):
        """Test player initialisation"""
        player = Player(self.event_manager, self.id_manager)
        self.assertEqual(player.event_manager, self.event_manager)
        self.assertEqual(player.game, None)
        self.assertEqual(player.name, "")
        self.assertTrue(player in self.event_manager.listener_groups["default"].listeners)
        for charactor in player.charactors:
            self.assertTrue(isinstance(charactor, Charactor))
            #TODO check id
        self.assertEqual(player.active_charactor, player.charactors[3])
        
    def testNotifyGameStartedEvent(self):
        """Test notifying the player abot a GameStartedEvent"""
        player = Player(self.event_manager, self.id_manager)
        game_started_event = GameStartedEvent()
        self.event_tester.clear()
        player.notify(game_started_event)
        for index, charactor in enumerate(player.charactors):
            self.assertTrue(isinstance(self.event_tester.events[index], CharactorPlaceRequest))
            self.assertEqual(charactor, self.event_tester.events[index].charactor)
            
    def testNotifyCharactorMoveToRequest(self):
        """Test notifying the player abot a CharactorMoveToRequest"""
        player = Player(self.event_manager, self.id_manager)
        request = CharactorMoveToRequest(None)
        self.event_tester.clear()
        player.notify(request)
        self.assertTrue(isinstance(self.event_tester.last_event(), CalculatePathRequest))
        self.assertEqual(None, self.event_tester.last_event().pos)
        
    def testCharactorShootRequest(self):
        """Test notifying the player about a CharactorChoorReuquest"""
        player = Player(self.event_manager, self.id_manager)
        request = CharactorShootRequest()
        self.event_tester.clear()
        def register_shoot_call():
            register_shoot_call.called = True
        self.shoot_called = False
        player.active_charactor.shoot = register_shoot_call
        player.notify(request)
        self.assertTrue(player.active_charactor.shoot.called)
    
    def testNotifyActiveCharactorChangeRequest(self):
        """Test notifying the player abot a ActiveCharactorChangeRequest"""
        player = Player(self.event_manager, self.id_manager)
        request = ActiveCharactorChangeRequest(None)
        self.event_tester.clear()
        player.notify(request)
        self.assertTrue(isinstance(self.event_tester.last_event(), OccupiedSectorAction))
        self.assertEqual(None, self.event_tester.last_event().pos)
        self.assertTrue(type(self.event_tester.last_event().function) is FunctionType)
        #TODO check for function
        
    def testNotifyCharatorMoveRequest(self):
        """Test notifying the player about a CharactorMoveRequest"""
        player = Player(self.event_manager, self.id_manager)
        request = CharactorMoveRequest(DIRECTION_DOWN)
        player.active_charactor.direction = DIRECTION_UP
        def register_turn_call(direction):
            register_turn_call.called = True
        player.active_charactor.turn = register_turn_call
        self.event_tester.clear()
        
        #charactor is facing up, request is to move down -> active charactor should turn 
        player.notify(request)
        self.assertTrue(player.active_charactor.turn.called)
        self.assertIsNone(self.event_tester.last_event())
        
        request = CharactorMoveRequest(DIRECTION_UP)
        player.active_charactor.turn.called = False
        player.notify(request)
        # the active charactor does not have a sector yet
        self.assertIsNone(self.event_tester.last_event())
        self.assertFalse(player.active_charactor.turn.called)
        
        request = CharactorMoveRequest(DIRECTION_UP, True)
        player.active_charactor.turn.called = False
        player.notify(request)
        # the active charactor does not have a sector yet
        self.assertIsNone(self.event_tester.last_event())
        self.assertFalse(player.active_charactor.turn.called)
        
        player.active_charactor.sector = Sector()
        player.notify(request)
        self.assertTrue(isinstance(self.event_tester.last_event(), FreeSectorAction))
        self.assertIsNone(self.event_tester.last_event().sector)