Advertisement
geosabev

python_hw04_tests

Nov 28th, 2022 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.40 KB | Software | 0 0
  1. import unittest
  2.  
  3. from solution1 import *
  4.  
  5.  
  6. class TestChessException(unittest.TestCase):
  7.     """Test the ChessException class."""
  8.  
  9.     def test_existence(self):
  10.         """Sanity test for the ChessException class."""
  11.         exception = ChessException('Paul Morphy')
  12.  
  13.  
  14. class TestChessScore(unittest.TestCase):
  15.     """Test the ChessScore class."""
  16.  
  17.     def test_simple_init(self):
  18.         """Sanity test for the ChessScore class."""
  19.         score = ChessScore(['p'])
  20.  
  21.     def test_conversion_to_int(self):
  22.         """Test __int__ method."""
  23.         score = ChessScore(['r', 'b'])
  24.  
  25.         self.assertEqual(int(score), 8)
  26.  
  27.     def test_empty_input(self):
  28.         """Test empty array of pieces."""
  29.         score = ChessScore([])
  30.  
  31.         self.assertEqual(int(score), 0)
  32.  
  33.     def test_less_than_operator(self):
  34.         """Test __lt__ method."""
  35.         rock_score = ChessScore(['r'])
  36.         queen_score = ChessScore(['q'])
  37.  
  38.         self.assertLess(rock_score, queen_score)
  39.  
  40.     def test_less_than_or_equal_operator(self):
  41.         """Test __le__ method."""
  42.         rock_score = ChessScore(['r'])
  43.         queen_score = ChessScore(['q'])
  44.  
  45.         self.assertLessEqual(rock_score, queen_score)
  46.  
  47.     def test_equal_operator(self):
  48.         """Test __eq__ method."""
  49.         king1 = ChessScore(['k'])
  50.         king2 = ChessScore(['k'])
  51.  
  52.         self.assertEqual(king1, king2)
  53.  
  54.     def test_greater_than_or_equal_operator(self):
  55.         """Test __ge__ method."""
  56.         queen_score = ChessScore(['q'])
  57.         rock_score = ChessScore(['r'])
  58.  
  59.         self.assertGreaterEqual(queen_score, rock_score)
  60.  
  61.     def test_greater_than_operator(self):
  62.         """Test __gt__ method."""
  63.         queen_score = ChessScore(['q'])
  64.         rock_score = ChessScore(['r'])
  65.  
  66.         self.assertGreater(queen_score, rock_score)
  67.  
  68.     def test_not_equal_operator(self):
  69.         """Test __ne__ method."""
  70.         queen_score = ChessScore(['q'])
  71.         rock_score = ChessScore(['r'])
  72.  
  73.         self.assertNotEqual(queen_score, rock_score)
  74.  
  75.     def test_addition_operator(self):
  76.         """Test __add__ method."""
  77.         score1 = ChessScore(['q', 'p'])
  78.         score2 = ChessScore(['r', 'n'])
  79.  
  80.         self.assertEqual(score1+score2, 18)
  81.  
  82.     def test_subtraction_operator(self):
  83.         """Test __sub__ method."""
  84.         score1 = ChessScore(['q', 'p'])
  85.         score2 = ChessScore(['r', 'n'])
  86.  
  87.         self.assertEqual(score1-score2, 2)
  88.  
  89.  
  90. class TestChessPosition(unittest.TestCase):
  91.     """Test the ChessPosition class."""
  92.  
  93.     def test_simple_init(self):
  94.         """Sanity test for the ChessPosition class."""
  95.         init_pos = ChessPosition('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
  96.         self.assertTrue(hasattr(init_pos, 'get_white_score'))
  97.         self.assertTrue(hasattr(init_pos, 'get_black_score'))
  98.         self.assertTrue(hasattr(init_pos, 'white_is_winning'))
  99.         self.assertTrue(hasattr(init_pos, 'black_is_winning'))
  100.         self.assertTrue(hasattr(init_pos, 'is_equal'))
  101.  
  102.     def test_missing_king(self):
  103.         """Test for missing king."""
  104.         with self.assertRaises(ChessException) as cm:
  105.             ChessPosition('rnbq1bnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
  106.  
  107.         self.assertEqual(str(cm.exception), 'kings')
  108.  
  109.     def test_multiple_kings(self):
  110.         """Test for more than one kings of same color."""
  111.         with self.assertRaises(ChessException) as cm:
  112.             ChessPosition('rnbqkbnr/pkpppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
  113.  
  114.         self.assertEqual(str(cm.exception), 'kings')
  115.  
  116.     def test_neighbor_kings_1(self):
  117.         """Test for kings next to each other."""
  118.         with self.assertRaises(ChessException) as cm:
  119.             ChessPosition('rnbqkKnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQ1BNR')
  120.  
  121.         self.assertEqual(str(cm.exception), 'kings')
  122.  
  123.     def test_neighbor_kings_2(self):
  124.         """Test for kings diagonally next to each other."""
  125.         with self.assertRaises(ChessException) as cm:
  126.             ChessPosition('rnbqkbnr/pppppKpp/8/8/8/8/PPPPPPPP/RNBQ1BNR')
  127.  
  128.         self.assertEqual(str(cm.exception), 'kings')
  129.  
  130.     def test_invalid_pawns_position_1(self):
  131.         """Test for pawn on eight row."""
  132.         with self.assertRaises(ChessException) as cm:
  133.             ChessPosition('rnbpkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
  134.  
  135.         self.assertEqual(str(cm.exception), 'pawns')
  136.  
  137.     def test_invalid_pawns_position_2(self):
  138.         """Test for pawn on first row."""
  139.         with self.assertRaises(ChessException) as cm:
  140.             ChessPosition('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBPKBNR')
  141.  
  142.         self.assertEqual(str(cm.exception), 'pawns')
  143.  
  144.     def test_string_representation(self):
  145.         """Test __str__ method."""
  146.         self.assertEqual(str(ChessPosition('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')),
  147.                          'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
  148.  
  149.     def test_len_method(self):
  150.         """Test __len__ method."""
  151.         self.assertEqual(len(ChessPosition('4k3/8/3pp3/8/8/8/1P6/4K3')), 5)
  152.  
  153.     def test_index_operator(self):
  154.         """Test __getitem__ method."""
  155.         position = ChessPosition('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR')
  156.         score = int(ChessScore(
  157.             [position['A8'], position['B7'], position['E8'].lower()]))
  158.  
  159.         self.assertEqual(score, 10)
  160.         self.assertEqual(position['C6'], None)
  161.  
  162.     def test_get_white_score(self):
  163.         """Test for getting white score."""
  164.         position = ChessPosition('4k3/8/8/8/8/8/PPPPPPPP/4K3')
  165.  
  166.         self.assertEqual(int(position.get_white_score()), 12)
  167.  
  168.     def test_get_black_score(self):
  169.         """Test for getting black score."""
  170.         position = ChessPosition('4k3/8/8/8/8/8/PPPPPPPP/4K3')
  171.  
  172.         self.assertEqual(int(position.get_black_score()), 4)
  173.  
  174.     def test_white_is_winning(self):
  175.         """Test for white_is_winning method."""
  176.         position = ChessPosition('4k3/8/8/8/8/8/PPPPPPPP/4K3')
  177.  
  178.         self.assertTrue(position.white_is_winning())
  179.  
  180.     def test_black_is_winning(self):
  181.         """Test for black_is_winning method."""
  182.         position = ChessPosition('4k3/pppppppp/8/8/8/8/8/4K3')
  183.  
  184.         self.assertTrue(position.black_is_winning())
  185.  
  186.     def test_is_equal(self):
  187.         """Test for is_equal method."""
  188.         position = ChessPosition('4k3/pppppppp/8/8/8/8/PPPPPPPP/4K3')
  189.  
  190.         self.assertTrue(position.is_equal())
  191.  
  192.  
  193. if __name__ == '__main__':
  194.     unittest.main()
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement