Skip to content

Commit

Permalink
test: add tests for val predicates visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofavorito committed Jul 8, 2023
1 parent a4dc28b commit 8f7bfbc
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 3 deletions.
7 changes: 4 additions & 3 deletions plan4past/utils/val_predicates_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from pddl.logic.predicates import Predicate
from pylogics.syntax.base import And as PLTLAnd
from pylogics.syntax.base import Formula
from pylogics.syntax.base import Not as PLTLNot
from pylogics.syntax.base import Or as PLTLOr
from pylogics.syntax.base import _BinaryOp, _UnaryOp
Expand Down Expand Up @@ -55,9 +54,11 @@ def val_predicates_unaryop(formula: _UnaryOp):


@singledispatch
def val_predicates(formula: Formula) -> Set[Predicate]:
def val_predicates(formula: object) -> Set[Predicate]:
"""Compute the value predicate for a formula."""
raise NotImplementedError(f"handler not implemented for formula {type(formula)}")
raise NotImplementedError(
f"handler not implemented for object of type {type(formula)}"
)


@val_predicates.register
Expand Down
139 changes: 139 additions & 0 deletions tests/test_utils/test_val_predicates_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# -*- coding: utf-8 -*-
#
# Copyright 2021 -- 2023 WhiteMech
#
# ------------------------------
#
# This file is part of Plan4Past.
#
# Plan4Past is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plan4Past is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Plan4Past. If not, see <https://www.gnu.org/licenses/>.
#

"""This module contains tests for the plan4past.utils.val_predicates_visitor module."""
import pytest
from pddl.logic import Predicate
from pylogics.syntax.pltl import Atomic as PLTLAtomic
from pylogics.syntax.pltl import (
Before,
Once,
PropositionalFalse,
PropositionalTrue,
Since,
)

from plan4past.helpers.utils import add_val_prefix
from plan4past.utils.val_predicates_visitor import val_predicates


def test_val_predicates_visitor_not_implemented_fail():
"""Test the val predicates visitor when the input argument is not supported."""
with pytest.raises(
NotImplementedError,
match="handler not implemented for object of type <class 'int'>",
):
val_predicates(1)


def test_val_predicates_visitor_true():
"""Test the val predicates visitor for the propositional true."""
assert val_predicates(PropositionalTrue()) == {Predicate(add_val_prefix("true"))}


def test_val_predicates_visitor_false():
"""Test the val predicates visitor for the propositional false."""
assert val_predicates(PropositionalFalse()) == {Predicate(add_val_prefix("false"))}


def test_val_predicates_visitor_atomic():
"""Test the val predicates visitor for the atomic formula."""
a = PLTLAtomic("a")
assert val_predicates(a) == {Predicate(add_val_prefix(a.name))}


def test_val_predicates_visitor_and():
"""Test the val predicates visitor for the and formula."""
a = PLTLAtomic("a")
b = PLTLAtomic("b")

val_a_and_b = Predicate(add_val_prefix("a-and-b"))
val_a = Predicate(add_val_prefix(a.name))
val_b = Predicate(add_val_prefix(b.name))

assert val_predicates(a & b) == {val_a_and_b, val_a, val_b}


def test_val_predicates_visitor_or():
"""Test the val predicates visitor for the or formula."""
a = PLTLAtomic("a")
b = PLTLAtomic("b")

val_a_or_b = Predicate(add_val_prefix("a-or-b"))
val_a = Predicate(add_val_prefix(a.name))
val_b = Predicate(add_val_prefix(b.name))

assert val_predicates(a | b) == {val_a_or_b, val_a, val_b}


def test_val_predicates_visitor_not():
"""Test the val predicates visitor for the not formula."""
a = PLTLAtomic("a")

val_not_a = Predicate(add_val_prefix("not-a"))
val_a = Predicate(add_val_prefix(a.name))

assert val_predicates(~a) == {val_not_a, val_a}


def test_val_predicates_visitor_before():
"""Test the val predicates visitor for the before formula."""
a = PLTLAtomic("a")
before_a = Before(a)

val_before_a = Predicate(add_val_prefix("Ya"))
val_a = Predicate(add_val_prefix(a.name))

assert val_predicates(before_a) == {val_before_a, val_a}


def test_val_predicates_visitor_since():
"""Test the val predicates visitor for the before formula."""
a = PLTLAtomic("a")
b = PLTLAtomic("b")
c = PLTLAtomic("c")
a_since_b_since_c = Since(a, b, c)

val_a_since_b_since_c = Predicate(add_val_prefix("a-S-b-S-c"))
val_b_since_c = Predicate(add_val_prefix("b-S-c"))
val_a = Predicate(add_val_prefix(a.name))
val_b = Predicate(add_val_prefix(b.name))
val_c = Predicate(add_val_prefix(c.name))

assert val_predicates(a_since_b_since_c) == {
val_a_since_b_since_c,
val_b_since_c,
val_a,
val_b,
val_c,
}


def test_val_predicates_visitor_once():
"""Test the val predicates visitor for the once formula."""
a = PLTLAtomic("a")
once_a = Once(a)

val_once_a = Predicate(add_val_prefix("Oa"))
val_a = Predicate(add_val_prefix(a.name))

assert val_predicates(once_a) == {val_once_a, val_a}

0 comments on commit 8f7bfbc

Please sign in to comment.