def test_circle(shapes: Dict[str, Shape]): circle: Circle = shapes['circle'] assert circle.start == Point(12345, 54321) assert circle.radius == 999 assert circle.color == Color(123, 255, 0) assert circle.get_props() == (12345, 54321, 999) d = PrinterMockup() circle.print_to(d) assert d.result == 'Drawed a ' + str(circle) assert str(circle) == 'Circle centered at [12345, 54321] with radius 999 with Color(123, 255, 0, alpha=255)' assert circle == Circle(Point(12345, 54321), 999, Color(123, 255, 0)) assert circle != Circle(Point(12345, 54321), 999, Color(123, 255, 1)) assert circle.contains(Point(12345, 54321)) is True assert circle.contains(Point(13344, 54321)) is True assert circle.contains(Point(12345, 53322)) is True assert circle.contains(Point(12344, 53322)) is False assert circle.contains(Point(13344, 54322)) is False # Vertical move new_circle = circle.move(Point(13344, 54321), Point(13344, 0)) assert circle.start == Point(12345, 54321) assert new_circle == Circle(Point(12345, 0), circle.radius, circle.color) # Horizontal move new_circle = circle.move(Point(12345, 53322), Point(0, 53322)) assert circle.start == Point(12345, 54321) assert new_circle == Circle(Point(0, 54321), circle.radius, circle.color) # Diagonal move new_circle = circle.move(Point(13344, 54321), Point(0, 0)) assert circle.start == Point(12345, 54321) assert new_circle == Circle(Point(-999, 0), circle.radius, circle.color)
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_move_shapes(shapes_store: ShapesStore, shapes: Dict[str, Shape]): shapes_store.add_shapes(*shapes.values()) res = shapes_store.move_shapes(Point(10, 10), Point(-100, 100)) assert res['moved'] == [ Polyline(Point(-100, 100), Point(-90, 110), Point(-80, 100), color=Color(48, 210, 111)) ] assert res['before_move'] == [*shapes.values()] assert len(shapes_store._controller.result) == 3 shapes_store.add_shapes( Rectangle(Point(-10, 3490), 20, 20, Color(255, 255, 255))) # Getting the previously moved polyline back shapes_store.move_shapes(Point(-100, 100), Point(10, 10)) res = shapes_store.move_shapes(Point(0, 3500), Point(-1000, -1000)) assert res['moved'] == [ Rectangle(Point(-1000, -4500), 1, 50000, Color(255, 255, 255)), Rectangle(Point(-1010, -1010), 20, 20, Color(255, 255, 255)) ] assert res['before_move'] == [ shapes['dot'], shapes['line'], shapes['rectangle'], shapes['circle'], Rectangle(Point(-10, 3490), 20, 20, Color(255, 255, 255)), shapes['polyline'] ] assert len(shapes_store._controller.result) == 8
def test_polyline(shapes: Dict[str, Shape]): with pytest.raises(ValueError): Polyline(Point(10, 10), color=Color(0, 0, 0)) polyline: Polyline = shapes['polyline'] assert polyline.start == Point(10, 10) assert polyline.color == Color(48, 210, 111) assert polyline.get_props() == polyline.points == (Point(10, 10), Point(20, 20), Point(30, 10)) d = PrinterMockup() polyline.print_to(d) assert d.result == 'Drawed a ' + str(polyline) assert str(polyline) == 'Polyline with points at ([10, 10], [20, 20], [30, 10]) with Color(48, 210, 111, alpha=255)' assert polyline == Polyline(Point(10, 10), Point(20, 20), Point(30, 10), color=Color(48, 210, 111)) assert polyline != Polyline(Point(10, 10), Point(20, 20), color=Color(48, 210, 111)) assert polyline.contains(Point(10, 10)) is True assert polyline.contains(Point(15, 15)) is True assert polyline.contains(Point(25, 15)) is True assert polyline.contains(Point(24.5, 15), divergence=True) is True assert polyline.contains(Point(15, 16)) is False assert polyline.contains(Point(24, 15)) is False assert polyline.contains(Point(24, 15), divergence=True) is False # Vertical move new_polyline = polyline.move(Point(20, 20), Point(20, 10)) assert polyline.start == Point(10, 10) assert polyline.points[0] == Point(10, 10) assert polyline.points[1] == Point(20, 20) assert polyline.points[2] == Point(30, 10) assert new_polyline == Polyline(Point(10, 0), Point(20, 10), Point(30, 0), color=polyline.color) # Horizontal move new_polyline = polyline.move(Point(28, 12), Point(40, 12)) assert polyline.start == Point(10, 10) assert polyline.points[0] == Point(10, 10) assert polyline.points[1] == Point(20, 20) assert polyline.points[2] == Point(30, 10) assert new_polyline == Polyline(Point(22, 10), Point(32, 20), Point(42, 10), color=polyline.color) # Diagonal move new_polyline = polyline.move(Point(15, 15), Point(10, 0)) assert polyline.start == Point(10, 10) assert polyline.points[0] == Point(10, 10) assert polyline.points[1] == Point(20, 20) assert polyline.points[2] == Point(30, 10) assert new_polyline == Polyline(Point(5, -5), Point(15, 5), Point(25, -5), color=polyline.color)
def test_shape_brush(controller: ControllerMockup, brush_class: Type[ShapeBrush], shape_command_class: Type[ShapeCommand], shape_name: str): b1 = brush_class() b2 = brush_class() assert b1 == b2 assert b1._shape_command_class == b2._shape_command_class == shape_command_class assert str(b1) == str(b2) == shape_name shape_command = shape_command_class(receiver=controller, start_x=-999, start_y=0, end_x=-999, end_y=100, color=(0, 0, 0)) preview_shape = copy.deepcopy(shape_command.shape) preview_shape.color = Color(0, 0, 0, 200) b1.mouse_move(controller, 10, 20, None) assert controller.command is None assert controller.preview is None b1.mouse_press(controller, -999, 0, Qt.LeftButton) b1.mouse_move(controller, -999, 100, None) assert controller.command is None assert controller.preview == preview_shape assert b1._start == (-999, 0) b1.mouse_press(controller, -999, 100, Qt.LeftButton) assert controller.command == shape_command assert controller.preview is None assert b1._start is None
def get_shape(start_x: int, start_y: int, color: Tuple[int, int, int], **kwargs) -> Circle: radius = kwargs['radius'] center = (start_x, start_y) circle = Circle(Point(*center), int(radius), Color(*color)) return circle
def test_circle_command_with_points(receiver: ReceiverMockup): command = PrintCircleCommand(receiver=receiver, start_x=0, start_y=0, color=(0, 0, 0), circle_factory=PointsCircleFactory, end_x=100, end_y=100) assert str(command) == 'circle 0,0 141 rgb(0,0,0)' assert (command == PrintCircleCommand(receiver=receiver, start_x=0, start_y=0, color=(0, 0, 0), circle_factory=PointsCircleFactory, end_x=100, end_y=100)) command.execute() assert receiver.received == Circle(center=Point(0, 0), radius=141, color=Color(0, 0, 0)) command.reverse() assert receiver.received is None assert receiver.deleted_lines == 2
def test_rect_command_with_dimensions(receiver: ReceiverMockup): command = PrintRectCommand(receiver=receiver, start_x=50, start_y=50, color=(255, 255, 255), rect_factory=DimensionsRectFactory, width=50, height=50) assert str(command) == 'rect 50,50 50 50 rgb(255,255,255)' assert (command == PrintRectCommand(receiver=receiver, start_x=50, start_y=50, color=(255, 255, 255), rect_factory=DimensionsRectFactory, width=50, height=50)) command.execute() assert receiver.received == Rectangle(top_left=Point(50, 50), width=50, height=50, color=Color(255, 255, 255)) command.reverse() assert receiver.received is None assert receiver.deleted_lines == 2
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 get_shape(start_x: int, start_y: int, color: Tuple[int, int, int], **kwargs) -> Rectangle: width = kwargs['width'] height = kwargs['height'] rectangle = Rectangle(Point(start_x, start_y), width, height, Color(*color)) return rectangle
def get_shape(start_x: int, start_y: int, color: Tuple[int, int, int], **kwargs) -> Circle: end_x = kwargs['end_x'] end_y = kwargs['end_y'] center = (start_x, start_y) radius = distance(Point(*center), Point(end_x, end_y)) circle = Circle(Point(*center), int(radius), Color(*color)) return 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 get_shape(start_x: int, start_y: int, color: Tuple[int, int, int], **kwargs) -> Rectangle: end_x = kwargs['end_x'] end_y = kwargs['end_y'] width = abs(start_x - end_x) height = abs(start_y - end_y) rectangle = Rectangle(Point(min(start_x, end_x), min(start_y, end_y)), width, height, Color(*color)) return rectangle
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_color(): c1 = Color(255, 123, 0) c2 = Color(255, 123, 0, 255) c3 = Color(123, 255, 0) assert c1.r == 255 assert c1.g == 123 assert c1.b == 0 assert c1.alpha == 255 def unpacking(r, g, b, alpha): assert r == 255 assert g == 123 assert b == 0 assert alpha == 255 unpacking(*c1) unpacking(*c2) assert c1 == c2 assert c1 != c3 assert c2 != c3 assert str(c1) == str(c2) == 'Color(255, 123, 0, alpha=255)' assert str(c3) == 'Color(123, 255, 0, alpha=255)' with pytest.raises(ValueError): c4 = Color(255, 255, 256) with pytest.raises(ValueError): c5 = Color(0, -1, 0) with pytest.raises(ValueError): c6 = Color(255, 255, 255, 256)
def test_abstract_shape(): abstract_shape = Shape(Point(100, 100), Color(0, 1, 2)) assert abstract_shape.start == Point(100, 100) assert abstract_shape.color == Color(0, 1, 2) with pytest.raises(NotImplementedError): abstract_shape.print_to(None) with pytest.raises(NotImplementedError): abstract_shape.get_props() with pytest.raises(NotImplementedError): abstract_shape.contains(Point(1, 1)) assert str(abstract_shape) == ' with Color(0, 1, 2, alpha=255)' assert abstract_shape == Shape(Point(100, 100), Color(0, 1, 2)) assert abstract_shape != Shape(Point(100, 101), Color(0, 1, 2)) new_shape = abstract_shape.move(Point(100, 100), Point(-32, 123)) assert abstract_shape.start == Point(100, 100) assert new_shape == Shape(Point(-32, 123), abstract_shape.color)
def test_dot(shapes: Dict[str, Shape]): dot = shapes['dot'] assert dot.start == Point(10, 200000000) assert dot.color == Color(1, 2, 3) assert dot.get_props() == (10, 200000000) d = PrinterMockup() dot.print_to(d) assert d.result == 'Drawed a ' + str(dot) assert str(dot) == 'Dot at [10, 200000000] with Color(1, 2, 3, alpha=255)' assert dot == Dot(Point(10, 200000000), Color(1, 2, 3)) assert dot != Dot(Point(10, 200000000), Color(1, 2, 4)) assert dot.contains(Point(10, 200000000)) is True assert dot.contains(Point(11, 200000000), divergence=True) is True assert dot.contains(Point(11, 200000000)) is False assert dot.contains(Point(13, 200000000), divergence=True) is False new_dot = dot.move(Point(10, 200000000), Point(0, 0)) assert dot.start == Point(10, 200000000) assert new_dot == Dot(Point(0, 0), dot.color)
def test_dot_command(receiver: ReceiverMockup): command = PrintDotCommand(receiver=receiver, x=0, y=-12, color=(1, 2, 3)) assert str(command) == 'dot 0,-12 rgb(1,2,3)' assert command == PrintDotCommand(receiver=receiver, x=0, y=-12, color=(1, 2, 3)) command.execute() assert receiver.received == Dot(start=Point(0, -12), color=Color(1, 2, 3)) command.reverse() assert receiver.received is None assert receiver.deleted_lines == 2
def parse_color(self, cli_input: str, color_parser: ColorParser) -> ParseResult: """ Parse a Color from given input. """ default_color = Color(0, 0, 0) if cli_input == '': return Success(default_color, '') color_result = color_parser.parse_color(cli_input) if color_result.is_successful(): return Success(color_result.get_match(), '') return Failure(color_result.get_expected(), cli_input)
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 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_rectangle(shapes: Dict[str, Shape]): rect: Rectangle = shapes['rectangle'] assert rect.start == Point(0, 0) assert rect.width == 1 assert rect.height == 50000 assert rect.color == Color(255, 255, 255) assert rect.get_props() == (0, 0, 1, 50000) d = PrinterMockup() rect.print_to(d) assert d.result == 'Drawed a ' + str(rect) assert str(rect) == '1x50000 rectangle with top-left corner at [0, 0] with Color(255, 255, 255, alpha=255)' assert rect == Rectangle(Point(0, 0), 1, 50000, Color(255, 255, 255)) assert rect != Rectangle(Point(0, 0), -1, 50000, Color(255, 255, 255)) assert rect.contains(Point(0, 0)) is True assert rect.contains(Point(1, 0)) is True assert rect.contains(Point(1, 50000)) is True assert rect.contains(Point(2, 0)) is False assert rect.contains(Point(0, 50001)) is False # Vertical move new_rect = rect.move(Point(1, 3500), Point(1, 0)) assert rect.start == Point(0, 0) assert new_rect == Rectangle(Point(0, -3500), rect.width, rect.height, rect.color) # Horizontal move new_rect = rect.move(Point(0, 20), Point(20, 20)) assert rect.start == Point(0, 0) assert new_rect == Rectangle(Point(20, 0), rect.width, rect.height, rect.color) # Diagonal move new_rect = rect.move(Point(1, 100), Point(20, 50)) assert rect.start == Point(0, 0) assert new_rect == Rectangle(Point(19, -50), rect.width, rect.height, rect.color)
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_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 test_polyline_command(receiver: ReceiverMockup): command = PrintPolylineCommand(receiver=receiver, points=[(10, 10), (20, 20), (30, 30), (40, 20), (50, 10)], color=(100, 200, 255)) assert str( command) == 'line 10,10 20,20 30,30 40,20 50,10 rgb(100,200,255)' assert (command == PrintPolylineCommand(receiver=receiver, points=[(10, 10), (20, 20), (30, 30), (40, 20), (50, 10)], color=(100, 200, 255))) command.execute() assert receiver.received == Polyline(Point(10, 10), Point(20, 20), Point(30, 30), Point(40, 20), Point(50, 10), color=Color(100, 200, 255)) command.reverse() assert receiver.received is None assert receiver.deleted_lines == 2
def test_dimensions_rect_factory(): rectangle = DimensionsRectFactory.get_shape(0, 0, (0, 20, 40), width=10, height=20) assert rectangle == Rectangle(Point(0, 0), 10, 20, Color(0, 20, 40))
def test_points_rect_factory(): rectangle = PointsRectFactory.get_shape(0, 0, (0, 20, 40), end_x=10, end_y=20) assert rectangle == Rectangle(Point(0, 0), 10, 20, Color(0, 20, 40))
def test_dimensions_circle_factory(): circle = DimensionsCircleFactory.get_shape(0, 0, (0, 20, 40), radius=141) assert circle == Circle(Point(0, 0), 141, Color(0, 20, 40))
def test_points_circle_factory(): circle = PointsCircleFactory.get_shape(0, 0, (0, 20, 40), end_x=100, end_y=100) assert circle == Circle(Point(0, 0), 141, Color(0, 20, 40))