def test_VariableReference(self): self.assertEqual(self.parse_expressions("<<ABC>>"), [TestExpression.create_variable_reference("ABC")]) self.assertEqual(self.parse_expressions("123<<ABC>>"), [ TestExpression.create_plain_text("123"), TestExpression.create_variable_reference("ABC") ]) self.assertEqual(self.parse_expressions("123 <<ABC>>"), [ TestExpression.create_plain_text("123 "), TestExpression.create_variable_reference("ABC") ]) self.assertEqual(self.parse_expressions("<<ABC>>XYZ"), [ TestExpression.create_variable_reference("ABC"), TestExpression.create_plain_text("XYZ") ]) self.assertEqual(self.parse_expressions("<<ABC>> XYZ"), [ TestExpression.create_variable_reference("ABC"), TestExpression.create_plain_text(" XYZ") ]) self.assertEqual(self.parse_expressions("123<<ABC>>XYZ"), [ TestExpression.create_plain_text("123"), TestExpression.create_variable_reference("ABC"), TestExpression.create_plain_text("XYZ") ]) self.assertEqual(self.parse_expressions("123 <<ABC>> XYZ"), [ TestExpression.create_plain_text("123 "), TestExpression.create_variable_reference("ABC"), TestExpression.create_plain_text(" XYZ") ])
def parse_checker_statement(parent, line, variant, line_no): """ This method parses the content of a check line stripped of the initial comment symbol and the CHECK-* keyword. """ statement = TestStatement(parent, variant, line, line_no) if statement.is_no_content_statement() and line: Logger.fail("Expected empty statement: '{}'".format(line), statement.filename, statement.line_no) # Loop as long as there is something to parse. while line: # Search for the nearest occurrence of the special markers. if statement.is_eval_content_statement(): # The following constructs are not supported in CHECK-EVAL, -IF and -ELIF lines match_whitespace = None match_pattern = None match_variable_definition = None else: match_whitespace = re.search(r"\s+", line) match_pattern = re.search(TestExpression.Regex.REGEX_PATTERN, line) match_variable_definition = re.search( TestExpression.Regex.REGEX_VARIABLE_DEFINITION, line) match_variable_reference = re.search( TestExpression.Regex.REGEX_VARIABLE_REFERENCE, line) # If one of the above was identified at the current position, extract them # from the line, parse them and add to the list of line parts. if _is_match_at_start(match_whitespace): # A whitespace in the check line creates a new separator of line parts. # This allows for ignored output between the previous and next parts. line = line[match_whitespace.end():] statement.add_expression(TestExpression.create_separator()) elif _is_match_at_start(match_pattern): pattern = line[0:match_pattern.end()] pattern = pattern[2:-2] line = line[match_pattern.end():] statement.add_expression(TestExpression.create_pattern(pattern)) elif _is_match_at_start(match_variable_reference): var = line[0:match_variable_reference.end()] line = line[match_variable_reference.end():] name = var[2:-2] statement.add_expression( TestExpression.create_variable_reference(name)) elif _is_match_at_start(match_variable_definition): var = line[0:match_variable_definition.end()] line = line[match_variable_definition.end():] colon_pos = var.find(":") name = var[2:colon_pos] body = var[colon_pos + 1:-2] statement.add_expression( TestExpression.create_variable_definition(name, body)) else: # If we're not currently looking at a special marker, this is a plain # text match all the way until the first special marker (or the end # of the line). first_match = _first_match([ match_whitespace, match_pattern, match_variable_reference, match_variable_definition ], line) text = line[0:first_match] line = line[first_match:] if statement.is_eval_content_statement(): statement.add_expression( TestExpression.create_plain_text(text)) else: statement.add_expression( TestExpression.create_pattern_from_plain_text(text)) return statement
def assertEqualsVarRef(self, string, name): self.assertEqual(self.parse_expression(string), TestExpression.create_variable_reference(name))