Ejemplo n.º 1
0
def test_frame_dynamic_database():
    dynamic = Predicate.from_name_arity('dynamic', 1)
    assertz = Predicate.from_name_arity('assertz', 1)

    foo = Functor('foo', 1)
    a = Term.from_atom_name('a')

    dynamic(foo(Term()))

    with Frame(discard=True):
        assertz(foo(a))
        assert_true(foo(a)())

    # Frames have no effect on the dynamic database
    assert_true(foo(a)())
Ejemplo n.º 2
0
def test_frame_close():
    f1 = Frame()
    t1 = f1.term()
    t1.put_atom_name('t1')
    assert_equal(str(t1), 't1')
    f1.close()
    with assert_raises(AttributeError):
        str(t1)
    with assert_raises(AttributeError):
        f1.close()

    with Frame() as f2:
        t2 = f2.term()
        t2.put_atom_name('t2')
        assert_equal(str(t2), 't2')

    with assert_raises(AttributeError):
        str(t2)
    with assert_raises(AttributeError):
        f2.close()

    X = Term()
    with Frame():
        X.put_integer(1)
        assert_equal(str(X), '1')
    assert_equal(str(X), '1')

    eq = Predicate.from_name_arity('=', 2)
    X = Term()
    with Frame():
        eq(X, Term.from_integer(1))
        assert_equal(str(X), '1')
    assert_equal(str(X), '1')
Ejemplo n.º 3
0
def test_term_equality():
    a = Term.from_atom_name('a')
    b = Term.from_atom_name('b')
    eq = Predicate.from_name_arity('=', 2)

    assert_true(eq(a, a))
    assert_false(eq(a, b))
Ejemplo n.º 4
0
def test_predicate__eq__str__repr():
    foo2 = Functor('foo', 2)
    mod = Module(Atom('mod'))

    foo2_no_module = Predicate(foo2)
    foo2_mod = Predicate(foo2, mod)
    foo2_no_module_name_arity = Predicate.from_name_arity('foo', 2)

    assert_equal(foo2_no_module, foo2_no_module_name_arity)
    assert_not_equal(foo2_no_module, foo2_mod)
    assert_not_equal(foo2_no_module_name_arity, foo2_mod)
    assert_equal(foo2_no_module, foo2_no_module)
    assert_equal(foo2_mod, foo2_mod)
    assert_equal(foo2_no_module_name_arity, foo2_no_module_name_arity)

    assert_equal(str(foo2_no_module), 'user:foo/2')
    assert_equal(str(foo2_no_module_name_arity), 'user:foo/2')
    assert_equal(str(foo2_mod), 'mod:foo/2')

    assert_equal(repr(foo2_no_module),
                 'Predicate(functor={functor!r}, module={module!r})'.format(
                     functor=foo2, module=Module.current_context()))
    assert_equal(repr(foo2_mod),
                 'Predicate(functor={functor!r}, module={module!r})'.format(
                     functor=foo2, module=mod))
Ejemplo n.º 5
0
def check_predicate(name, arity, module=None, predicate=None):
    if isinstance(name, str):
        name = Atom(name)
    if isinstance(module, str):
        module = Module(Atom(module))

    module_str = str(module) if module is not None else None

    if predicate is None:
        check_predicate(name=name, arity=arity, module=module,
                        predicate=Predicate.from_name_arity(
                            name=str(name), arity=arity,
                            module_name=module_str)
                        )
        check_predicate(name=name, arity=arity, module=module,
                        predicate=Predicate(
                            functor=Functor(name=name, arity=arity),
                            module=module))
        return

    stored_module = Module.current_context() if module is None else module

    assert_equal(str(predicate), '{module!s}:{name!s}/{arity!s}'.format(
        module=stored_module, name=name, arity=arity))
    assert_equal(repr(predicate),
                 'Predicate(functor={functor!r}, module={module!r})'.format(
                     functor=Functor(name=name, arity=arity),
                     module=stored_module))

    info = predicate.get_info()
    assert_equal(info.name, name)
    assert_equal(info.arity, arity)
    assert_equal(info.module, stored_module)

    assert_equal(predicate, predicate)
    assert_equal(predicate, Predicate(functor=Functor(name, arity),
                                      module=module))
    assert_equal(predicate,
                 Predicate.from_name_arity(str(name), arity, module_str))

    assert_equal(hash(predicate), hash(Predicate(functor=Functor(name, arity),
                                                 module=module)))
Ejemplo n.º 6
0
def test_term_variable_assignment():
    a = Term.from_atom_name('a')
    b = Term.from_atom_name('b')
    eq = Predicate.from_name_arity('=', 2)

    X = Term()
    assert_true(eq(X, a))
    assert_equal(X, a)
    assert_false(eq(X, b))

    assert_true(eq(Term(), b))

    X.put_variable()
    assert_true(eq(X, b))
Ejemplo n.º 7
0
def test_frame_discard():
    eq = Predicate.from_name_arity('=', 2)

    X = Term()
    with Frame(discard=True):
        X.put_integer(1)
        assert_equal(str(X), '1')
    assert_equal(str(X), '1')

    X = Term()
    with Frame(discard=True):
        eq(X, Term.from_integer(1))
        assert_equal(str(X), '1')
        assert_equal(X.type(), 'integer')
    assert_not_equal(str(X), '1')
    assert_equal(X.type(), 'variable')
Ejemplo n.º 8
0
 def test_unification_eq(self):
     X = Term()
     assert_not_equal(self.term, X)
     Predicate.from_name_arity('=', 2)(self.term, X)
     assert_equal(self.term, X)
Ejemplo n.º 9
0
 def setup(self):
     self.retractall = Predicate.from_name_arity('retractall', 1)
     self.assertz = Predicate.from_name_arity('assertz', 1)
     self.dynamic = Predicate.from_name_arity('dynamic', 1)
     self.call = Predicate.from_name_arity('call', 1)
     self.rule = Functor(':-', 2)