def test_call(self): # With default select and action try: c = StructureBuildingCodelet() w = Workspace() # Add content to the Workspace for i in range(10): w(i) # invoke __call__ several times (testing for exceptions) for v in range(100): c(w) except Exception as e: self.fail(e) # With custom select and action try: c = StructureBuildingCodelet(select=lambda x: x < 3, transform=lambda x: x + 1) w = Workspace() # Add content to the Workspace for i in range(10): w(i) # invoke __call__ several times (testing for exceptions) for v in range(100): c(w) except Exception as e: self.fail(e)
def test_call(self): try: w = Workspace() # invoke __call__ several times (testing for exceptions) for v in range(100): w('content') except Exception as e: self.fail(e)
def test_next(self): try: w = Workspace() # next should return the last value that was updated into Workspace (using __call__) for expected in range(10): w(expected) # Update Workspace actual = next(w) self.assertEqual(expected, actual) except Exception as e: self.fail(e)
def test_iter(self): try: w = Workspace() # adding expected values to workspace values = range(10) for v in values: w(v) # should be able to iterate over expected values in order of insertion for expected, actual in zip(values, w): self.assertEqual(expected, actual) except Exception as e: self.fail(e)
def test_next(self): try: w = Workspace() # Add content to the Workspace for i in range(10): w(i) for v in range(10): c = StructureBuildingCodelet(select=lambda x: x == v, transform=lambda x: x + 1) c(w) actual = next(c) self.assertEqual([v + 1], actual) except Exception as e: self.fail(e)
def test_create_move_sbc(self): EMPTY_BOARD = Board.blank_board() SINGLE_BLANK_BOARD = Board( ['O', 'X', 'O', 'X', 'O', 'X', 'O', BLANK, 'O']) FULL_BOARD = Board(['X'] * 9) try: # Scenario #1 - Empty Board ########################### sbc = StructureBuildingCodelet( select=lambda s: is_board(s) and not s.is_full(), transform=create_move) # Update Workspace workspace = Workspace() workspace(EMPTY_BOARD) # Update SBC and Retrieve New Structures sbc(workspace) # Should only be one new structure new_structures = next(sbc) self.assertEqual(len(new_structures), 1) # Both move and new board should be defined move, new_board = new_structures[0] self.assertIsNotNone(move) self.assertIsNotNone(new_board) # Should be a legal move self.assertTrue(0 <= move.position < len(EMPTY_BOARD)) # Move and returned board should be consistent expected_board = EMPTY_BOARD.copy() expected_board[move.position] = move.mark self.assertEqual(new_board, expected_board) # Scenario #2 - Full Board ########################## sbc = StructureBuildingCodelet( select=lambda s: is_board(s) and not s.is_full(), transform=create_move) # Update Workspace workspace = Workspace() workspace(FULL_BOARD) # Update SBC and Retrieve New Structures sbc(workspace) new_structures = next(sbc) self.assertEqual(len(new_structures), 0) # Scenario #3 - Single Open Position #################################### sbc = StructureBuildingCodelet( select=lambda s: is_board(s) and not s.is_full(), transform=create_move) # Update Workspace workspace = Workspace() workspace(SINGLE_BLANK_BOARD) # Update SBC and Retrieve New Structures sbc(workspace) new_structures = next(sbc) self.assertEqual(len(new_structures), 1) move, new_board = new_structures[0] blank_pos = SINGLE_BLANK_BOARD.first_blank self.assertEqual(move, (blank_pos, 'X')) expected_board = SINGLE_BLANK_BOARD.copy() expected_board[move.position] = move.mark self.assertEqual(new_board, expected_board) self.assertTrue(expected_board.is_full()) # Scenario #4 - Multiple Boards in Workspace ############################################ sbc = StructureBuildingCodelet( select=lambda s: is_board(s) and not s.is_full(), transform=create_move) # Update Workspace workspace = Workspace() workspace(SINGLE_BLANK_BOARD) workspace(FULL_BOARD) workspace(EMPTY_BOARD) # Update SBC and Retrieve New Structures sbc(workspace) new_structures = next(sbc) # Full board filtered out by select criteria self.assertEqual(len(new_structures), 2) except Exception as e: self.fail(e)
def test_init(self): try: w = Workspace() except Exception as e: self.fail(e)