def test_list_shape_command(receiver: ReceiverMockup): command = ListShapeCommand(receiver) assert str(command) == 'ls' assert command == ListShapeCommand(receiver) command.execute() assert receiver.listed_shapes == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert command.listed == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] command.reverse() assert receiver.deleted_lines == len(command.listed) + 1 command = ListShapeCommand(receiver=receiver, x=123, y=321) assert str(command) == 'ls 123,321' assert command == ListShapeCommand(receiver=receiver, x=123, y=321) command.execute() assert receiver.listed_shapes == [ Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert command.listed == [ Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] command.reverse() assert receiver.deleted_lines == len(command.listed) + 1
def test_remove_shape_command(receiver: ReceiverMockup): command = RemoveShapeCommand(receiver=receiver, x=123, y=321) assert str(command) == 'remove 123,321' assert command == RemoveShapeCommand(receiver=receiver, x=123, y=321) command.execute() assert receiver.received == Point(123, 321) assert command._before_remove == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert receiver.removed == [ Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert receiver.deleted_lines == 0 command.reverse() assert receiver.received == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert receiver.deleted_lines == 1 command = RemoveShapeCommand(receiver=receiver, x=-1, y=-1) command.execute() assert receiver.removed == [] assert receiver.deleted_lines == 1 assert receiver.last_command_removed is True
def test_remove_shapes_at(shapes_store: ShapesStore): r1 = Rectangle(Point(0, 0), 100, 100, QColor(0, 0, 0)) r2 = Rectangle(Point(1, 1), 100, 100, QColor(0, 0, 0)) c = Circle(Point(0, 0), 50, QColor(0, 0, 0)) l1 = Line(Point(-10, 0), Point(10, 0), QColor(0, 0, 0)) l2 = Line(Point(-10, 1), Point(10, 1), QColor(0, 0, 0)) d = Dot(Point(0, 0), QColor(0, 0, 0)) shapes_store.add_shapes(r1, r2, c, l1, l2, d) res = shapes_store.remove_shapes_at(Point(0, 0)) assert res['before_remove'] == [r1, r2, c, l1, l2, d] assert res['removed'] == [r1, c, l1, d] assert shapes_store._shapes == [r2, l2] assert len(shapes_store._controller.result) == 2
def test_move_shape_command(receiver: ReceiverMockup): command = MoveShapeCommand(receiver=receiver, start_x=0, start_y=0, end_x=10, end_y=10) assert str(command) == 'move 0,0 10,10' assert command == MoveShapeCommand(receiver=receiver, start_x=0, start_y=0, end_x=10, end_y=10) command.execute() assert receiver.received == (Point(0, 0), Point(10, 10)) assert command._before_move == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert receiver.moved == [] assert receiver.deleted_lines == 1 assert receiver.last_command_removed is True command = MoveShapeCommand(receiver=receiver, start_x=10, start_y=10, end_x=0, end_y=0) command.execute() assert receiver.received == (Point(10, 10), Point(0, 0)) assert command._before_move == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert receiver.moved == [ Dot(start=Point(0, 0), color=Color(0, 0, 0)), Line(start=Point(113, 311), end=Point(0, 0), color=Color(0, 0, 0)) ] command.reverse() assert receiver.received == [ Dot(start=Point(10, 10), color=Color(0, 0, 0)), Line(start=Point(123, 321), end=Point(10, 10), color=Color(0, 0, 0)) ] assert receiver.deleted_lines == 1
def test_shape_class_diff(): abstract_shape = Shape(Point(100, 100), Color(100, 100, 100)) dot = Dot(Point(100, 100), Color(100, 100, 100)) line = Line(Point(100, 100), Point(100, 100), Color(100, 100, 100)) polyline = Polyline(Point(100, 100), Point(100, 100), color=Color(100, 100, 100)) rect = Rectangle(Point(100, 100), 100, 100, Color(100, 100, 100)) circle = Circle(Point(100, 100), 100, Color(100, 100, 100)) assert abstract_shape != dot != line != polyline != rect != circle
def test_clear_command(receiver: ReceiverMockup): command = ClearCommand(receiver) assert str(command) == 'clear' assert command == ClearCommand(receiver) command.execute() assert receiver.restarted is True assert command.shapes == [ Line(Point(10, 10), Point(20, 20), Color(1, 2, 3)), Dot(Point(10, 10), Color(1, 2, 3)) ] command.reverse() assert receiver.deleted_lines == 1 assert receiver.received == [ Line(Point(10, 10), Point(20, 20), Color(1, 2, 3)), Dot(Point(10, 10), Color(1, 2, 3)) ]
def test_line(shapes: Dict[str, Shape]): line: Line = shapes['line'] assert line.start == Point(1000, -1000) assert line.end == Point(0, -1000) assert line.color == Color(0, 0, 0) assert line.get_props() == (1000, -1000, 0, -1000) d = PrinterMockup() line.print_to(d) assert d.result == 'Drawed a ' + str(line) assert str(line) == 'Line from [1000, -1000] to [0, -1000] with Color(0, 0, 0, alpha=255)' assert line == Line(Point(1000, -1000), Point(0, -1000), Color(0, 0, 0)) assert line != Line(Point(1000, -1000), Point(0, 1000), Color(0, 0, 0)) assert line.contains(Point(500, -1000)) is True assert line.contains(Point(502, -1000), divergence=True) is True assert line.contains(Point(-1, -1000)) is False assert line.contains(Point(-3, -1000), divergence=False) is False # Vertical move new_line = line.move(Point(500, -1000), Point(500, 0)) assert line.start == Point(1000, -1000) assert line.end == Point(0, -1000) assert new_line == Line(Point(1000, 0), Point(0, 0), line.color) # Horizontal move new_line = line.move(Point(1, -1000), Point(-999, -1000)) assert line.start == Point(1000, -1000) assert line.end == Point(0, -1000) assert new_line == Line(Point(0, -1000), Point(-1000, -1000), line.color) # Diagonal move new_line = line.move(Point(350, -1000), Point(500, -1200)) assert line.start == Point(1000, -1000) assert line.end == Point(0, -1000) assert new_line == Line(Point(1150, -1200), Point(150, -1200), line.color)
def __init__(self): self.shapes = [ Dot(Point(10, 10), Color(0, 0, 0)), Line(Point(123, 321), Point(10, 10), Color(0, 0, 0)) ] self.received = None self.moved = [] self.removed = [] self.listed_shapes = None self.last_command_removed = None self.printed = '' self.deleted_lines = 0 self.restarted = False self.saved = None self.loaded = None self.quited = False
def shapes() -> Dict[str, Shape]: return { 'dot': Dot(Point(10, 200000000), Color(1, 2, 3)), 'line': Line(Point(1000, -1000), Point(0, -1000), Color(0, 0, 0)), 'polyline': Polyline(Point(10, 10), Point(20, 20), Point(30, 10), color=Color(48, 210, 111)), 'rectangle': Rectangle(Point(0, 0), 1, 50000, Color(255, 255, 255)), 'circle': Circle(Point(12345, 54321), 999, Color(123, 255, 0)) }
def test_line_command(receiver: ReceiverMockup): command = PrintLineCommand(receiver=receiver, start_x=10, start_y=10, end_x=20, end_y=20, color=(100, 200, 100)) assert str(command) == 'line 10,10 20,20 rgb(100,200,100)' assert (command == PrintLineCommand(receiver=receiver, start_x=10, start_y=10, end_x=20, end_y=20, color=(100, 200, 100))) command.execute() assert receiver.received == Line(start=Point(10, 10), end=Point(20, 20), color=Color(100, 200, 100)) command.reverse() assert receiver.received is None assert receiver.deleted_lines == 2
def shapes_at(self, point: Point, divergence: bool = False) -> List[Shape]: shapes = [ Line(Point(0, 0), Point(0, 10), Color(10, 20, 30)), Rectangle(Point(0, 5), 10, 10, Color(0, 0, 0)) ] return [shape for shape in shapes if shape.contains(point)]
def __init__(self, receiver, start_x: int, start_y: int, end_x: int, end_y: int, color: tuple): super().__init__(receiver) self.shape = Line(Point(start_x, start_y), Point(end_x, end_y), Color(*color))
def print_line(self, line: Line): painter = self._prepare_painter(line.color) painter.drawLine(*line.get_props())
def shapes_at(self, point: Point, divergence: bool = False) -> List[Shape]: return [ Line(Point(0, 0), Point(0, 10), Color(10, 20, 30)), Rectangle(Point(0, 5), 10, 10, Color(0, 0, 0)) ]
def shapes_at(self, point: Point = None) -> List[Shape]: return [ Line(Point(10, 10), Point(20, 20), Color(1, 2, 3)), Dot(Point(10, 10), Color(1, 2, 3)) ]