def test_peek_code(): src = "__src__" empty = "__empty__" create_file(src, "¼") create_file(empty, "") try: assert_true(""" open('%s', read, S), peek_code(S, C), C = 194, peek_code(S, D), D = 194, get_code(S, _), peek_code(S, E), E = 188, get_code(S, _), peek_code(S, F), F = -1, close(S). """ % src) assert_true(""" open('%s', read, S), peek_code(S, -1), close(S). """ % empty) finally: delete_file(src) delete_file(empty)
def test_impl_use_module(): from prolog.builtin.modules import impl_use_module from prolog.interpreter.heap import Heap filecontent = """ :- module(blub, []). """ e = Engine() h = Heap() m = e.modulewrapper create_file("blub.pl", filecontent) try: impl_use_module(e, m.user_module, h, term.Callable.build("blub.pl")) assert "blub" in e.modulewrapper.modules.keys() finally: delete_file("blub.pl") create_file("blub", filecontent) m.modules = {} m.seen_modules = {} try: impl_use_module(e, m.user_module, h, term.Callable.build("blub")) assert "blub" in m.modules.keys() finally: delete_file("blub")
def test_unify_explicit_alias(): src = "__src__" create_file(src, "") try: assert_true("open('%s', read, S, [alias(s)]), S = s." % src) finally: delete_file(src)
def test_peek_char(): src = "__src__" empty = "__empty__" create_file(src, "aü¼") create_file(empty, "") try: assert_true(""" open('%s', read, S), peek_char(S, C), C = 'a', peek_char(S, D), D = 'a', get_char(S, _), peek_char(S, E), E = 'ü', peek_char(S, F), F = 'ü', get_char(S, _), peek_char(S, G), G = '¼', get_char(S, _), peek_char(S, Z), Z = end_of_file, close(S). """ % src) assert_true(""" open('%s', read, S), peek_char(S, end_of_file), close(S). """ % empty) finally: delete_file(src) delete_file(empty)
def test_peek_byte(): src = "__src__" empty = "__empty__" create_file(src, "\x94\xef") create_file(empty, "") try: assert_true(""" open('%s', read, S), peek_byte(S, C), C = 148, peek_byte(S, D), D = 148, get_byte(S, _), peek_byte(S, E), E = 239, peek_byte(S, F), F = 239, get_byte(S, _), peek_byte(S, Z), Z = -1, close(S). """ % src) assert_true(""" open('%s', read, S), peek_byte(S, -1), close(S). """ % empty) finally: delete_file(src) delete_file(empty)
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_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 XXX_test_at_end_of_stream_or(): src = "__src__" create_file(src, "a") try: assert_false(""" open('%s', read, S), (at_end_of_stream(S); at_end_of_stream(S)). """ % src) finally: delete_file(src)
def test_see_with_file(): src = "__src__" create_file(src, "abc") try: assert_true(""" see('%s'), get_char(a), get_char(b), get_char(c). """ % src) finally: delete_file(src)
def test_get_source(): content = "some important content" name = "__testfile__" try: create_file(name, content) source, file_name = get_source(name) finally: delete_file(name) assert source == content assert file_name == os.path.abspath(name)
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_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_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_see_with_alias(): src = "__src__" create_file(src, "abc") try: assert_true(""" open('%s', read, S, [alias(xxx)]), see(xxx), get_char(a), get_char(b), get_char(c). """ % src) finally: delete_file(src)
def test_file_ending(): content = "some content" filename = "__testfile__.pl" searchname = filename[:len(filename) - 3] try: create_file(filename, content) source, file_name = get_source(searchname) finally: delete_file(filename) assert source == content assert file_name == os.path.abspath(filename)
def test_read_comment_1(): src = "__src__" create_file(src, " % asd ") try: assert_true(""" open('%s', read, S), read(S, end_of_file), close(S). """ % src) finally: delete_file(src)
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_get_char_at_eof(): src = "__empty__" create_file(src, "") try: assert_true(""" open('%s', read, S), get_char(S, C), C = end_of_file, close(S). """ % src) finally: delete_file(src)
def test_at_end_of_stream_empty(): src = "__src__" create_file(src, "") try: assert_true(""" open('%s', read, S), at_end_of_stream(S), close(S). """ % src) finally: delete_file(src)
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_current_input_2(): src = "__src__" create_file(src, "") try: assert_true(""" open('%s', read, S), set_input(S), close(S). """ % src) finally: delete_file(src)
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_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_nl(): src = "__src__" create_file(src, "") try: assert_true(""" open('%s', write, S), nl(S), close(S). """ % src) assert "\n" == file_content(src) finally: delete_file(src)
def test_write_unify(): src = "__src__" term = "X = a" create_file(src, "") try: assert_true(""" open('%s', write, S), write(S, %s), close(S). """ % (src, term)) finally: delete_file(src)
def test_write(): src = "__src__" term = "f(a, b, g(e))" create_file(src, "") try: assert_true(""" open('%s', write, S), write(S, %s), close(S). """ % (src, term)) assert term == file_content(src) finally: delete_file(src)
def test_unify_default_alias(): src = "__src__" create_file(src, "") try: e = Engine() assert_true("open('%s', read, S)." % src, e) assert len(e.streamwrapper.aliases) == 3 for key in e.streamwrapper.aliases.keys(): if not key.endswith("_0") and not key.endswith("_1"): alias = key assert_true("S = '%s'." % alias, e) finally: delete_file(src)
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_read(): src = "__src__" create_file(src, "f(a). b. g(d(x)).") try: assert_true(""" open('%s', read, S), read(S, X), X = f(a), read(S, Y), Y = b, read(S, Z), Z = g(d(x)), close(S). """ % src) finally: delete_file(src)
def test_get_byte(): src = "__src__" create_file(src, "\xa4\x17\xcf") try: assert_true(""" open('%s', read, S), get_byte(S, B), B = 164, get_byte(S, C), C = 23, get_byte(S, D), D = 207, get_byte(S, E), E = -1, close(S). """ % src) finally: delete_file(src)
def test_read_current_stream(): src = "__src__" create_file(src, """ f(a). """) try: assert_true(""" open('%s', read, S), set_input(S), read(X), X = f(a), close(S). """ % src) finally: delete_file(src)
def test_put_byte(): target = "__target__" create_file(target, "") try: assert_true(""" open('%s', write, S), put_byte(S, 97), put_byte(S, 194), put_byte(S, 165), close(S). """ % target) assert file_content(target) == "a¥" finally: delete_file(target)
def test_read_comment_2(): src = "__src__" create_file(src, """ f(a).%f(b). g(x). """) try: assert_true(""" open('%s', read, S), read(S, X), X = f(a), read(S, Y), Y = g(x), close(S). """ % src) finally: delete_file(src)
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)