def assert_matches(regexp, string, anywhere=False): if anywhere: match = re.search else: match = re.match assert match(regexp, string, re.DOTALL), \ "Expected\n%s\nto match r'%s', but it didn't." % (quoted_block(string), regexp)
def parse(code): """String -> AST Parse the string and return its AST representation. May raise a ParseError exception. """ added_newline = False if not code.endswith("\n"): code += "\n" added_newline = True try: drv = driver.Driver(pygram.python_grammar, pytree.convert) result = drv.parse_string(code, True) except ParseError: log.debug("Had problems parsing:\n%s\n" % quoted_block(code)) raise # Always return a Node, not a Leaf. if isinstance(result, Leaf): result = Node(syms.file_input, [result]) result.added_newline = added_newline return result
def assert_doesnt_contain(haystack, needle): assert needle not in haystack,\ "Expected\n%s\nto NOT contain %r, but it did." % (quoted_block(haystack), needle)
def assert_contains_one_after_another(haystack, needle1, needle2): assert re.search(''.join([needle1, '.*', needle2]), haystack, re.DOTALL), \ "Expected\n%s\nto contain %r and then %r, but it didn't." %\ (quoted_block(haystack), needle1, needle2)
def assert_contains_once(haystack, needle): repeated = len(re.findall(re.escape(needle), haystack)) assert repeated == 1, "Expected\n%s\nto contain %r once, but it contained it %d times instead." %\ (quoted_block(haystack), needle, repeated)
def assert_contains(haystack, needle): assert needle in haystack,\ "Expected\n%s\nto contain %r, but it didn't." % (quoted_block(haystack), needle)