def parse_params(self, cli_input: str) -> ParseResult: points_result = self.parse_two_points(cli_input) if points_result.is_successful(): abs_points = self.convert_points(points_result.get_match()) # Parse color color_result = self.parse_color(points_result.get_remainder(), self.color_parser) if color_result.is_successful(): start_x = abs_points[0].x start_y = abs_points[0].y end_x = abs_points[1].x end_y = abs_points[1].y return Success( PrintRectCommand( self._controller, start_x, start_y, color_result.get_match(), end_x=end_x, end_y=end_y, ), '') else: return Failure(color_result.get_expected(), points_result.get_remainder()) point_and_nats_result = self.parse_point_and_nats(cli_input) if point_and_nats_result.is_successful(): width = point_and_nats_result.get_match()[1] height = point_and_nats_result.get_match()[2] abs_points = self.convert_points( [point_and_nats_result.get_match()[0]]) # Parse color color_result = self.parse_color( point_and_nats_result.get_remainder(), self.color_parser) if color_result.is_successful(): start_x = abs_points[0].x start_y = abs_points[0].y return Success( PrintRectCommand(self._controller, start_x, start_y, color_result.get_match(), DimensionsRectFactory, width=width, height=height), '') else: return Failure(color_result.get_expected(), point_and_nats_result.get_remainder()) return Failure( points_result.get_expected() + ' | ' + point_and_nats_result.get_expected(), cli_input)
def shape_commands( controller: Controller ) -> Tuple[Command, Command, Command, Command, Command]: c1 = PrintDotCommand(receiver=controller, x=10, y=200000000, color=(1, 2, 3)) c2 = PrintLineCommand(receiver=controller, start_x=1000, start_y=-1000, end_x=0, end_y=-1000, color=(0, 0, 0)) c3 = PrintPolylineCommand(receiver=controller, points=[(10, 10), (20, 20), (30, 10)], color=(48, 210, 111)) c4 = PrintRectCommand(receiver=controller, start_x=0, start_y=0, end_x=1, end_y=50000, color=(255, 255, 255)) c5 = PrintCircleCommand(receiver=controller, start_x=12345, start_y=54321, end_x=13344, end_y=54321, color=(123, 255, 0)) return c1, c2, c3, c4, c5
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 test_not_equals(receiver: ReceiverMockup): assert ( PrintDotCommand(receiver, 0, 0, (0, 0, 0)) != PrintLineCommand( receiver, 0, 0, 0, 0, (0, 0, 0)) != PrintRectCommand( receiver, 0, 0, (0, 0, 0), PointsRectFactory, end_x=0, end_y=0) != PrintCircleCommand( receiver, 0, 0, (0, 0, 0), PointsCircleFactory, end_x=0, end_y=0)) assert (PrintDotCommand(receiver, 0, 0, (0, 0, 0)) != PrintLineCommand( receiver, 0, 0, 0, 0, (0, 0, 0) ) != PrintRectCommand( receiver, 0, 0, (0, 0, 0), DimensionsRectFactory, width=0, height=0) != PrintCircleCommand( receiver, 0, 0, (0, 0, 0), DimensionsCircleFactory, radius=0)) assert (PrintDotCommand(receiver, 0, 0, (0, 0, 0)) != PrintDotCommand( receiver, 1, 0, (0, 0, 0))) assert (PrintLineCommand(receiver, 0, 0, 0, 0, (0, 0, 0)) != PrintLineCommand( receiver, 0, 0, 1, 0, (0, 0, 0))) assert (PrintRectCommand( receiver, 0, 0, (0, 0, 0), PointsRectFactory, end_x=0, end_y=0) != PrintRectCommand( receiver, 0, 0, (0, 0, 0), PointsRectFactory, end_x=0, end_y=1)) assert (PrintRectCommand( receiver, 0, 0, (0, 0, 0), DimensionsRectFactory, width=0, height=0 ) != PrintRectCommand( receiver, 0, 1, (0, 0, 0), DimensionsRectFactory, width=0, height=0)) assert (PrintCircleCommand( receiver, 0, 0, (0, 0, 0), PointsCircleFactory, end_x=0, end_y=0) != PrintCircleCommand( receiver, 0, 1, (0, 0, 0), PointsCircleFactory, end_x=0, end_y=0)) assert (PrintCircleCommand( receiver, 0, 0, (0, 0, 0), DimensionsCircleFactory, radius=0) != PrintCircleCommand( receiver, 0, 1, (0, 0, 0), DimensionsCircleFactory, radius=0))
def test_mouse_press_event(canvas: Canvas): assert canvas.brush == MoveShapeBrush() assert canvas.cursor() == Qt.ArrowCursor canvas.mousePressEvent(EventMockup) assert canvas._controller.command is None canvas.set_brush(RectShapeBrush()) canvas.mousePressEvent(EventMockup) canvas.mousePressEvent(EventMockup) assert (canvas._controller.command == PrintRectCommand( receiver=canvas._controller, start_x=EventMockup.x(), start_y=EventMockup.y(), color=(255, 255, 255), rect_factory=PointsRectFactory, end_x=EventMockup.x(), end_y=EventMockup.y()))
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
def test_rect_parser(controller: Controller, cli_parser: CliParser): """ :return: """ # Test invalid inputs, two points as parameters invalid_inputs = [ "rect 10,-20 30,40", "rect 10,20 30,+40", "rect 10,20 30.40", "rect 10 20 30,40", "rect10,20 30,40", "rect 10,20", "rectt 10,20 30,40", "rect something", "rect 10,20 30,40 rgb(0,0,-1)", "rect 10,20 30,40 rgb(0,0,0", "rect 10,20 30,40 rgb 0,0,0", "rect 10,20 30,40 rgb(0,1)", "rect 10,20 30,40rgb(0,1,2)", "rect 10,20 30,40 rgb(1a,2,3)", "rect 10,20 30,40,rgb(0,2,3)", "rect 10,20 30,40 rgb(256,0,1)", "rect 10,20 30,40 rgb(2.0.1)", "rect 10,20 30,40 rgb(123)" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs, two points as parameters valid_inputs = [("rect 10,20 30,40", PrintRectCommand(controller, 10, 20, (0, 0, 0), end_x=30, end_y=40)), ("rect 10 , 20 -10 ,-20", PrintRectCommand(controller, 10, 20, (0, 0, 0), end_x=0, end_y=0)), (" rect -5 ,-5 +5,+5 ", PrintRectCommand(controller, -5, -5, (0, 0, 0), end_x=0, end_y=0)), ("rect -5,-5 10, 20", PrintRectCommand(controller, -5, -5, (0, 0, 0), end_x=10, end_y=20)), ("rect 10,20 30,40 rgb (10,20,30)", PrintRectCommand(controller, 10, 20, (10, 20, 30), end_x=30, end_y=40)), (" rect 10,20 -10 , -20 rgb(0 , 0 ,0 ) ", PrintRectCommand(controller, 10, 20, (0, 0, 0), end_x=0, end_y=0))] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected # Test invalid inputs, one point and two natural numbers (width and height) as parameters invalid_inputs = [ "rect 10,-20 30 40", "rect 10,20 30 +40", "rect 10,20 -30 40", "rect 10 20 30 40", "rect10,20 30 40", "rect 10,20", "rect 10,20 30", "rectt 10,20 30 40", "rect 10,20 something", "rect 10,20 30 something", "rect 10,20 30 40 rgb(0,0,-1)", "rect 10,20 30 40 rgb(0,0,0", "rect 10,20 30 40 rgb 0,0,0", "rect 10,20 30 40 rgb(0,1)", "rect 10,20 30 40rgb(0,1,2)", "rect 10,20 30 40 rgb(1a,2,3)", "rect 10,20 30 40,rgb(0,2,3)", "rect 10,20 30 40 rgb(256,0,1)", "rect 10,20 30 40 rgb(2.0.1)", "rect 10,20 30 40 rgb(123)" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs, one point and two natural numbers (width and height) as parameters valid_inputs = [ ("rect 10 ,20 20 20", PrintRectCommand(controller, 10, 20, (0, 0, 0), DimensionsRectFactory, width=20, height=20)), ("rect -10, -20 10 20", PrintRectCommand(controller, -10, -20, (0, 0, 0), DimensionsRectFactory, width=10, height=20)), (" rect +10, +20 0 5 ", PrintRectCommand(controller, 10, 20, (0, 0, 0), DimensionsRectFactory, width=0, height=5)), ("rect +10, -20 20 20 rgb( 10,20,30 )", PrintRectCommand(controller, 10, -20, (10, 20, 30), DimensionsRectFactory, width=20, height=20)), ("rect -10 , +20 1000 2 rgb ( 0 , 0 ,0 )", PrintRectCommand(controller, -10, 20, (0, 0, 0), DimensionsRectFactory, width=1000, height=2)), ] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected