Best Python code snippet using lisa_python
test_gamelogic_players.py
Source:test_gamelogic_players.py
...111213class TestUtils:1415 def get_disks(self, black_pos, white_pos, black=None, white=None):16 disks = [[None for _ in range(8)] for _ in range(8)]1718 for r, c in black_pos:19 disks[r][c] = black20 for r, c in white_pos:21 disks[r][c] = white22 return disks2324 def get_disk_instance(self, color):25 disk = mock.create_autospec(26 spec=Disk,27 instance=True,28 color=color29 )30 return disk3132 def get_gameover_disks(self, black, white):33 disks = [[white for _ in range(8)] for _ in range(8)]34 disks[0][0] = None35 for r, c in [(0, 1), (1, 0), (1, 1)]:36 disks[r][c] = black37 return disks3839 def get_board_instance(self, disks):40 board = mock.create_autospec(41 spec=Board,42 instance=True,43 disks=disks,44 grid_num=845 )46 return board4748 def find_best_move(self, cands):49 for cand in cands:50 yield cand5152 def get_placeables(self, positions):53 for pos in positions:54 yield pos555657class GameLogicTestCase(TestCase, TestUtils):58 """Tests for Board class59 """6061 def setUp(self):62 self.game_logic = GameLogic()63 black_pos = [(2, 2), (3, 3), (3, 4), (4, 4)]64 white_pos = [(4, 3), (5, 3), (5, 4), (5, 5)]65 self.piece_disks = self.get_disks(66 black_pos,67 white_pos,68 Piece.BLACK,69 Piece.WHITE70 )71 self.sprite_disks = self.get_disks(72 black_pos,73 white_pos,74 self.get_disk_instance(Piece.BLACK),75 self.get_disk_instance(Piece.WHITE)76 )7778 def test_reverse_check(self):79 tests = [80 [(self.piece_disks, Piece.BLACK.value, 5, 3, -1, 0), 2],81 [(self.piece_disks, Piece.BLACK.value, 5, 3, -1, -1), None],82 [(self.piece_disks, Piece.BLACK.value, 5, 3, -1, 1), 1],83 [(self.piece_disks, Piece.WHITE.value, 3, 4, 1, 0), 2],84 [(self.piece_disks, Piece.WHITE.value, 3, 4, 1, -1), 1],85 [(self.piece_disks, Piece.WHITE.value, 5, 5, 0, -1), 0],86 [(self.piece_disks, Piece.WHITE.value, 2, 2, -1, 0), None],87 [(self.sprite_disks, Piece.BLACK, 5, 3, -1, 0), 2],88 [(self.sprite_disks, Piece.BLACK, 5, 3, -1, -1), None],89 [(self.sprite_disks, Piece.BLACK, 5, 3, -1, 1), 1],90 [(self.sprite_disks, Piece.WHITE, 3, 4, 1, 0), 2],91 [(self.sprite_disks, Piece.WHITE, 3, 4, 1, -1), 1],92 [(self.sprite_disks, Piece.WHITE, 5, 5, 0, -1), 0],93 [(self.sprite_disks, Piece.WHITE, 2, 2, -1, 0), None]94 ]95 for test, expect in tests:96 with self.subTest(test):97 result = self.game_logic.reverse_check(*test)98 self.assertEqual(result, expect)99100 def test_is_placeable(self):101 tests = [102 [(0, 2, self.piece_disks, Piece.BLACK.value), False],103 [(6, 3, self.piece_disks, Piece.WHITE.value), False],104 [(2, 2, self.piece_disks, Piece.BLACK.value), False],105 [(3, 4, self.piece_disks, Piece.WHITE.value), False],106 [(6, 3, self.piece_disks, Piece.BLACK.value), True],107 [(2, 5, self.piece_disks, Piece.WHITE.value), True],108 [(0, 2, self.sprite_disks, Piece.BLACK), False],109 [(6, 3, self.sprite_disks, Piece.WHITE), False],110 [(2, 2, self.sprite_disks, Piece.BLACK), False],111 [(3, 4, self.sprite_disks, Piece.WHITE), False],112 [(6, 3, self.sprite_disks, Piece.BLACK), True],113 [(2, 5, self.sprite_disks, Piece.WHITE), True],114 ]115 for test, expect in tests:116 with self.subTest(test):117 result = self.game_logic.is_placeable(*test)118 self.assertEqual(result, expect)119120 def test_get_reversibles(self):121 expect = [(3, 4), (4, 3), (5, 2)]122 result = [pos for pos in self.game_logic.get_reversibles(3, 4, 1, -1, 3)]123 self.assertEqual(result, expect)124125 def test_find_reversibles(self):126 tests = [127 [(6, 3, self.piece_disks, Piece.BLACK.value), [(5, 3), (4, 3)]],128 [(1, 1, self.piece_disks, Piece.WHITE.value), [(2, 2), (3, 3), (4, 4)]],129 [(2, 3, self.piece_disks, Piece.WHITE.value), [(3, 3)]],130 [(6, 3, self.sprite_disks, Piece.BLACK), [(5, 3), (4, 3)]],131 [(1, 1, self.sprite_disks, Piece.WHITE), [(2, 2), (3, 3), (4, 4)]],132 [(2, 3, self.sprite_disks, Piece.WHITE), [(3, 3)]]133 ]134 for test, expect in tests:135 result = [pos for pos in self.game_logic.find_reversibles(*test)]136 self.assertEqual(result, expect)137138 def test_has_placeables_true(self):139 tests = [140 (self.piece_disks, Piece.WHITE.value),141 (self.piece_disks, Piece.BLACK.value),142 (self.sprite_disks, Piece.WHITE),143 (self.sprite_disks, Piece.BLACK)144 ]145 for test in tests:146 with self.subTest():147 result = self.game_logic.has_placeables(*test)148 self.assertTrue(result)149150 def test_has_placeables_false(self):151 piece_disks = self.get_gameover_disks(Piece.BLACK, Piece.WHITE)152 sprite_disks = self.get_gameover_disks(153 self.get_disk_instance(Piece.BLACK), self.get_disk_instance(Piece.WHITE))154 tests = [155 (piece_disks, Piece.BLACK.value),156 (sprite_disks, Piece.BLACK)157 ]158 for test in tests:159 with self.subTest(test):160 result = self.game_logic.has_placeables(*test)161 self.assertFalse(result)162163164class PlayersTestCase(TestCase):165 """Tests for Players class166 """167168 def setUp(self):169 self.mock_sound = mock.MagicMock()170 mock_mixier = mock.patch('othello.pygame.mixer.Sound').start()171 mock_mixier.return_value = self.mock_sound172173 self.disks = [[None for _ in range(8)] for _ in range(8)]174 self.mock_board = mock.create_autospec(175 spec=Board, instance=True, disks=self.disks)176 self.players = Players(self.mock_board, Piece.BLACK)177178 def tearDown(self):179 mock.patch.stopall()180181 def test_set_color(self):182 tests = [Piece.WHITE, Piece.BLACK]183 expects = [184 (Piece.WHITE, Piece.BLACK, Images.WHITE_DISPLAY),185 (Piece.BLACK, Piece.WHITE, Images.BLACK_DISPLAY),186 ]187 for test, (color, opposite, display) in zip(tests, expects):188 with self.subTest(test):189 self.players.set_color(test)190 self.assertEqual(self.players.color, color)191 self.assertEqual(self.players.opposite_color, opposite)192 self.assertEqual(self.players.display_disk, display)193194 def test_reverse(self):195 positions = [(2, 3), (3, 3), (4, 3)]196 calls = [mock.call(r, c, Piece.BLACK) for r, c in positions]197 self.players.clicked = (1, 3)198199 def find_reversibles():200 for pos in positions:201 yield pos202203 with mock.patch('othello.GameLogic.find_reversibles') as mock_find:204 mock_find.return_value = find_reversibles()205 self.players.reverse()206207 self.mock_sound.play.assert_called_once()208 mock_find.assert_called_once_with(1, 3, self.disks, Piece.BLACK)209 self.assertEqual(self.mock_board.reverse.call_args_list, calls)210211 def test_click(self):212 self.players.click(5, 4)213 self.mock_sound.play.assert_called_once_with()214 self.mock_board.place.assert_called_once_with(5, 4, Piece.BLACK)215216217class PlayerCommon(TestCase, TestUtils):218219 def setUp(self):220 mock.patch('othello.pygame.mixer.Sound').start()221 black_pos = [(2, 2), (3, 3), (3, 4), (4, 4)]222 white_pos = [(4, 3), (5, 3), (5, 4), (5, 5)]223 self.disks = self.get_disks(224 black_pos,225 white_pos,226 self.get_disk_instance(Piece.BLACK),227 self.get_disk_instance(Piece.WHITE)228 )229 self.mock_board = self.get_board_instance(self.disks)230231 def tearDown(self):232 mock.patch.stopall()233234235class PlayerTestCase(PlayerCommon):236 """Tests for Player class237 """238239 def setUp(self):240 super().setUp()241 self.player = Player(self.mock_board, Piece.BLACK)242243 def test_place_true(self):244 pt = Point(397, 512)245 pos = (6, 1)246 self.mock_board.find_position.return_value = pos247248 with mock.patch('othello.GameLogic.is_placeable') as mock_placeable, \249 mock.patch('othello.Players.click') as mock_click:250 mock_placeable.return_value = True251252 result = self.player.place(pt)253 self.assertTrue(result)254 self.mock_board.find_position.assert_called_once_with(pt.x, pt.y)255 mock_placeable.assert_called_once_with(*pos, self.disks, Piece.BLACK)256 mock_click.assert_called_once_with(*pos)257258 def test_place_false(self):259 pt = Point(397, 512)260 pos = (6, 1)261 self.mock_board.find_position.return_value = pos262263 with mock.patch('othello.GameLogic.is_placeable') as mock_placeable, \264 mock.patch('othello.Players.click') as mock_click:265 mock_placeable.return_value = False266267 result = self.player.place(pt)268 self.assertFalse(result)269 self.mock_board.find_position.assert_called_once_with(pt.x, pt.y)270 mock_placeable.assert_called_once_with(*pos, self.disks, Piece.BLACK)271 mock_click.assert_not_called()272273274class OpponentTestCase(PlayerCommon):275 """Tests for Opponent class276 """277278 def setUp(self):279 super().setUp()280 self.opponent = Opponent(self.mock_board, Piece.WHITE)281282 def test_get_placeables(self):283 black_pos = [(2, 2), (3, 3), (3, 4), (4, 4)]284 white_pos = [(4, 3), (5, 3), (5, 4), (5, 5)]285 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)286 expects = [(1, 1), (2, 3), (2, 4), (2, 5), (3, 5), (4, 5)]287 result = [pos for pos in self.opponent.get_placeables(disks, Piece.WHITE)]288 self.assertEqual(result, expects)289290 def test_find_corners(self):291 grids = ((0, 3), (0, 7), (5, 6), (7, 0), (7, 1))292 expect = (0, 7)293 result = self.opponent.find_corners(grids)294 self.assertEqual(result, expect)295296 def test_counter_corners(self):297 black_pos = [(7, 7)]298 white_pos = [(0, 7), (7, 0), (0, 0)]299 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)300301 tests = [302 [Piece.BLACK, (3, 1)],303 [Piece.WHITE, (1, 3)]304 ]305 for color, expect in tests:306 with self.subTest(color):307 self.opponent.color = color308 result = self.opponent.counter(disks, self.opponent._corners)309 self.assertEqual(result, expect)310311 def test_counter_non_corners(self):312 black_pos = [(7, 7), (1, 0), (3, 2), (5, 3)]313 white_pos = [(0, 7), (7, 6), (4, 3)]314 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)315316 tests = [317 [Piece.BLACK, (2, 3)],318 [Piece.WHITE, (3, 2)]319 ]320 for color, expect in tests:321 with self.subTest(color):322 self.opponent.color = color323 result = self.opponent.counter(disks, self.opponent._non_corners)324 self.assertEqual(result, expect)325326 def test_counter_around_corners(self):327 black_pos = [(1, 0), (3, 2), (1, 7)]328 white_pos = [(0, 7), (7, 6), (6, 6), (6, 7), (4, 3)]329 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)330331 tests = [332 [Piece.BLACK, (3, 1)],333 [Piece.WHITE, (1, 3)]334 ]335 for color, expect in tests:336 with self.subTest(color):337 self.opponent.color = color338 corners = [None, Piece.WHITE, None, None]339 result = self.opponent.counter(340 disks, lambda: self.opponent._around_corners(corners))341 self.assertEqual(result, expect)342343 def test_counter_sides(self):344 black_pos = [(1, 0), (3, 2), (1, 7), (0, 2), (0, 3), (4, 0)]345 white_pos = [(0, 7), (7, 6), (6, 6), (4, 3), (7, 4), (4, 7)]346 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)347348 tests = [349 [Piece.BLACK, (2, 3)],350 [Piece.WHITE, (3, 2)]351 ]352 for color, expect in tests:353 with self.subTest(color):354 self.opponent.color = color355 result = self.opponent.counter(disks, self.opponent._sides)356 self.assertEqual(result, expect)357358 def test_copy_currnet_board(self):359 result = self.opponent.copy_current_board()360361 for r in range(8):362 for c in range(8):363 with self.subTest((r, c)):364 if item := self.opponent.board.disks[r][c]:365 self.assertEqual(item.color, result[r][c])366 else:367 self.assertTrue(result[r][c] is None)368369 def test_simulate(self):370 white_pos = [(0, 4), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (4, 2), (5, 1)]371 black_pos = [(3, 2), (3, 3), (3, 4), (4, 3), (4, 4), (4, 5), (5, 5), (6, 5)]372 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)373 tests = [Piece.BLACK, Piece.WHITE]374 expects = [(0, 4, 1), (0, 1, 0)]375376 for test, expect in zip(tests, expects):377 with self.subTest():378 result = self.opponent.simulate(disks, test)379 self.assertEqual(result, expect)380381 def test_evaluate(self):382 white_pos = [(0, 0), (0, 1), (0, 2), (1, 0), (2, 0)]383 black_pos = [(0, 5), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (3, 2), (3, 3)]384 disks = self.get_disks(black_pos, white_pos, Piece.BLACK, Piece.WHITE)385 result = self.opponent.evaluate(disks)386 self.assertEqual(result, 34)387388 def test_find_best_move(self):389 grids = [(2, 4), (4, 1)]390 disks = self.get_disks([], [])391 expect = [392 Candidate(evaluation=10, row=2, col=4, corners=1, arounds=2, sides=3),393 Candidate(evaluation=20, row=4, col=1, corners=4, arounds=5, sides=6)394 ]395396 def find_reversibles(li):397 for r, c in li:398 yield (r, c)399400 with mock.patch('othello.GameLogic.find_reversibles') as mock_find, \401 mock.patch('othello.Opponent.evaluate') as mock_evaluate, \402 mock.patch('othello.Opponent.simulate') as mock_simulate:403 mock_find.side_effect = [find_reversibles([(3, 4), (4, 4)]), find_reversibles([(4, 2), (4, 3)])]404 mock_evaluate.side_effect = [10, 20]405 mock_simulate.side_effect = [(1, 2, 3), (4, 5, 6)]406 result = [cand for cand in self.opponent.find_best_move(grids, disks, Piece.WHITE)]407 self.assertEqual(result, expect)408409 def test_guess_choice(self):410 cands = [Candidate(0, 2, 4, 0, 0, 0), Candidate(0, 3, 4, 0, 0, 0), Candidate(0, 3, 5, 0, 0, 0)]411412 with mock.patch('othello.Opponent.find_best_move') as mock_best_move, \413 mock.patch('othello.random.choice') as mock_choice:414 mock_best_move.return_value = self.find_best_move(cands)415 mock_choice.return_value = cands[1]416 result = self.opponent.guess(mock.MagicMock(), mock.MagicMock())417 mock_choice.assert_called_once()418 self.assertEqual(result, (3, 4))419420 def test_guess(self):421 tests = [422 [Candidate(10, 2, 4, 0, 0, 0), Candidate(20, 3, 4, 0, 3, 0), Candidate(20, 3, 5, 0, 2, 0)],423 [Candidate(10, 2, 4, 0, 3, 5), Candidate(10, 3, 4, 0, 2, 5), Candidate(10, 3, 5, 0, 1, 5)],424 [Candidate(10, 2, 4, 0, 0, 0), Candidate(20, 3, 4, 0, 0, 0), Candidate(30, 3, 5, 0, 0, 0)],425 [Candidate(10, 2, 4, 0, 0, 1), Candidate(30, 3, 4, 0, 0, 5), Candidate(30, 3, 5, 0, 0, 4)],426 [Candidate(30, 2, 4, 1, 3, 0), Candidate(30, 3, 4, 1, 2, 0), Candidate(30, 3, 5, 2, 0, 0)],427 [Candidate(30, 2, 4, 1, 3, 2), Candidate(30, 3, 4, 1, 3, 1), Candidate(30, 3, 5, 1, 2, 1)],428 [Candidate(20, 2, 4, 1, 0, 0), Candidate(30, 3, 4, 2, 0, 0), Candidate(30, 3, 5, 1, 0, 0)],429 [Candidate(20, 2, 4, 2, 0, 1), Candidate(20, 3, 4, 2, 0, 2), Candidate(10, 3, 5, 1, 0, 1)]430 ]431 expects = [(3, 4), (2, 4), (3, 5), (3, 5), (2, 4), (3, 4), (3, 5), (2, 4)]432433 with mock.patch('othello.Opponent.find_best_move') as mock_best_move:434 for test, expect in zip(tests, expects):435 with self.subTest(test):436 mock_best_move.return_value = self.find_best_move(test)437 result = self.opponent.guess(mock.MagicMock(), mock.MagicMock())438 self.assertEqual(result, (expect))439440 def test_place_corner(self):441 positions = [(0, 0), (0, 7)]442443 with mock.patch('othello.Opponent.get_placeables') as mock_placeables, \444 mock.patch('othello.Opponent.guess') as mock_guess, \445 mock.patch('othello.Players.click') as mock_click:446 mock_placeables.return_value = self.get_placeables(positions)447 self.opponent.place()448 mock_click.assert_called_once_with(*positions[0])449 mock_guess.assert_not_called()450451 def test_place_filtered(self):452 black_pos = [(2, 2), (3, 3), (3, 4), (4, 4)]453 white_pos = [(4, 3), (5, 3), (5, 4), (5, 5)]454 disks = self.get_disks(455 black_pos, white_pos, Piece.BLACK, Piece.WHITE)456 positions = [(0, 1), (1, 0), (2, 3), (3, 4), (4, 5)]457 filtered = [(2, 3), (3, 4), (4, 5)]458 guessed = (1, 3)459460 with mock.patch('othello.Opponent.get_placeables') as mock_placeables, \461 mock.patch('othello.Opponent.guess') as mock_guess, \462 mock.patch('othello.Players.click') as mock_click:463 mock_placeables.return_value = self.get_placeables(positions)464 mock_guess.return_value = guessed465 self.opponent.place()466467 mock_placeables.assert_called_once_with(disks, Piece.WHITE)468 mock_guess.assert_called_once_with(filtered, disks)469 mock_click.assert_called_once_with(*guessed)470471 def test_place_no_filtered(self):472 black_pos = [(2, 2), (3, 3), (3, 4), (4, 4)]473 white_pos = [(4, 3), (5, 3), (5, 4), (5, 5)]474 disks = self.get_disks(475 black_pos, white_pos, Piece.BLACK, Piece.WHITE)476 positions = [(0, 1), (1, 0), (1, 1)]477 guessed = (1, 3)478479 with mock.patch('othello.Opponent.get_placeables') as mock_placeables, \480 mock.patch('othello.Opponent.guess') as mock_guess, \481 mock.patch('othello.Players.click') as mock_click:482 mock_placeables.return_value = self.get_placeables(positions)483 mock_guess.return_value = guessed484 self.opponent.place()485486 mock_placeables.assert_called_once_with(disks, Piece.WHITE)487 mock_guess.assert_called_once_with(positions, disks)488 mock_click.assert_called_once_with(*guessed)
...
main.py
Source:main.py
1import os.path2from time import sleep3import keyboard4from random import randint5def get_disks():6 disks_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'7 connected_disks = ['%s:' % d for d in disks_list if os.path.exists('%s:' % d)]8 return connected_disks9def detect_disk(last_disks, curr_disks):10 connected = [item for item in curr_disks if item not in last_disks]11 disconnected = [item for item in last_disks if item not in curr_disks]12 if connected:13 return True14 return False15def matrix(n):16 text = ""17 for i in range(n):18 text += chars[randint(0, len(chars)-1)]19 return text20def enter_root():21 pw = input("enter admin password : ") 22 if pw == "123456":23 return True24 print("process failed")25 return False26def styled_print(str):27 l = len(str)28 for i in range(l*3):29 print(f"\r{matrix(l)}", end="")30 sleep(0.2)31def decrypt():32 symb = ["\\", "|", "/", "-"]33 text = "Decrypting bypass key - [--------------------]"34 liste = [char for char in text]35 delay_print("".join(liste), False)36 for i in range(40):37 liste[22] = symb[int(i%4)]38 if i%2 == 0:39 liste[25+int(i/2)] = "#"40 print("".join(liste), end="\r")41 sleep(0.1)42def delay_print(strings, p):43 text = ""44 for char in strings:45 text += char46 print(text, end='\r')47 sleep(0.000001)48 if p:49 print()50 51numbers = range(48, 58)52letters = range(97, 103)53chars = [chr(n) for n in (*numbers, *letters)]54last_disks = get_disks()55curr_disks = get_disks()56path = "restricted"57usb = False58running = True59while running:60 print()61 cmd = input(f"{path}/>")62 63 if cmd == "root":64 success = enter_root()65 if success:66 path = "root"67 68 elif cmd == "open door":69 if path == "root":70 curr_disks = get_disks()71 usb = detect_disk(last_disks, curr_disks)72 last_disks = curr_disks73 if usb:74 decrypt()75 print()76 delay_print("door unlocked", True)77 else:78 delay_print("bypass key not detected, insert it", True)79 else:80 delay_print("command not available, root only", True)81 elif cmd == "quit":82 break83 elif cmd == "":84 pass...
hanoi-tower-by-stacks.py
Source:hanoi-tower-by-stacks.py
...34 if n > 0:35 self.move_disks(n-1, buffer, destination)36 self.move_top_to(destination)37 buffer.move_disks(n-1, destination, self)38 def get_disks(self):39 return self.disks40if __name__ == "__main__":41 # Your Tower object will be instantiated and called as such:42 towers = [Tower(0), Tower(1), Tower(2)]43 n = 344 for i in range(n - 1, -1, -1):45 towers[0].add(i)46 towers[0].move_disks(n, towers[2], towers[1])...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!