Example #1
0
    def test_json_serialization(self):
        black = Bot(0, (1, 1), 0, (0, 3))
        white = Bot(1, (6, 6), 1, (3, 6), current_pos = (1, 1))

        black_json = json_converter.dumps(black)
        white_json = json_converter.dumps(white)

        black_json_target = {'__id__': 'pelita.datamodel.Bot',
                             '__value__': {'current_pos': [1, 1],
                                           'homezone': [0, 3],
                                           'index': 0,
                                           'initial_pos': [1, 1],
                                           'team_index': 0,
                                           'noisy': False}}

        white_json_target = {'__id__': 'pelita.datamodel.Bot',
                             '__value__': {'current_pos': [1, 1],
                                           'homezone': [3, 6],
                                           'index': 1,
                                           'initial_pos': [6, 6],
                                           'team_index': 1,
                                           'noisy': False}}

        self.assertEqual(json.loads(black_json), black_json_target)
        self.assertEqual(json.loads(white_json), white_json_target)

        self.assertEqual(json_converter.loads(black_json), black)
        self.assertEqual(json_converter.loads(white_json), white)
Example #2
0
    def test_json_serialization(self):
        team_black = Team(0, 'black', (0, 2))
        team_white = Team(1, 'white', (3, 6), score=5, bots=[1, 3, 5])

        team_black_json = json_converter.dumps(team_black)
        team_white_json = json_converter.dumps(team_white)

        team_black_json_target = {"__id__": "pelita.datamodel.Team",
                                  "__value__": {"index": 0,
                                                "bots": [],
                                                "score": 0,
                                                "name": "black",
                                                "zone": [0, 2]}}

        team_white_json_target = {"__id__": "pelita.datamodel.Team",
                                  "__value__": {"index": 1,
                                                "bots": [1, 3, 5],
                                                "score": 5,
                                                "name": "white",
                                                "zone": [3, 6]}}

        self.assertEqual(json.loads(team_black_json), team_black_json_target)
        self.assertEqual(json.loads(team_white_json), team_white_json_target)

        self.assertEqual(json_converter.loads(team_black_json), team_black)
        self.assertEqual(json_converter.loads(team_white_json), team_white)
Example #3
0
    def test_json_serialization(self):
        team_black = Team(0, 'black', (0, 2))
        team_white = Team(1, 'white', (3, 6), score=5, bots=[1, 3, 5])

        team_black_json = json_converter.dumps(team_black)
        team_white_json = json_converter.dumps(team_white)

        team_black_json_target = {
            "__id__": "pelita.datamodel.Team",
            "__value__": {
                "index": 0,
                "bots": [],
                "score": 0,
                "name": "black",
                "zone": [0, 2]
            }
        }

        team_white_json_target = {
            "__id__": "pelita.datamodel.Team",
            "__value__": {
                "index": 1,
                "bots": [1, 3, 5],
                "score": 5,
                "name": "white",
                "zone": [3, 6]
            }
        }

        self.assertEqual(json.loads(team_black_json), team_black_json_target)
        self.assertEqual(json.loads(team_white_json), team_white_json_target)

        self.assertEqual(json_converter.loads(team_black_json), team_black)
        self.assertEqual(json_converter.loads(team_white_json), team_white)
Example #4
0
    def test_json(self):
        tal = TypeAwareList([Wall(), Free(), Food()], base_class=MazeComponent)
        tal_json = json_converter.dumps(tal)
        self.assertEqual(json_converter.loads(tal_json), tal)

        class A(object):
            pass

        tal = TypeAwareList([], base_class=A)
        tal_json = json_converter.dumps(tal)
        self.assertRaises(AttributeError, json_converter.loads, tal_json)

        # test base_class=None
        tal = TypeAwareList([], base_class=None)
        tal_json = json_converter.dumps(tal)
        self.assertEqual(json_converter.loads(tal_json), tal)
 def send(self, obj):
     """ Converts `obj` to a json string and sends it.
     """
     if self.socket:
         json_string = json_converter.dumps(obj)
         self._send(json_string)
     else:
         raise RuntimeError("Cannot send without a connection.")
Example #6
0
 def test_json(self):
     test_layout3 = (
     """ ##################
         #0#.  .  # .     #
         #1#####    #####2#
         #     . #  .  .#3#
         ################## """)
     universe = create_CTFUniverse(test_layout3, 4)
     universe_json = json_converter.dumps(universe)
     self.assertEqual(json_converter.loads(universe_json), universe)
Example #7
0
    def test_json(self):
        m = Mesh(2, 3, data=[1, 2, 3, 4, 5, 6])
        m_json = json_converter.dumps(m)
        m_json_target = {"__id__": "pelita.containers.Mesh",
                         "__value__": {"width": 2,
                                       "data": [1, 2, 3, 4, 5, 6],
                                       "height": 3}}

        self.assertEqual(json.loads(m_json), m_json_target)
        self.assertEqual(json_converter.loads(m_json), m)
Example #8
0
    def test_json(self):
        m = Mesh(2, 3, data=[1, 2, 3, 4, 5, 6])
        m_json = json_converter.dumps(m)
        m_json_target = {"__id__": "pelita.containers.Mesh",
                         "__value__": {"width": 2,
                                       "data": [1, 2, 3, 4, 5, 6],
                                       "height": 3}}

        self.assertEqual(json.loads(m_json), m_json_target)
        self.assertEqual(json_converter.loads(m_json), m)
Example #9
0
    def test_json_serialization(self):
        black = Bot(0, (1, 1), 0, (0, 3))
        white = Bot(1, (6, 6), 1, (3, 6), current_pos=(1, 1))

        black_json = json_converter.dumps(black)
        white_json = json_converter.dumps(white)

        black_json_target = {
            '__id__': 'pelita.datamodel.Bot',
            '__value__': {
                'current_pos': [1, 1],
                'homezone': [0, 3],
                'index': 0,
                'initial_pos': [1, 1],
                'team_index': 0,
                'noisy': False
            }
        }

        white_json_target = {
            '__id__': 'pelita.datamodel.Bot',
            '__value__': {
                'current_pos': [1, 1],
                'homezone': [3, 6],
                'index': 1,
                'initial_pos': [6, 6],
                'team_index': 1,
                'noisy': False
            }
        }

        self.assertEqual(json.loads(black_json), black_json_target)
        self.assertEqual(json.loads(white_json), white_json_target)

        self.assertEqual(json_converter.loads(black_json), black)
        self.assertEqual(json_converter.loads(white_json), white)
Example #10
0
    def test_json(self):
        reconvert = lambda event: json_converter.loads(json_converter.dumps(event))

        bot_moves = BotMoves(0, (0, 0), (1, 0))
        self.assertEqual(bot_moves, reconvert(bot_moves))
        bot_eats = BotEats(1, (0, 0))
        self.assertEqual(bot_eats, reconvert(bot_eats))
        food_eaten = FoodEaten((0, 0))
        self.assertEqual(food_eaten, reconvert(food_eaten))
        bot_destroyed = BotDestroyed(0, (0, 0), (0, 1), (0, 0), 1, (0, 1), (0, 1))
        self.assertEqual(bot_destroyed, reconvert(bot_destroyed))
        team_score_change = TeamScoreChange(0, 1, 2)
        self.assertEqual(team_score_change, reconvert(team_score_change))
        team_wins = TeamWins(0)
        self.assertEqual(team_wins, reconvert(team_wins))
        timeout_event = TimeoutEvent(0)
        self.assertEqual(timeout_event, reconvert(timeout_event))
        game_draw = GameDraw()
        self.assertEqual(game_draw, reconvert(game_draw))
Example #11
0
    def test_json_non_trivial(self):
        simple_mesh = Mesh(2, 3, data=[1, 2, 3, 4, 5, 6])
        m = Mesh(2, 3, data=[1,
                            {"key": "value"},
                             [3, 6, 9, 27],
                             ("a", "tuple?"),
                              u"ünico∂e",
                              simple_mesh])

        m_no_tuple = Mesh(2, 3, data=[1,
                            {"key": "value"},
                             [3, 6, 9, 27],
                             ["a", "tuple?"],
                              u"ünico∂e",
                              simple_mesh])

        m_json = json_converter.dumps(m)

        self.assertEqual(json_converter.loads(m_json), m_no_tuple)
Example #12
0
    def test_json_non_trivial(self):
        simple_mesh = Mesh(2, 3, data=[1, 2, 3, 4, 5, 6])
        m = Mesh(2, 3, data=[1,
                            {"key": "value"},
                             [3, 6, 9, 27],
                             ("a", "tuple?"),
                              u"ünico∂e",
                              simple_mesh])

        m_no_tuple = Mesh(2, 3, data=[1,
                            {"key": "value"},
                             [3, 6, 9, 27],
                             ["a", "tuple?"],
                              u"ünico∂e",
                              simple_mesh])

        m_json = json_converter.dumps(m)

        self.assertEqual(json_converter.loads(m_json), m_no_tuple)
Example #13
0
    def test_json(self):
        test_layout3 = (
        """ ##################
            #0#.  .  # .     #
            #1#####    #####2#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout3, 4)
        universe_json = json_converter.dumps(universe)
        self.assertEqual(json_converter.loads(universe_json), universe)

        universe_dict = json.loads(universe_json)
        self.assertEqual(universe_dict["__id__"], "pelita.datamodel.CTFUniverse")
        universe_dict_value = universe_dict["__value__"]
        self.assertTrue("maze" in universe_dict_value)
        self.assertEqual(len(universe_dict_value["bots"]), 4)
        self.assertEqual(len(universe_dict_value["teams"]), 2)
        # check that bots and teams are directly embedded
        self.assertEqual([bot["index"] for bot in universe_dict_value["bots"]], list(range(4)))
        self.assertEqual([team["index"] for team in universe_dict_value["teams"]], list(range(2)))
Example #14
0
    def test_json(self):
        test_layout3 = (""" ##################
            #0#.  .  # .     #
            #1#####    #####2#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout3, 4)
        universe_json = json_converter.dumps(universe)
        self.assertEqual(json_converter.loads(universe_json), universe)

        universe_dict = json.loads(universe_json)
        self.assertEqual(universe_dict["__id__"],
                         "pelita.datamodel.CTFUniverse")
        universe_dict_value = universe_dict["__value__"]
        self.assertTrue("maze" in universe_dict_value)
        self.assertEqual(len(universe_dict_value["bots"]), 4)
        self.assertEqual(len(universe_dict_value["teams"]), 2)
        # check that bots and teams are directly embedded
        self.assertEqual([bot["index"] for bot in universe_dict_value["bots"]],
                         range(4))
        self.assertEqual(
            [team["index"] for team in universe_dict_value["teams"]], range(2))
Example #15
0
 def test_json(self):
     maze = Maze(2, 1, data=["#", " ."])
     maze_json = json_converter.dumps(maze)
     self.assertEqual(json_converter.loads(maze_json), maze)
Example #16
0
 def test_json(self):
     maze = Maze(2, 1, data=["#", " ."])
     maze_json = json_converter.dumps(maze)
     self.assertEqual(json_converter.loads(maze_json), maze)