def test_numbervars_simple(): assert_true("numbervars(X, 0, 1).", e) assert_true("numbervars(f(X,Y), 0, 2).", e) assert_true("numbervars(f(X,X), 0, 1).", e) assert_true("numbervars(a, 0, 0).", e) assert_true("numbervars(0, 0, 0).", e) prolog_raises("type_error(_, _)", "numbervars(_, no_number, _)", e)
def test_importlist_intersection(): e = get_engine(""" :- use_module(m, []). """, m = """ :- module(m, [f/1]). f(a). g(a). """) prolog_raises("existence_error(procedure, '/'('f', 1))", "f(a)", e) prolog_raises("existence_error(procedure, '/'('g', 1))", "g(a)", e) e = get_engine(""" :- use_module(m, [g/1]). """, m = """ :- module(m, [f/1]). f(a). g(a). """) prolog_raises("existence_error(procedure, '/'('f', 1))", "f(a)", e) prolog_raises("existence_error(procedure, '/'('g', 1))", "g(a)", e) e = get_engine(""" :- use_module(m, [f/1, g/1]). """, m = """ :- module(m, [f/1]). f(a). g(a). """) assert_true("f(a).", e) prolog_raises("existence_error(procedure, '/'('g', 1))", "g(a)", e)
def test_library_load_priority(): tempdir = "__tempdir__" mod = "m" create_dir(tempdir) create_file(tempdir + "/" + mod, """ :- module(m, [f/1]). f(a). g. """) create_file(mod, """ :- module(m, [f/1, g]). f(b). g. """) try: e = get_engine(":- add_library_dir('%s')." % tempdir) assert len(e.modulewrapper.libs) == 1 assert_true("use_module(library('%s'))." % mod, e) assert_true("f(a).", e) prolog_raises("existence_error(X, Y)", "g", e) finally: delete_dir(tempdir) delete_file(mod)
def test_exception_handling(): assert_true("catch(f, E, true).") assert_true("catch(throw(error), E, true).") prolog_raises("_", "catch(true, E, fail), f") prolog_raises("_", "catch(throw(error(x)), error(failure), fail)") assert_true("catch(catch(throw(error), failure, fail), error, true).") assert_true("catch((X = y, throw(X)), E, E == y).")
def test_once_other_module(): e = get_engine("", m=""" :- module(m, []). f(a). """) assert_true("once(m:f(X)), X = a.", e) prolog_raises("existence_error(_, _)", "f(X)", e)
def test_importlist_intersection(): e = get_engine(""" :- use_module(m, []). """, m=""" :- module(m, [f/1]). f(a). g(a). """) prolog_raises("existence_error(procedure, '/'('f', 1))", "f(a)", e) prolog_raises("existence_error(procedure, '/'('g', 1))", "g(a)", e) e = get_engine(""" :- use_module(m, [g/1]). """, m=""" :- module(m, [f/1]). f(a). g(a). """) prolog_raises("existence_error(procedure, '/'('f', 1))", "f(a)", e) prolog_raises("existence_error(procedure, '/'('g', 1))", "g(a)", e) e = get_engine(""" :- use_module(m, [f/1, g/1]). """, m=""" :- module(m, [f/1]). f(a). g(a). """) assert_true("f(a).", e) prolog_raises("existence_error(procedure, '/'('g', 1))", "g(a)", e)
def test_assert_retract(): e = get_engine("g(b, b).") assert_true("g(B, B).", e) assert_true("assert(g(c, d)).", e) assert_true("assert(g(a, b)).", e) assert_true("assert(g(a, b)).", e) # assert the same rule multiple times assert_true("g(B, B).", e) assert_true("g(a, b).", e) assert_true("g(c, d).", e) assert_true("retract(g(B, B)).", e) assert_false("g(B, B).", e) assert_true("retract(g(a, b)).", e) assert_true("g(a, b).", e) assert_true("retract(g(a, b)).", e) assert_false("retract(g(a, b)).", e) assert_false("g(a, b).", e) assert_true("g(c, d).", e) e = get_engine(""" g(b, b). f(X) :- g(X, b). f(a). """) assert_true("f(b).", e) assert_true("f(a).", e) assert_true("retract(f(X) :- g(X, Y)), Y == b.", e) assert_false("f(b).", e) assert_true("f(a).", e) prolog_raises("permission_error(X, Y, Z)", "retract(atom(X))")
def test_hook_not_defined(): e = get_engine("", m = """ :- module(m2, []). """) prolog_raises("existence_error(A, B)", "put_attr(X, bla, blub), X = 1") prolog_raises("existence_error(A, B)", "put_attr(X, m, blub), X = 1", e)
def test_freeze(): assert_true("freeze(X, Y = 1), X = 1, Y == 1.", e) assert_false("freeze(X, true), X = 1, attvar(X).", e) assert_false("freeze(X, Y = 1), X = 1, fail; Y == 1.", e) assert_true("freeze(a, Y = 1), Y == 1.", e) prolog_raises("existence_error(_, _)", "freeze(a, a)", e) assert_true("freeze(X, Y = 1), freeze(X, Z = 2), X = a, Y == 1, Z == 2.", e) assert_true("freeze(X, f(a)), X = 1.", e) assert_true("freeze(X, user:f(a)), X = 1.", e) assert_true("freeze(X, m:g(q)), X = 1.", e) assert_false("freeze(X, Y = 1), freeze(X, Y = 2), X = a.", e) assert_true("freeze(X, Y = 1), freeze(X, Z = 2), X = a, Y == 1, Z == 2.", e) assert_true( "freeze(X, Z1 = 1), freeze(Y, Z2 = 2), var(Z1), var(Z2), X = Y, var(Z1), var(Z2).", e) assert_true( "freeze(X, Z1 = 1), freeze(Y, Z2 = 2), var(Z1), var(Z2), X = Y, var(Z1), var(Z2), X = 1, Z1 == 1, Z2 == 2.", e) assert_true( "freeze(X, Z1 = 1), freeze(Y, Z2 = 2), var(Z1), var(Z2), X = Y, var(Z1), var(Z2), Y = 1, Z1 == 1, Z2 == 2.", e) assert_true( "freeze(X, Z1 = 1), freeze(Y, Z2 = 2), var(Z1), var(Z2), Y = X, var(Z1), var(Z2), Y = 1, Z1 == 1, Z2 == 2.", e)
def test_once_other_module(): e = get_engine("", m = """ :- module(m, []). f(a). """) assert_true("once(m:f(X)), X = a.", e) prolog_raises("existence_error(_, _)", "f(X)", e)
def test_switch_to_nonexistent_module(): e = get_engine(""" :- module(m). """) prolog_raises("existence_error(X, Y)", "x", e) assert_true("assert(x).", e) assert_true("x.", e) assert_true("module(user).", e) prolog_raises("existence_error(X, Y)", "x", e)
def test_assert_retract_colon(): e = get_engine(""" :(1, 2, 3). :(a). """) assert_true(":(1, 2, 3), :(a).", e) assert_true("assert(:(a, b, c, d)).", e) assert_true(":(a, b, c, d).", e) assert_true("retract(:(a, b, c, d)).", e) prolog_raises("existence_error(_, _)", ":(a, b, c, d)", e)
def test_abolish_from_other_module(): e = get_engine(""" :- use_module(m). """, m = """ :- module(m, []). a. """) assert_true("m:abolish(a/0).", e) prolog_raises("existence_error(_, _)", "m:a", e)
def test_abolish_colon(): e = get_engine(""" :(a). :(1, 2, 3). """) assert_true("abolish(:/1).", e) prolog_raises("existence_error(_, _)", ":(a)", e) assert_true(":(1, 2, 3).", e) assert_true("abolish(:/3).", e) prolog_raises("existence_error(_, _)", ":(1, 2, 3)", e)
def test_abolish_from_other_module(): e = get_engine(""" :- use_module(m). """, m=""" :- module(m, []). a. """) assert_true("m:abolish(a/0).", e) prolog_raises("existence_error(_, _)", "m:a", e)
def test_read_eof_error(): src = "__src__" create_file(src, "f(a)") try: prolog_raises("syntax_error(E)", """ open('%s', read, S), read(S, X) """ % src) finally: delete_file(src)
def test_put_byte_below_zero(): target = "__xxx__" create_file(target, "") try: prolog_raises("type_error(byte, X)", """ open('%s', write, S), put_byte(S, -1) """ % target) finally: delete_file(target)
def test_put_char_type_error(): src = "__src__" create_file(src, "") try: prolog_raises("type_error(X, Y)", """ open('%s', write, S), put_char(S, aa) """ % src) finally: delete_file(src)
def test_asserta_other_module(): e = Engine() assert_true("asserta(m:f(a)).", e) assert len(e.modulewrapper.modules) == 2 assert_true("m:f(a).", e) prolog_raises("existence_error(_, _)", "f(a)", e) assert_true("module(m).", e) assert_true("f(a).", e) assert_true("module(user).", e) prolog_raises("existence_error(_, _)", "f(a)", e)
def test_del_attributes(): assert_true("del_attr(X, m).") assert_true("del_attr(a, m).") prolog_raises("instantiation_error", "del_attr(X, Y)") prolog_raises("type_error(A, B)", "del_attr(X, 1)") assert_false("put_attr(X, m, 1), del_attr(X, m), attvar(X).") assert_true("""put_attr(X, m, 1), put_attr(X, m2, 2), del_attr(X, m), attvar(X).""") assert_true("put_attr(X, m, 1), (del_attr(X, m), fail; true), get_attr(X, m, 1).") assert_true("put_attr(X, m, 1), (del_attr(X, m), fail; attvar(X)).")
def test_put_attrs(): e = Engine(load_system=True) assert_false("put_attrs(X, []), attvar(X).", e) prolog_raises("representation_error(A, B)", "put_attrs(a, [])", e) prolog_raises("representation_error(A, B)", "put_attrs(a, att(m, 1, []))", e) assert_true("put_attrs(X, att(m, 1, [])), get_attr(X, m, 1).", e) assert_true("put_attrs(X, att(m, 1, att(n, W, []))), get_attr(X, m, 1), get_attr(X, n, W).", e) assert_false("put_attrs(X, att(m, 1, [])), fail; attvar(X).", e) assert_true("put_attr(X, m, 1), (put_attrs(X, att(m, 2, [])), fail; true), get_attr(X, m, 1).", e) assert_true("put_attr(X, m, 1), put_attrs(X, att(m, 2, [])), get_attr(X, m, 2).", e)
def test_importlist_with_not_existing_rule(): e = Engine() m = "mod" create_file(m, """ :- module('%s', [f/1]). """ % m) try: prolog_raises("import_error(mod, 'f/1')", "use_module(%s)" % m, e) assert e.modulewrapper.current_module.name == "user" finally: delete_file(m)
def test_put_byte_below_zero(): target = "__xxx__" create_file(target, "") try: prolog_raises( "type_error(byte, X)", """ open('%s', write, S), put_byte(S, -1) """ % target) finally: delete_file(target)
def test_put_char_type_error(): src = "__src__" create_file(src, "") try: prolog_raises( "type_error(X, Y)", """ open('%s', write, S), put_char(S, aa) """ % src) finally: delete_file(src)
def test_consult(): p = py.test.ensuretemp("prolog") f = p.join("test.pl") f.write("g(a, a). g(a, b).") e = get_engine("g(c, c).") assert_true("g(c, c).", e) assert_true("consult('%s')." % (f, ), e) assert_true("g(c, c).", e) assert_true("g(a, a).", e) assert_true("g(a, b).", e) prolog_raises("_", "consult('/hopefully/does/not/exist')")
def test_del_attributes(): assert_true("del_attr(X, m).") assert_true("del_attr(a, m).") prolog_raises("instantiation_error", "del_attr(X, Y)") prolog_raises("type_error(A, B)", "del_attr(X, 1)") assert_false("put_attr(X, m, 1), del_attr(X, m), attvar(X).") assert_true("""put_attr(X, m, 1), put_attr(X, m2, 2), del_attr(X, m), attvar(X).""") assert_true( "put_attr(X, m, 1), (del_attr(X, m), fail; true), get_attr(X, m, 1).") assert_true("put_attr(X, m, 1), (del_attr(X, m), fail; attvar(X)).")
def test_read_eof_error(): src = "__src__" create_file(src, "f(a)") try: prolog_raises( "syntax_error(E)", """ open('%s', read, S), read(S, X) """ % src) finally: delete_file(src)
def test_nonexisting_predicates_in_import_list(): e = get_engine(""" :- use_module(m, [z/0, g/1]). """, m = """ :- module(m, [f/1, g/0]). f(a). g. q. """) prolog_raises("existence_error(X, Y)", "z", e) prolog_raises("existence_error(X, Y)", "g(A)", e)
def test_put_attr_and_get_attr(): assert_true("put_attr(X, m, 1).") assert_true("put_attr(X, m, abc).") assert_true("put_attr(X, m, Y).") prolog_raises("type_error(A, B)", "put_attr(X, 1, 1)") assert_true("put_attr(X, m1, 1), put_attr(X, m2, 1), put_attr(X, m1, 2), get_attr(X, m1, 2), get_attr(X, m2, 1).") assert_true("put_attr(X, b, 1), (put_attr(X, b, 1), fail; get_attr(X, b, 1)).") assert_true("put_attr(X, a, 1), Y = X, attvar(Y), attvar(X).") assert_true("put_attr(X, a, 1), X = Y, attvar(Y), attvar(X).") e = get_engine("g(X) :- !, put_attr(X, m, 1), fail.") assert_true("\+ g(X), \+ attvar(X).", e) prolog_raises("representation_error(_, _)", "put_attr(a, a, a)")
def test_load_broken_module_twice(): e = Engine() m = "mod" create_file(m, """ :- module(%s, [f/1]). """ % m) try: prolog_raises("import_error(mod, 'f/1')", "use_module('%s')" % m, e) assert_true("use_module('%s')." % m, e) assert m in e.modulewrapper.modules finally: delete_file(m)
def test_nonexisting_predicates_in_import_list(): e = get_engine(""" :- use_module(m, [z/0, g/1]). """, m=""" :- module(m, [f/1, g/0]). f(a). g. q. """) prolog_raises("existence_error(X, Y)", "z", e) prolog_raises("existence_error(X, Y)", "g(A)", e)
def test_retract_other_module(): e = get_engine("", m=""" :- module(m, []). f(a). f(b). """) assert_true("m:f(a), m:f(b).", e) assert_true("retract(m:f(a)).", e) assert_false("retract(m:f(a)).", e) assert_false("m:f(a).", e) assert_true("m:f(b).", e) assert_true("retract(m:f(b)).", e) prolog_raises("existence_error(_, _)", "f(b)", e)
def test_import_list_simple(): e = get_engine(""" :- use_module(m, [f/1, g/0]). h(X) :- f(X), g. """, m=""" :- module(m, [f/1, g/0]). f(a). g. q. """) assert_true("h(a).", e) prolog_raises("existence_error(X, Y)", "q", e)
def test_open_with_options(): m = "mod" create_file(m, """ :- module(%s, []). """ % m) try: prolog_raises("domain_error(stream_option, _)", "open(%s, read, _, [g, 1, a, f(a)])" % m) prolog_raises("instantiation_error", "open(%s, read, _, [f(a), X])" % m) assert_true("open(%s, read, _, [])." % m) assert_true("open(%s, read, _, [a, f(a), []])." % m) assert_true("open(%s, read, _, [a, f(a), []])." % m) finally: delete_file(m)
def test_import_list_simple(): e = get_engine(""" :- use_module(m, [f/1, g/0]). h(X) :- f(X), g. """, m = """ :- module(m, [f/1, g/0]). f(a). g. q. """) assert_true("h(a).", e) prolog_raises("existence_error(X, Y)", "q", e)
def test_assert_rule_into_other_module_2(): e = get_engine(""" :- use_module(m). """, m=""" :- module(m, [f/1]). f(Rule) :- assert(Rule). """) assert_true("f(g(a)).", e) prolog_raises("existence_error(_, _)", "g(a)", e) assert_true("m:g(a).", e)
def test_assert_rule_into_other_module_2(): e = get_engine(""" :- use_module(m). """, m = """ :- module(m, [f/1]). f(Rule) :- assert(Rule). """) assert_true("f(g(a)).", e) prolog_raises("existence_error(_, _)", "g(a)", e) assert_true("m:g(a).", e)
def test_block(): e = get_engine(""" :- block f('?', '-', '?'), f('-', '-', '?'). :- block g('?'). :- block h('-', '-', '-', '?'), h('-', '-', '?', '?'), h('-', '?', '?', '?'). f(A, B, C) :- C = 10. g(_) :- assert(gg(1)). h(A, B, C, D) :- D is A + B + C. """, load_system=True) assert_false("f(X, Y, Z), Z == 10.", e) assert_false("f(a, X, Z), Z == 10.", e) assert_true("f(a, b, Z), Z == 10.", e) assert_true("f(a, X, Z), \+ Z == 10, X = 5, Z == 10.", e) assert_true("f(A, B, Z), \+ Z == 10, A = a, \+ Z == 10, B = b, Z == 10.", e) assert_true("f(a, X, Z), (X = 1, fail; true), var(Z).", e) prolog_raises("existence_error(_, _)", "g(5), gg(1)", e) prolog_raises("existence_error(_, _)", "g(X), gg(1)", e) assert_true("h(A, B, C, D), var(D), C = 5, var(D), B = 5, var(D), A = 5, D == 15.", e) prolog_raises("instantiation_error", "h(5, B, C, D)", e) prolog_raises("instantiation_error", "h(5, 1, C, D)", e) assert_true("h(1, 2, 3, 6).", e) assert_true("h(A, B, C, D), var(D), B = 5, var(D), C = 5, var(D), A = 5, D == 15.", e)
def test_retract_other_module(): e = get_engine("", m = """ :- module(m, []). f(a). f(b). """) assert_true("m:f(a), m:f(b).", e) assert_true("retract(m:f(a)).", e) assert_false("retract(m:f(a)).", e) assert_false("m:f(a).", e) assert_true("m:f(b).", e) assert_true("retract(m:f(b)).", e) prolog_raises("existence_error(_, _)", "f(b)", e)
def test_assert_rule_into_other_module(): e = get_engine(""" :- use_module(m). """, m=""" :- module(m, []). """) assert_true("m:assert(a).", e) assert_true("m:a.", e) prolog_raises("existence_error(_, _)", "a", e) assert_true("m:assert(user:b).", e) assert_true("b.", e) prolog_raises("existence_error(_, _)", "m:b", e)
def test_assert_rule_into_other_module(): e = get_engine(""" :- use_module(m). """, m = """ :- module(m, []). """) assert_true("m:assert(a).", e) assert_true("m:a.", e) prolog_raises("existence_error(_, _)", "a", e) assert_true("m:assert(user:b).", e) assert_true("b.", e) prolog_raises("existence_error(_, _)", "m:b", e)
def test_module_prefixing(): e = get_engine(""" a. """, m=""" :- module(m, []). f(a). f(b). """) assert_true("m:f(a), m:f(b).", e) assert_true("m:f(a), a.", e) prolog_raises("existence_error(X, Y)", "m:a", e) assert_true("module(m).", e) prolog_raises("existence_error(X, Y)", "a", e) assert_true("user:a.", e)
def test_use_module_locate_file(): src1 = "src.pl" src2 = "src2" create_file(src1, ":- module(src, []).") create_file(src2, ":- module(src2, []).") try: assert_true("use_module('%s')." % src1) assert_true("use_module('%s')." % "src") # XXX some problems with unification, should be existence_error(_, _) # instead of X prolog_raises("X", "use_module('%s')" % "src2.pl") assert_true("use_module('%s')." % "src2") finally: delete_file(src1) delete_file(src2)
def test_close(): src = "__src__" create_file(src, "") try: e = get_engine(""" :- open('%s', read, S), close(S). """ % src) assert len(e.streamwrapper.streams) == 2 assert 0 in e.streamwrapper.streams assert 1 in e.streamwrapper.streams prolog_raises("instantiation_error", "close(X)") prolog_raises("domain_error(stream, Y)", "close(a)") finally: delete_file(src)
def test_abolish_other_module(): e = get_engine("", m=""" :- module(m, []). f(a). f(b). g(c). """) assert_true("m:f(a), m:f(b), m:g(c).", e) assert_true("abolish(m:f/1).", e) prolog_raises("existence_error(_, _)", "m:f(X)", e) assert_true("m:g(c).", e) assert_true("abolish(m:g/1).", e) prolog_raises("existence_error(_, _)", "m:g(c)", e) assert_true("abolish(m:g/1).", e)
def test_put_attr_and_get_attr(): assert_true("put_attr(X, m, 1).") assert_true("put_attr(X, m, abc).") assert_true("put_attr(X, m, Y).") prolog_raises("type_error(A, B)", "put_attr(X, 1, 1)") assert_true( "put_attr(X, m1, 1), put_attr(X, m2, 1), put_attr(X, m1, 2), get_attr(X, m1, 2), get_attr(X, m2, 1)." ) assert_true( "put_attr(X, b, 1), (put_attr(X, b, 1), fail; get_attr(X, b, 1)).") assert_true("put_attr(X, a, 1), Y = X, attvar(Y), attvar(X).") assert_true("put_attr(X, a, 1), X = Y, attvar(Y), attvar(X).") e = get_engine("g(X) :- !, put_attr(X, m, 1), fail.") assert_true("\+ g(X), \+ attvar(X).", e) prolog_raises("representation_error(_, _)", "put_attr(a, a, a)")
def test_seek_domain_error(): src = "__src__" create_file(src, "") try: prolog_raises("domain_error(seek_method, Y)", """ open('%s', read, S), seek(S, 1, ajhdsasd, P) """ % src) prolog_raises("domain_error(position, X)", """ open('%s', read, S), seek(S, -1, bof, A) """ % src) finally: delete_file(src)