def parse_wrap_semantic_error(string, expr, contains=None): """ Assert semantic error. If contains is not None, it is a substring that must be contained in the error. Returns the exception. """ if isinstance(expr, ParsingElement): expr = expr.get() try: res = parse_wrap(expr, string)[0] # note the 0, first element except DPSemanticError as e: if contains is not None: s = str(e) if not contains in s: msg = 'Expected a DPSemanticError with substring %r.' % contains raise_wrapped(TestFailed, e, msg, expr=find_parsing_element(expr), string=string) return e else: return e except BaseException as e: msg = 'Expected DPSemanticError, but obtained %s.' % type(e) raise_wrapped(TestFailed, e, msg, expr=find_parsing_element(expr), string=string) msg = 'Expected DPSemanticError, but no except was thrown.' raise_desc(TestFailed, msg, expr=find_parsing_element(expr), string=string, result=res) assert False
def parse_wrap_syntax_error(string, expr): """ Assert syntax error """ if isinstance(expr, ParsingElement): expr = expr.get() try: res = parse_wrap(expr, string) _res = res[0] # note the 0, first element msg = 'Expected DPSyntaxError.' raise_desc(TestFailed, msg, res=res.__repr__()) except DPSyntaxError as e: return e except BaseException as e: msg = 'Expected DPSyntaxError.' raise_wrapped(TestFailed, e, msg, expr=find_parsing_element(expr), string=string)
def parse_wrap_check(string, expr, result=None): check_isinstance(string, str) if isinstance(expr, ParsingElement): expr = expr.get() try: expr_name = find_parsing_element(expr) except ValueError: expr_name = '(unknown parse expr)' try: res = parse_wrap(expr, string)[0] # note the 0, first element res0 = remove_where_info(res) if result is not None: assert_equal(result, res0) return res except BaseException as e: msg = 'Cannot parse %r' % string raise_wrapped(TestFailed, e, msg, expr_name=expr_name, string=string, expected=result)
def syn(expr, string): expr = find_parsing_element(expr) register_indep(parse_wrap_syntax_error, dynamic=False, args=(string, expr), kwargs=dict())
def ok(expr, string, result=None): expr = find_parsing_element(expr) register_indep(parse_wrap_check, dynamic=False, args=(string, expr, result), kwargs=dict())