def _parse_and_test_file(filename: str) -> bool: print("Parsing %s" % filename) with open(filename, "r") as ifile: contents = ifile.read() try: data = parse(contents) except Exception: print(" Parsing error!") import traceback traceback.print_exc() return False data_lines = [l for l in str(data).split("\n") if l] content_lines = [l for l in contents.split("\n") if l] if data_lines != content_lines: print(" Error!") max_len = max([len(l) for l in content_lines]) if max_len < 100: for orig, parsed in zip_longest(content_lines, data_lines, fillvalue=""): c = " " if orig == parsed else "x" print("%s <%s> %s" % (orig.ljust(max_len), c, parsed)) else: for orig, parsed in zip_longest(content_lines, data_lines, fillvalue="----EMPTY----"): c = " " if orig == parsed else "XXX)" print("%s\n%s%s" % (orig, c, parsed)) return False return True
def _run_test(self, string: str, expected): """ Run a set of tests """ try: parse_result = parse(string) if expected == "error": assert False, "Parsing '%s' should have failed.\nGot: %s" % ( string, parse_result, ) else: self.assertEqual(parse_result, expected) except ParseException as e: if expected != "error": print(string) print(" " * e.loc + "^") print(str(e)) raise except AssertionError: print("Parsing : %s" % string) print("Expected: %r" % expected) print("Got : %r" % parse_result) raise