def test_flip_flops(): code = """ let count: int while top[1, 1] != top[2, 2] or count == 0 do RUR'U' count = count + 1 end out(count) """ out_fn = MagicMock() stack = Stack() finish_function = MagicMock() cube_runtime = CubeRuntime(Cube((3, 3, 3)), Orientation(), lambda action: None, finish_function) cube_runtime.functions.initialize_stack(stack) stdlib.initialize_stack(stack) stack.add_global("out", Function(([Integer], Void))) globals = {"out": out_fn, **stdlib.exec_globals, **cube_runtime.functions.exec_globals} executor = ExecutionContext(globals) executor.compile(parser.parse(code, stack)) executor.execute(MockTracebackWriter()) cube_runtime.finished() out_fn.assert_called_once_with(6) finish_function.assert_called_once()
def test_action_callback(): callback: Callable[[Action], None] = MagicMock() runtime = CubeRuntime(Cube((3, 3, 3)), Orientation(), callback, lambda: None) runtime.perform_turn(Side.LEFT, 2, [1]) callback: MagicMock action = callback.call_args_list[0][0][0] assert isinstance(action, Turn) assert action.type == TurningType.VERTICAL
def test_get_color(side, orientation): class Colors: def __getitem__(self, item): pass class Side: def __init__(self): self.colors = Colors() with patch.object(Cube, 'get_side', return_value=Side()) as mock_method: runtime = CubeRuntime(Cube((3, 3, 3)), Orientation(), lambda action: None, lambda: None) runtime.get_color(side, 0, 0) mock_method.assert_called_once_with(orientation)
def test_runtime_globals(): runtime = CubeRuntime(Cube((3, 3, 3)), Orientation(), lambda action: None, lambda: None) existing = set(runtime.functions.global_values.keys()) assert existing.issuperset(set(CubeRuntime.COLOR_NAMES.keys())) assert existing.issuperset(set(CubeRuntime.SIDE_NAMES.keys())) assert "push_orientation" in existing
def test_orient(): code = """ orient top: {G--/---/---}, bottom: {--Y/---/---} then out(red) else-orient top: {-W-/---/---}, right: {---/---/-O-} then out(top[1, 1]) end """ out_fn = MagicMock() stack = Stack() cube_runtime = CubeRuntime(Cube((3, 3, 3)), Orientation(), lambda action: None, lambda: None) cube_runtime.functions.initialize_stack(stack) stdlib.initialize_stack(stack) stack.add_global("out", Function(([Color], Void))) globals = {"out": out_fn, **stdlib.exec_globals, **cube_runtime.functions.exec_globals} executor = ExecutionContext(globals) executor.compile(parser.parse(code, stack)) executor.execute(MockTracebackWriter()) cube_runtime.finished() out_fn.assert_called_once_with(orientation.Color.WHITE)
def test_suspend_rotations(): actions = [] runtime = CubeRuntime(Cube((2, 2, 2)), Orientation(), actions.append, lambda: None) runtime.perform_turn(Side.FRONT, 1, [1]) runtime.perform_rotate(Side.TOP, False) runtime.suspend_rotations() for _ in range(3): runtime.perform_turn(Side.FRONT, 1, [1]) runtime.perform_rotate(Side.TOP, False) runtime.resume_rotations() assert "FYFRBY'" == "".join(map(str, actions))
def test_state_stack(): actions = [] runtime = CubeRuntime(Cube((2, 2, 2)), Orientation(), actions.append, lambda: None) runtime.perform_turn(Side.FRONT, 1, [1]) runtime.perform_rotate(Side.TOP, False) runtime.push_orientation() for _ in range(3): runtime.perform_turn(Side.FRONT, 1, [1]) runtime.perform_rotate(Side.TOP, False) runtime.pop_orientation() assert "FYFYFYFYY" == "".join(map(str, actions))