コード例 #1
0
def test_partial(polar):
    polar.load_str("f(1);")
    polar.load_str("f(x) if x = 1 and x = 2;")

    results = polar.query_rule("f", Partial("x"))
    first = next(results)

    x = first["bindings"]["x"]
    assert unwrap_and(x) == Expression("Unify", [Variable("_this"), 1])

    second = next(results)
    x = second["bindings"]["x"]

    # Top level should be and
    and_args = unwrap_and(x)
    assert and_args[0] == Expression("Unify", [Variable("_this"), 1])

    polar.load_str("g(x) if x.bar = 1 and x.baz = 2;")

    results = polar.query_rule("g", Partial("x"))
    first = next(results)

    x = first["bindings"]["x"]
    and_args = unwrap_and(x)
    assert len(and_args) == 2
    assert unwrap_and(and_args[0]) == Expression(
        "Unify", [Expression("Dot", [Variable("_this"), "bar"]), 1]
    )
    assert unwrap_and(and_args[1]) == Expression(
        "Unify", [Expression("Dot", [Variable("_this"), "baz"]), 2]
    )
コード例 #2
0
def test_return_none(polar, qeval):
    class Foo:
        def this_is_none(self):
            return None

    polar.register_class(Foo)
    polar.load_str("f(x) if x.this_is_none = 1;")
    assert not list(polar.query_rule("f", Foo()))

    polar.load_str("f(x) if x.this_is_none.bad_call = 1;")

    with pytest.raises(exceptions.PolarRuntimeException) as e:
        list(polar.query_rule("f", Foo()))
    assert str(e.value).find(
        "Application error: 'NoneType' object has no attribute 'bad_call'")
コード例 #3
0
def test_partial_constraint(polar):
    class User:
        pass

    class Post:
        pass

    polar.register_class(User)
    polar.register_class(Post)

    polar.load_str("f(x: User) if x.user = 1;")
    polar.load_str("f(x: Post) if x.post = 1;")

    partial = Partial("x", TypeConstraint("User"))
    results = polar.query_rule("f", partial)

    first = next(results)["bindings"]["x"]
    and_args = unwrap_and(first)

    assert len(and_args) == 2

    assert and_args[0] == Expression("Isa", [Variable("_this"), Pattern("User", {})])

    unify = unwrap_and(and_args[1])
    assert unify == Expression(
        "Unify", [Expression("Dot", [Variable("_this"), "user"]), 1]
    )

    with pytest.raises(StopIteration):
        next(results)
コード例 #4
0
def test_static_method(polar, qeval):
    class Foo(list):
        @staticmethod
        def plus_one(x):
            return x + 1

        def map(self, f):
            return [f(x) for x in self]

    polar.register_class(Foo)
    polar.load_str("f(x: Foo) if x.map(Foo.plus_one) = [2, 3, 4];")
    assert next(polar.query_rule("f", Foo([1, 2, 3])))