def test_clear_parser(controller: Controller, cli_parser: CliParser): # Test valid inputs assert cli_parser.parse_input("clear") == ClearCommand(controller) assert cli_parser.parse_input(" clear") == ClearCommand(controller) assert cli_parser.parse_input(" clear ") == ClearCommand(controller) # Test invalid inputs assert cli_parser.parse_input("clear something") == InvalidCommand( controller) assert cli_parser.parse_input("clearsomething") == InvalidCommand( controller)
def test_quit_parser(controller: Controller, cli_parser: CliParser): # Test valid inputs assert cli_parser.parse_input("quit") == QuitCommand(controller) assert cli_parser.parse_input(" quit") == QuitCommand(controller) assert cli_parser.parse_input(" quit ") == QuitCommand(controller) # Test invalid inputs assert cli_parser.parse_input("quit something") == InvalidCommand( controller) assert cli_parser.parse_input("quitsomething") == InvalidCommand( controller)
def test_move_shape_parser(controller: Controller, cli_parser: CliParser): # Test invalid inputs, two points as parameters invalid_inputs = [ "move 10,-20 30,40", "move 10,20 30,+40", "move 10,20 30.40", "move 10 20 30,40", "move10,20 30,40", "move 10,20", "movee 10,20 30,40", "move something", ] 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 = [ ("move 10,20 30,40", MoveShapeCommand(controller, 10, 20, 30, 40)), ("move 10,20 -10,-20", MoveShapeCommand(controller, 10, 20, 0, 0)), ("move 10 ,20 30, 40", MoveShapeCommand(controller, 10, 20, 30, 40)), (" move 10, 20 30 ,40 ", MoveShapeCommand(controller, 10, 20, 30, 40)), ("move 10 , 20 30 , 40", MoveShapeCommand(controller, 10, 20, 30, 40)), ("move -5,-5 +5,+5", MoveShapeCommand(controller, -5, -5, 0, 0)), ("move -5,-5 10,20", MoveShapeCommand(controller, -5, -5, 10, 20)) ] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected
def test_dot_parser(controller: Controller, cli_parser: CliParser): """ :return: """ # Test invalid inputs invalid_inputs = [ "dot 10,-20", "dot +10,20", "dot 10.20", "dot 10 20", "dot10,20", "dot 10,20 30,40", "dott 10,20", "dot something", "dot 10,20 rgb(0,0,-1)", "dot 10,20 30,40 rgb(0,0,0", "dot 10,20 rgb 0,0,0", "dot 10,20 rgb(0,1)", "dot 10,20rgb(0,1,2)", "dot 10,20 rgb(1a,2,3)", "dot 10,20,rgb(0,2,3)", "dot 10,20 rgb(256,0,1)", "dot 10,20 rgb(2.0.1)", "dot 10,20 rgb(123)" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs valid_inputs = [ ("dot 10, 20", PrintDotCommand(controller, 10, 20, (0, 0, 0))), ("dot -10 ,+20", PrintDotCommand(controller, -10, +20, (0, 0, 0))), (" dot -5,-5 ", PrintDotCommand(controller, -5, -5, (0, 0, 0))), ("dot 10,20 rgb (10,20,30) ", PrintDotCommand(controller, 10, 20, (10, 20, 30))), ("dot -10 , -20 rgb ( 0 , 0, 0 ) ", PrintDotCommand(controller, -10, -20, (0, 0, 0))) ] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected
def test_invalid_command(receiver: ReceiverMockup): command = InvalidCommand(receiver) assert str(command) == 'invalid command' assert command == InvalidCommand(receiver) command.execute() assert receiver.printed == 'Invalid command!' command.reverse() assert receiver.deleted_lines == 2
def test_load_parser(controller: Controller, cli_parser: CliParser): # Test valid inputs assert cli_parser.parse_input("load") == LoadCommand(controller) assert cli_parser.parse_input(" load") == LoadCommand(controller) assert cli_parser.parse_input(" load ") == LoadCommand(controller) assert cli_parser.parse_input("load filename") == LoadCommand( controller, "filename") assert cli_parser.parse_input(" load filename something") == LoadCommand( controller, "filename something") # Test invalid inputs assert cli_parser.parse_input("loadsomething") == InvalidCommand( controller)
def test_save_parser(controller: Controller, cli_parser: CliParser): # Test valid inputs assert cli_parser.parse_input("save") == SaveCommand(controller) assert cli_parser.parse_input(" save") == SaveCommand(controller) assert cli_parser.parse_input(" save ") == SaveCommand(controller) assert cli_parser.parse_input("save filename") == SaveCommand( controller, "filename") assert cli_parser.parse_input(" save filename something") == SaveCommand( controller, "filename something") # Test invalid inputs assert cli_parser.parse_input("savesomething") == InvalidCommand( controller)
def parse_params(self, command_parser: CommandParser, remainder: str) -> Command: """ Parse remaining input using given CommandParser into parameters if given parser requires any. :param command_parser: successfully matched command parser :param remainder: the remainder of CLI input to be parsed :return: corresponding Command if parsing is successful, InvalidCommand otherwise """ params_result = command_parser.parse_params(remainder) if params_result.is_successful() and params_result.get_remainder( ) == '': return params_result.get_match() else: return InvalidCommand(self.controller)
def test_remove_shape_parser(controller: Controller, cli_parser: CliParser): # Test invalid inputs invalid_inputs = [ "remove 10,-20", "remove +10,20", "remove 10.20", "remove 10 20", "remove10,20", "remove 10,20 30,40", "removet 10,20", "remove something", "remove 10,20 something" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs valid_inputs = [ ("remove 10,20", RemoveShapeCommand(controller, 10, 20)), ("remove 10, 20", RemoveShapeCommand(controller, 10, 20)), (" remove 10 ,20 ", RemoveShapeCommand(controller, 10, 20)), (" remove 10 , 20", RemoveShapeCommand(controller, 10, 20)), ("remove -10,+20", RemoveShapeCommand(controller, -10, +20)), ("remove -5,-5", RemoveShapeCommand(controller, -5, -5)) ] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected
def parse_input(self, cli_input: str) -> Command: """ Parse given command line input. Parse a command using given CommandParsers. If successful, parse parameters from the rest of the input. :param cli_input: the input from CLI :return: corresponding Command if parsing is successful, InvalidCommand otherwise """ # Parsing the first word -> this will be used as a key to the dictionary with respective parsers word_parser = WordParser() command_result = word_parser.parse_input(cli_input) if command_result.is_successful() and command_result.get_match( ) in self.command_parsers: command_parser = self.command_parsers[command_result.get_match()] if command_parser.has_parameters(): return self.parse_params(command_parser, command_result.get_remainder()) elif command_result.get_remainder() == '': # there must not be any other characters remaining in the input string return command_parser.get_command() return InvalidCommand(self.controller)
def test_list_shape_parser(controller: Controller, cli_parser: CliParser): # Test invalid inputs invalid_inputs = [ "ls 10,-20", "ls +10,20", "ls 10.20", "ls 10 20", "ls10,20", "ls 10,20 30,40", "lst 10,20", "ls 10,20 something", "ls something", "l s" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs valid_inputs = [("ls 10,20", ListShapeCommand(controller, 10, 20)), ("ls 10 ,20 ", ListShapeCommand(controller, 10, 20)), (" ls 10 , 20", ListShapeCommand(controller, 10, 20)), ("ls -10,+20", ListShapeCommand(controller, -10, +20)), ("ls -5,-5", ListShapeCommand(controller, -5, -5)), ("ls", ListShapeCommand(controller)), ("ls ", ListShapeCommand(controller)), (" ls ", ListShapeCommand(controller))] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected
def test_line_parser(controller: Controller, cli_parser: CliParser): invalid_inputs = [ "line 10,-20 30,40", "line 10,20 30,+40", "line 10,20 30.40", "line 10 20 30,40", "line10,20 30,40", "line 10,20", "linet 10,20 30,40", "line something", "line 10,20 30,40 rgb(0,0,-1)", "line 10,20 30,40 rgb(0,0,0", "line 10,20 30,40 rgb 0,0,0", "line 10,20 30,40 rgb(0,1)", "line 10,20 30,40rgb(0,1,2)", "line 10,20 30,40 rgb(1a,2,3)", "line 10,20 30,40,rgb(0,2,3)", "line 10,20 30,40 rgb(256,0,1)", "line 10,20 30,40 rgb(2.0.1)", "line 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 valid_inputs = [("line 10 ,20 30,40", PrintLineCommand(controller, 10, 20, 30, 40, (0, 0, 0))), ("line 10, 20 -10, -20", PrintLineCommand(controller, 10, 20, 0, 0, (0, 0, 0))), (" line -5,-5 +5,+5 ", PrintLineCommand(controller, -5, -5, 0, 0, (0, 0, 0))), ("line -5,-5 10,20", PrintLineCommand(controller, -5, -5, 10, 20, (0, 0, 0))), (" line 10, 20 30, 40 rgb(10,20, 30)", PrintLineCommand(controller, 10, 20, 30, 40, (10, 20, 30))), ("line 10, 20 -10 , -20 rgb ( 0, 0 , 0 ) ", PrintLineCommand(controller, 10, 20, 0, 0, (0, 0, 0)))] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected # Test invalid inputs, three points invalid_inputs = [ "line 10,20 30,40 50,-60", "line 10,20 30,40 50,+60", "line 10,20 30,40 50.60", "line 10,20 30,40 50 60", "line 10,20 30,40 something", "line 10,20 30,40 50,60 rgb(0,0,-1)", "line 10,20 30,40 50,60 rgb(0,0,0", "line 10,20 30,40 rgb 0,0,0", "line 10,20 30,40 50,60 rgb(0,1)", "line 10,20 30,40 50,60 rgb(1a,2,3)", "line 10,20 30,40,50,60 rgb(0,2,3)", "line 10,20 30,40 50,60 rgb(256,0,1)", "line 10,20 30,40 50,60 rgb(2.0.1)", "line 10,20 30,40 50,60rgb(0,1,2)", "line 10,20 30,40 50,60 rgb(123)" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs, three points valid_inputs = [ ("line 10,20 30 ,40 50, 60", PrintPolylineCommand(controller, [(10, 20), (30, 40), (50, 60)], (0, 0, 0))), (" line 10 ,20 -10,-20 10,20", PrintPolylineCommand(controller, [(10, 20), (0, 0), (10, 20)], (0, 0, 0))), ("line -5, -5 +5,+5 5 ,5", PrintPolylineCommand(controller, [(-5, -5), (0, 0), (5, 5)], (0, 0, 0))), ("line -5 , -5 10,20 -10,-20", PrintPolylineCommand(controller, [(-5, -5), (10, 20), (0, 0)], (0, 0, 0))), ("line 10,20 30 ,40 50,60 rgb (10 ,20,30)", PrintPolylineCommand(controller, [(10, 20), (30, 40), (50, 60)], (10, 20, 30))), ("line 10 ,20 -10, -20 +30 ,+40 rgb ( 0, 0 , 0 ) ", PrintPolylineCommand(controller, [(10, 20), (0, 0), (30, 40)], (0, 0, 0))), ] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected # Test invalid inputs, four points invalid_inputs = [ "line 10,20 30,40 50,60 70,-80", "line 10,20 30,40 50,60 70,+80", "line 10,20 30,40 50,60 70.80", "line 10,20 30,40 50,60 70 80", "line 10,20 30,40 50,60 something", "line 10,20 30,40 50,60 70,80 rgb(0,0,-1)", "line 10,20 30,40 50,60 70,80 rgb(0,0,0", "line 10,20 30,40 50,60 70,80 rgb 0,0,0", "line 10,20 30,40 50,60 70,80 rgb(0,1)", "line 10,20 30,40 50,60 70,80 rgb(1a,2,3)", "line 10,20 30,40,50,60 70,80 rgb(0,2,3)", "line 10,20 30,40 50,60 70,80 rgb(256,0,1)", "line 10,20 30,40 50,60 70,80 rgb(2.0.1)", "line 10,20 30,40 50,60 70,80rgb(0,1,2)", "line 10,20 30,40 50,60 70,80 rgb(123)" ] for cli_input in invalid_inputs: command = cli_parser.parse_input(cli_input) assert command == InvalidCommand(controller) # Test valid inputs, four points valid_inputs = [ ("line 10,20 30,40 50,60 70,80", PrintPolylineCommand(controller, [(10, 20), (30, 40), (50, 60), (70, 80)], (0, 0, 0))), ("line 10,20 -10,-20 10,20 -10,-20 ", PrintPolylineCommand(controller, [(10, 20), (0, 0), (10, 20), (0, 0)], (0, 0, 0))), ("line -5,-5 +5 , +5 5 ,5 -5,-5", PrintPolylineCommand(controller, [(-5, -5), (0, 0), (5, 5), (0, 0)], (0, 0, 0))), ("line -5 ,-5 10,20 -10,-20 10,20", PrintPolylineCommand(controller, [(-5, -5), (10, 20), (0, 0), (10, 20)], (0, 0, 0))), ("line 10,20 30,40 50,60 70,80 rgb(10,20,30)", PrintPolylineCommand(controller, [(10, 20), (30, 40), (50, 60), (70, 80)], (10, 20, 30))), (" line 10, 20 -10 , -20 +30,+40 -30 , -40 rgb (0 ,0 ,0 )", PrintPolylineCommand(controller, [(10, 20), (0, 0), (30, 40), (0, 0)], (0, 0, 0))), ] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected
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
def test_circle_parser(controller: Controller, cli_parser: CliParser): # Test invalid inputs, two points as parameters invalid_inputs = [ "circle 10,-20 30,40", "circle 10,20 30,+40", "circle 10,20 30.40", "circle 10 20 30,40", "circle10,20 30,40", "circle 10,20", "circlee 10,20 30,40", "circle something", "circle 10,20 30,40 rgb(0,0,-1)", "circle 10,20 30,40 rgb(0,0,0", "circle 10,20 30,40 rgb 0,0,0", "circle 10,20 30,40 rgb(0,1)", "circle 10,20 30,40rgb(0,1,2)", "circle 10,20 30,40 rgb(1a,2,3)", "circle 10,20 30,40,rgb(0,2,3)", "circle 10,20 30,40 rgb(256,0,1)", "circle 10,20 30,40 rgb(2.0.1)", "circle 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 = [("circle 10,20 30,40", PrintCircleCommand(controller, 10, 20, (0, 0, 0), end_x=30, end_y=40)), ("circle 10, 20 -10,-20", PrintCircleCommand(controller, 10, 20, (0, 0, 0), end_x=0, end_y=0)), ("circle -5,-5 +5, +5", PrintCircleCommand(controller, -5, -5, (0, 0, 0), end_x=0, end_y=0)), ("circle -5 ,-5 10,20 ", PrintCircleCommand(controller, -5, -5, (0, 0, 0), end_x=10, end_y=20)), ("circle 10 ,20 30, 40 rgb (10,20,30)", PrintCircleCommand(controller, 10, 20, (10, 20, 30), end_x=30, end_y=40)), (" circle 10,20 -10, -20 rgb( 0, 0 , 0 ) ", PrintCircleCommand(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 one natural number (radius) as parameters invalid_inputs = [ "circle 10,20 30 40", "circle 10,20 +30", "circle 10,20 -30", "circle 10,20", "circle10,20 30", "circlee 10,20 30", "circle 10,20 something", "circle 10,20 30 rgb(0,0,-1)", "circle 10,20 30 rgb(0,0,0", "circle 10,20 30 rgb 0,0,0", "circle 10,20 30 rgb(0,1)", "circle 10,20 30rgb(0,1,2)", "circle 10,20 30 rgb(1a,2,3)", "circle 10,20 30,rgb(0,2,3)", "circle 10,20 30 rgb(256,0,1)", "circle 10,20 30 rgb(2.0.1)", "circle 10,20 30 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 one natural number (radius) as parameters valid_inputs = [("circle 10, 20 30", PrintCircleCommand(controller, 10, 20, (0, 0, 0), DimensionsCircleFactory, radius=30)), ("circle -5 ,-5 30 ", PrintCircleCommand(controller, -5, -5, (0, 0, 0), DimensionsCircleFactory, radius=30)), (" circle 10 , 20 30 rgb (10, 20,30)", PrintCircleCommand(controller, 10, 20, (10, 20, 30), DimensionsCircleFactory, radius=30)), ("circle 10, 20 30 rgb (0, 0 , 0 ) ", PrintCircleCommand(controller, 10, 20, (0, 0, 0), DimensionsCircleFactory, radius=30))] for cli_input, expected in valid_inputs: command = cli_parser.parse_input(cli_input) assert command == expected