def test_valid_move(self): """ STS-1 Test 1 """ s = State() request = s.to_dict() request.pop('winner', None) request['piece'] = 1 request['target'] = 18 result = self.app.post('/move', data=json.dumps(request), headers={'content-type': 'application/json'}) self.assertEqual(200, result.status_code, 'Status is OK') expected = s.get_child(1 << 1, 1 << 18) expected = expected.to_dict() jd = json.loads(result.data) jd['prev_move'] = tuple(jd['prev_move']) jd['can_castle'] = tuple(jd['can_castle']) # subset because we're not considering jd['valid_moves'] self.assertTrue(expected.items() <= jd.items(), 'Returned response')
def test_winning(self): s = State((0, 0, 0, 0, 2 << 16, 4 << 16), (0, 0, 0, 0, 0, 1), turn='w', in_check=False) request = s.to_dict() request.pop('winner', None) request['piece'] = 17 request['target'] = 9 result = self.app.post('/moveai', data=json.dumps(request), headers={'content-type': 'application/json'}) self.assertEqual(200, result.status_code, 'Status is OK') data = json.loads(result.data) self.assertEqual(data['winner'], 'P1_WINS', 'White wins')
def reset(): s = State() moves = [i.prev_move for i in s.get_children()] legal_move_dict = {} for piece, target in moves: legal_move_dict[piece.bit_length() - 1] = legal_move_dict.get( piece.bit_length() - 1, []) + [target.bit_length() - 1] d = s.to_dict() d['legal_moves'] = legal_move_dict response = jsonify(d) response.status_code = 200 return response return response
def test_stalemate(self): s = State((0, 0, 0, 0, 1 << 24, 4 << 16), (0, 0, 0, 0, 0, 2), turn='w', in_check=False) request = s.to_dict() request.pop('winner', None) request['piece'] = 24 request['target'] = 16 result = self.app.post('/moveai', data=json.dumps(request), headers={'content-type': 'application/json'}) self.assertEqual(200, result.status_code, 'Status is OK') data = json.loads(result.data) self.assertEqual(data['winner'], 'DRAW', 'White moves, stalemate ensues')
def test_invalid_move(self): """ STS-1 Test 2 """ s = State() request = s.to_dict() request.pop('winner', None) request['piece'] = 1 request['target'] = 20 result = self.app.post('/move', data=json.dumps(request), headers={'content-type': 'application/json'}) self.assertEqual(400, result.status_code, 'Illegal request throws error') result_message = json.loads(result.data)['message'] self.assertEqual(result_message, 'Completely and utterly illegal move', 'Illegal move message')
def test_capture(self): s = State(wp=0x0800F000, bp=0x1000000000) request = s.to_dict() request.pop('winner', None) request['piece'] = 27 request['target'] = 36 result = self.app.post('/move', data=json.dumps(request), headers={'content-type': 'application/json'}) self.assertEqual(200, result.status_code, 'Status is OK') expected = s.get_child(1 << 27, 1 << 36) expected = expected.to_dict() jd = json.loads(result.data) jd['prev_move'] = tuple(jd['prev_move']) jd['can_castle'] = tuple(jd['can_castle']) self.assertTrue(expected.items() <= jd.items(), 'Returned response')
def test_valid_nowin(self): s = State() request = s.to_dict() request.pop('winner', None) request['piece'] = 1 request['target'] = 18 result = self.app.post('/moveai', data=json.dumps(request), headers={'content-type': 'application/json'}) self.assertEqual(200, result.status_code, 'Status is OK') move_one = s.get_child(1 << 1, 1 << 18) possible_children = set(move_one.get_children()) data = json.loads(result.data) actual = State.from_dict(data['pieces'], data['turn'], data['in_check'], data['can_castle'], data['prev_move']) self.assertTrue(actual in possible_children, 'Valid responding move')