def test_point_parser_relative_points(): """ Test PointParser's parsing of relative points. """ parser = PointParser() # Test invalid inputs invalid_inputs = [ '-10 -20', '-10.-20', '+10', '+ 10,-20', '+10,- 20', '-10,', '+x,-20', '-10,+y', '-x,-y' ] for cli_input in invalid_inputs: result = parser.parse_point(cli_input) assert result == Failure("x,y or (+-)x,(+-)y", cli_input) # Test valid inputs valid_inputs = [ ('-10,+20', RelativeParserPoint(-10, 20), ''), ('+10,+20 ', RelativeParserPoint(10, 20), ''), ('-10 ,-20', RelativeParserPoint(-10, -20), ''), ('+10, -20', RelativeParserPoint(10, -20), ''), ('-10 , +20', RelativeParserPoint(-10, 20), ''), (' +10 , -20', RelativeParserPoint(10, -20), ''), (' +10 , -20 something', RelativeParserPoint(10, -20), 'something'), ('+100,-250 something', RelativeParserPoint(100, -250), 'something'), ('-0,+0', RelativeParserPoint(0, 0), ''), ('+10,-20 30,40', RelativeParserPoint(10, -20), '30,40') ] for cli_input, expected, remainder in valid_inputs: result = parser.parse_point(cli_input) assert result == Success(expected, remainder)
def test_point_parser_absolute_points(): """ Test PointParser's parsing of absolute points. """ parser = PointParser() # Test invalid inputs invalid_inputs = [ '1020', '10 20', '10.20', '10', '10,', 'x,20', '10,y', 'x,y' ] for cli_input in invalid_inputs: result = parser.parse_point(cli_input) assert result == Failure("x,y or (+-)x,(+-)y", cli_input) # Test valid inputs valid_inputs = [ ('10,20', AbsoluteParserPoint(10, 20), ''), ('10,20 ', AbsoluteParserPoint(10, 20), ''), ('10 ,20', AbsoluteParserPoint(10, 20), ''), ('10, 20', AbsoluteParserPoint(10, 20), ''), ('10 , 20', AbsoluteParserPoint(10, 20), ''), (' 10 , 20', AbsoluteParserPoint(10, 20), ''), (' 10 , 20 something', AbsoluteParserPoint(10, 20), 'something'), ('100,250 something', AbsoluteParserPoint(100, 250), 'something'), ('0,0', AbsoluteParserPoint(0, 0), ''), ('10,20 30,40', AbsoluteParserPoint(10, 20), '30,40') ] for cli_input, expected, remainder in valid_inputs: result = parser.parse_point(cli_input) assert result == Success(expected, remainder)
def parse_params(self, cli_input: str) -> ParseResult: result = PointParser().parse_point(cli_input) if result.is_successful(): abs_point = self.convert_points([result.get_match()]) x = abs_point[0].x y = abs_point[0].y return Success(RemoveShapeCommand(self._controller, x, y), result.get_remainder()) else: return result
def parse_point_and_nat(self, cli_input: str) -> ParseResult: """ Parse a Point and a Natural number from given input. """ point_result = PointParser().parse_point(cli_input) if point_result.is_successful(): radius_result = self.radius_parser.parse_input( point_result.get_remainder()) if radius_result.is_successful(): return Success( [point_result.get_match(), radius_result.get_match()], radius_result.get_remainder()) return Failure("circle <POINT> <NAT>", cli_input)
def parse_two_points(cli_input: str) -> ParseResult: """ Parse two Points from given input. """ point_result1 = PointParser().parse_point(cli_input) if point_result1.is_successful(): point_result2 = PointParser().parse_point( point_result1.get_remainder()) if point_result2.is_successful(): return Success( [point_result1.get_match(), point_result2.get_match()], point_result2.get_remainder()) return Failure("<POINT> <POINT>", cli_input)
def __init__(self, controller: Controller, color_parser: ColorParser): self.controller = controller self.color_parser = color_parser self.point_parser = PointParser() self.width_parser = NatParser(' ') self.height_parser = NatParser() self.radius_parser = NatParser() self.command_parsers = { 'remove': RemoveShapeParser(controller), 'move': MoveShapeParser(controller), 'save': SaveParser(controller), 'load': LoadParser(controller), 'quit': QuitParser(controller), 'ls': ListParser(controller), 'clear': ClearParser(controller), 'rect': RectParser(controller, self.width_parser, self.height_parser, self.color_parser), 'circle': CircleParser(controller, self.radius_parser, self.color_parser), 'dot': DotParser(controller, self.color_parser), 'line': LineParser(controller, self.color_parser) }
def parse_params(self, cli_input: str) -> ParseResult: points_result = self.parse_two_points(cli_input) if points_result.is_successful(): points = points_result.get_match() # two points parsed successfully, try to parse a color remainder = points_result.get_remainder() color_result = self.parse_color(remainder, self.color_parser) if color_result.is_successful(): # color parsed successfully, no more points will be parsed color = color_result.get_match() abs_points = self.convert_points(points_result.get_match()) 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( PrintLineCommand(self._controller, start_x, start_y, end_x, end_y, color), '') else: # try to parse a point or a color while True: point_result = PointParser().parse_point(remainder) if point_result.is_successful(): points.append(point_result.get_match()) remainder = point_result.get_remainder() else: color_result = self.parse_color( remainder, self.color_parser) if color_result.is_successful(): # color parsed successfully, no more points will be parsed color = color_result.get_match() abs_points = self.convert_points(points) return Success( PrintPolylineCommand(self._controller, [(p.x, p.y) for p in abs_points], color), color_result.get_remainder()) else: break return Failure("line <POINT> <POINTS>", cli_input)
def parse_point_and_nats(self, cli_input: str) -> ParseResult: """ Parse a Point and two Natural numbers from given input. """ point_result = PointParser().parse_point(cli_input) if point_result.is_successful(): width_result = self.width_parser.parse_input( point_result.get_remainder()) if width_result.is_successful(): height_result = self.height_parser.parse_input( width_result.get_remainder()) if height_result.is_successful(): return Success([ point_result.get_match(), width_result.get_match(), height_result.get_match() ], height_result.get_remainder()) return Failure("rect <POINT> <NAT> <NAT>", cli_input)
def parse_params(self, cli_input: str): if cli_input == '': # point parameter is absent, return ListShapeCommand with default values of parameters x and y return Success(ListShapeCommand(self._controller), '') result = PointParser.parse_point(cli_input) if result.is_successful(): abs_point = self.convert_points([result.get_match()]) x = abs_point[0].x y = abs_point[0].y return Success(ListShapeCommand(self._controller, x, y), result.get_remainder()) else: return result
def parse_params(self, cli_input: str) -> ParseResult: point_result = PointParser().parse_point(cli_input) if point_result.is_successful(): abs_point = self.convert_points([point_result.get_match()]) point_x = abs_point[0].x point_y = abs_point[0].y # Parse color color_result = self.parse_color(point_result.get_remainder(), self.color_parser) if color_result.is_successful(): color = color_result.get_match() return Success( PrintDotCommand(self._controller, point_x, point_y, color), '') else: return Failure(color_result.get_expected(), point_result.get_remainder()) return Failure("dot <POINT>", cli_input)