def test_from_json(self): players_json = [ { 'color': "red", 'score': 0, 'places': [] }, { 'color': "black", 'score': 123, 'places': [[2, 3]] }, { 'color': "white", 'score': 321, 'places': [[0, 0], [4, 5]] }, { 'color': "brown", 'score': 7, 'places': [[0, 1], [321, 123]] }, ] board_json = [ [1, 2], [4, 0, 0, 5], [1, 1, 0, 1] ] state_json = {'players': players_json, 'board': board_json} players = [PlayerData.from_json(player) for player in players_json] board = Board.from_json(board_json) self.assertEqual(State(players, board), State.from_json(state_json))
def test_from_json(self): jsons = [ { 'color': "red", 'score': 0, 'places': [] }, { 'color': "black", 'score': 123, 'places': [[2, 3]] }, { 'color': "white", 'score': 321, 'places': [[0, 0], [4, 5]] }, { 'color': "brown", 'score': 7, 'places': [[0, 1], [321, 123]] }, ] for json in jsons: p = PlayerData.from_json(json) self.assertEqual(Color(json['color']), p.get_color()) self.assertEqual(json['score'], p.get_score()) self.assertEqual([tuple(place) for place in json['places']], p.get_penguins())
def from_json(value): """ Class method to create a State from a JSON value. JSON value -> State """ players = [PlayerData.from_json(player) for player in value['players']] board = Board.from_json(value['board']) return State(players, board)