<commit_before>import py, pytest

from prolog.interpreter.parsing import get_engine
from prolog.interpreter.parsing import get_query_and_vars
from prolog.interpreter.error import UncaughtError

def test_errstr():
    e = get_engine("""
        f(X) :- drumandbass(X).
    """)
    (t, vs) = get_query_and_vars("f(X).")

    m = e.modulewrapper

    info = pytest.raises(UncaughtError, e.run_query_in_current, t)
    assert info.value.get_errstr(e) == "Undefined procedure: drumandbass/1"

def test_exception_knows_rule():
    e = get_engine("""
        f(1).
        f(X) :- drumandbass(X).
    """)
    (t, vs) = get_query_and_vars("f(X), X = 2.")

    m = e.modulewrapper
    sig = t.argument_at(0).signature()
    rule = m.user_module.lookup(sig).rulechain.next

    info = pytest.raises(UncaughtError, e.run_query_in_current, t)
    assert info.value.rule is rule

    # toplevel rule
    (t, vs) = get_query_and_vars("drumandbass(X).")

    m = e.modulewrapper

    info = pytest.raises(UncaughtError, e.run_query_in_current, t)
    assert info.value.rule is m.current_module._toplevel_rule

def test_exception_knows_rule_change_back_to_earlier_rule():
    e = get_engine("""
        g(a).
        f(X) :- g(X), drumandbass(X).
    """)
    (t, vs) = get_query_and_vars("f(X).")

    m = e.modulewrapper
    sig = t.signature()
    rule = m.user_module.lookup(sig).rulechain

    info = pytest.raises(UncaughtError, e.run_query_in_current, t)
    assert info.value.rule is rule<commit_msg>factor out catching the error into a helper<commit_after>import py, pytest

from prolog.interpreter.parsing import get_engine
from prolog.interpreter.parsing import get_query_and_vars
from prolog.interpreter.error import UncaughtError

def get_uncaught_error(query, e):
    if isinstance(query, str):
        (query, _) = get_query_and_vars(query)
    return pytest.raises(UncaughtError, e.run_query_in_current, query).value


def test_errstr():
    e = get_engine("""
        f(X) :- drumandbass(X).
    """)
    error = get_uncaught_error("f(X).", e)
    assert error.get_errstr(e) == "Undefined procedure: drumandbass/1"

def test_exception_knows_rule():
    e = get_engine("""
        f(1).
        f(X) :- drumandbass(X).
    """)
    (t, vs) = get_query_and_vars("f(X), X = 2.")

    m = e.modulewrapper
    sig = t.argument_at(0).signature()
    rule = m.user_module.lookup(sig).rulechain.next
    error = get_uncaught_error(t, e)
    assert error.rule is rule

def test_exception_knows_rule_toplevel():
    # toplevel rule
    e = get_engine("")
    m = e.modulewrapper
    error = get_uncaught_error("drumandbass(X).", e)
    assert error.rule is m.current_module._toplevel_rule

def test_exception_knows_rule_change_back_to_earlier_rule():
    e = get_engine("""
        g(a).
        f(X) :- g(X), drumandbass(X).
    """)
    (t, vs) = get_query_and_vars("f(X).")

    m = e.modulewrapper
    sig = t.signature()
    rule = m.user_module.lookup(sig).rulechain

    error = get_uncaught_error(t, e)
    assert error.rule is rule

{'fraction_non_alphanumeric': 0.13216875411997364, 'fraction_numerical': 0.0026367831245880024, 'mean_word_length': 3.4113372093023258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4316655', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1136, 'n_words': 212}