Esempio n. 1
0
def test_shape_printing(controller: Controller, shape_commands,
                        stream: io.StringIO, shapes: Dict[str, Shape]):
    for command in shape_commands:
        controller.execute_command(command)

    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {shape_commands[3]}\n{shapes["rectangle"]}\n'
        f' > {shape_commands[4]}\n{shapes["circle"]}')
    assert controller._command_engine._undos == [
        shape_commands[0],
        shape_commands[1],
        shape_commands[2],
        shape_commands[3],
        shape_commands[4],
    ]
    assert controller._shapes._shapes == [*shapes.values()]

    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
    )
    assert stream.getvalue() == res
Esempio n. 2
0
def test_move_shapes(controller: Controller, shape_commands, commands,
                     stream: io.StringIO, shapes: Dict[str, Shape]):
    for command in shape_commands:
        controller.execute_command(command)

    rect = Rectangle(top_left=Point(10, 10),
                     width=10,
                     height=10,
                     color=Color(10, 20, 30))
    moved_rect = Rectangle(top_left=Point(-10, 20),
                           width=rect.width,
                           height=rect.height,
                           color=rect.color)
    moved_polyline = Polyline(Point(-10, 20),
                              Point(0, 30),
                              Point(10, 20),
                              color=shapes['polyline'].color)
    rect_command = PrintRectCommand(receiver=controller,
                                    start_x=rect.start.x,
                                    start_y=rect.start.y,
                                    color=rect.color,
                                    rect_factory=DimensionsRectFactory,
                                    width=rect.width,
                                    height=rect.height)
    controller.execute_command(rect_command)

    controller.execute_command(commands[4])
    assert controller._shapes._shapes == [
        shapes['dot'], shapes['line'], shapes['rectangle'], shapes['circle'],
        moved_polyline, moved_rect
    ]
    assert controller._command_engine._undos == [
        *shape_commands, rect_command, commands[4]
    ]
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {shape_commands[3]}\n{shapes["rectangle"]}\n'
        f' > {shape_commands[4]}\n{shapes["circle"]}\n'
        f' > {rect_command}\n{rect}\n'
        f' > {commands[4]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{rect}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{moved_polyline}\n{moved_rect}\n'
    )
    assert stream.getvalue() == res

    controller.execute_command(commands[5])
    assert controller._shapes._shapes == [
        shapes['dot'], shapes['line'], shapes['rectangle'], shapes['circle'],
        moved_polyline, moved_rect
    ]
    assert controller._command_engine._undos == [
        *shape_commands, rect_command, commands[4]
    ]
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {shape_commands[3]}\n{shapes["rectangle"]}\n'
        f' > {shape_commands[4]}\n{shapes["circle"]}\n'
        f' > {rect_command}\n{rect}\n'
        f' > {commands[4]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{rect}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{moved_polyline}\n{moved_rect}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{moved_polyline}\n{moved_rect}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{moved_polyline}\n{moved_rect}\n'
    )
    assert stream.getvalue() == res
Esempio n. 3
0
def test_undo_redo(controller: Controller, shape_commands, stream: io.StringIO,
                   shapes: Dict[str, Shape]):
    for command in shape_commands:
        controller.execute_command(command)
    assert controller._command_engine._redos == []
    assert controller._gui._ui.actionUndo.isEnabled() is True
    assert controller._gui._ui.actionRedo.isEnabled() is False

    for i in range(len(shape_commands)):
        controller.undo()
    assert controller._command_engine._undos == []
    assert controller._command_engine._redos == [
        shape_commands[4],
        shape_commands[3],
        shape_commands[2],
        shape_commands[1],
        shape_commands[0],
    ]
    assert controller._shapes._shapes == []
    assert controller._gui._ui.actionUndo.isEnabled() is False
    assert controller._gui._ui.actionRedo.isEnabled() is True
    assert controller._gui._ui.history.toPlainText() == ''
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n')
    assert stream.getvalue() == res

    controller.redo()
    controller.redo()
    assert controller._command_engine._undos == [
        shape_commands[0], shape_commands[1]
    ]
    assert controller._command_engine._redos == [
        shape_commands[4], shape_commands[3], shape_commands[2]
    ]
    assert controller._shapes._shapes == [shapes['dot'], shapes['line']]
    assert controller._gui._ui.actionUndo.isEnabled() is True
    assert controller._gui._ui.actionRedo.isEnabled() is True
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n')
    assert stream.getvalue() == res

    controller.execute_command(shape_commands[0])
    assert controller._command_engine._undos == [
        shape_commands[0], shape_commands[1], shape_commands[0]
    ]
    assert controller._command_engine._redos == []
    assert controller._shapes._shapes == [
        shapes['dot'], shapes['line'], shapes['dot']
    ]
    assert controller._gui._ui.actionUndo.isEnabled() is True
    assert controller._gui._ui.actionRedo.isEnabled() is False
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[0]}\n{shapes["dot"]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["dot"]}\n')
    assert stream.getvalue() == res
Esempio n. 4
0
def test_remove_shapes(controller: Controller, shape_commands, commands,
                       stream: io.StringIO, shapes: Dict[str, Shape]):
    for command in shape_commands:
        controller.execute_command(command)
    controller.execute_command(shape_commands[2])

    controller.execute_command(commands[0])
    assert controller._shapes._shapes == [
        shapes['dot'], shapes['line'], shapes['rectangle'], shapes['circle']
    ]
    assert controller._command_engine._undos == [
        *shape_commands, shape_commands[2], commands[0]
    ]
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {shape_commands[3]}\n{shapes["rectangle"]}\n'
        f' > {shape_commands[4]}\n{shapes["circle"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {commands[0]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
    )
    assert stream.getvalue() == res

    controller.execute_command(commands[1])
    assert controller._shapes._shapes == [
        shapes['dot'], shapes['line'], shapes['rectangle'], shapes['circle']
    ]
    assert controller._command_engine._undos == [
        *shape_commands, shape_commands[2], commands[0]
    ]
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {shape_commands[3]}\n{shapes["rectangle"]}\n'
        f' > {shape_commands[4]}\n{shapes["circle"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {commands[0]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
    )
    assert stream.getvalue() == res

    controller.undo()
    assert controller._shapes._shapes == [*shapes.values(), shapes['polyline']]
    assert controller._gui._ui.history.toPlainText() == (
        f' > {shape_commands[0]}\n{shapes["dot"]}\n'
        f' > {shape_commands[1]}\n{shapes["line"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}\n'
        f' > {shape_commands[3]}\n{shapes["rectangle"]}\n'
        f' > {shape_commands[4]}\n{shapes["circle"]}\n'
        f' > {shape_commands[2]}\n{shapes["polyline"]}')
    res = (
        f'{shapes["dot"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{shapes["polyline"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n'
        f'{shapes["dot"]}\n{shapes["line"]}\n{shapes["polyline"]}\n{shapes["rectangle"]}\n{shapes["circle"]}\n{shapes["polyline"]}\n'
    )
    assert stream.getvalue() == res