Skip to content

Commit

Permalink
Remove some code repetition from reification tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
namcsi committed Jun 19, 2023
1 parent 3981f29 commit da9f0bc
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions tests/test_reify.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class TestReifyReflect(TestCase):
default_ast_facts = []
base_str = ""

def get_test_facts(self, fact_file_str: str):
"""Parse fact file from test directory."""
facts = parse_fact_files(
[str(test_reify_files / fact_file_str)],
unifier=preds.AST_Facts)
return facts

def assertReifyReflectEqual(self,
prog_str: str,
ast_facts: FactBase):
Expand All @@ -41,8 +48,9 @@ def assertReifyReflectEqual(self,
self.assertEqual(rast.program_string, expected_string)


class TestReifyReflectSingleNormalRule(TestReifyReflect):
"""Test cases for programs containing only a single normal rule."""
class TestReifyReflectSimplePrograms(TestReifyReflect):
"""Test cases for simple programs containing only a couple
statements."""

base_str = "#program base.\n"

Expand All @@ -59,72 +67,56 @@ def test_add_ast_facts(self):
def test_reify_program_prop_fact(self):
"""Test reification of a propositional fact."""
prog_str = "a."
facts = parse_fact_files(
[str(test_reify_files / "prop_fact.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("prop_fact.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_prop_normal_rule(self):
"""
Test reification of a normal rule containing only propositional atoms.
"""
prog_str = "a :- b; not c."
facts = parse_fact_files(
[str(test_reify_files / "prop_normal_rule.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("prop_normal_rule.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_function(self):
"""
Test reification of a variable-free normal rule with function symbols.
"""
prog_str = "rel(2,1) :- rel(1,2)."
facts = parse_fact_files(
[str(test_reify_files / "function.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("function.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_nested_function(self):
prog_str = "next(move(a))."
facts = parse_fact_files(
[str(test_reify_files / "nested_function.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("nested_function.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_variable(self):
"""
Test reification of normal rule with variables.
"""
prog_str = "rel(Y,X) :- rel(X,Y)."
facts = parse_fact_files(
[str(test_reify_files / "variable.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("variable.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_string(self):
"""
Test reification of normal rule with string.
"""
prog_str = 'yummy("carrot").'
facts = parse_fact_files(
[str(test_reify_files / "string.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("string.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_constant_term(self):
"""
Test reification of normal rule with constant term.
"""
prog_str = "good(human)."
facts = parse_fact_files(
[str(test_reify_files / "constant.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("constant.lp")
self.assertReifyReflectEqual(prog_str, facts)

def test_reify_program_binary_operator(self):
prog_str = "equal((1+1),2)."
facts = parse_fact_files(
[str(test_reify_files / "binary_operation.lp")],
unifier=preds.AST_Facts)
facts = self.get_test_facts("binary_operation.lp")
self.assertReifyReflectEqual(prog_str, facts)

0 comments on commit da9f0bc

Please sign in to comment.