Skip to content

Commit

Permalink
Add a Context test
Browse files Browse the repository at this point in the history
  • Loading branch information
teiesti committed Sep 2, 2024
1 parent 5b853fe commit 3f8b401
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions clintest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,52 @@ def outcome(self) -> Outcome:
return self.test.outcome()


class Context(Test):
"""
A test that behaves identical to a given other `test` but permits changes to its
string representation. This can be helpful to create human-readable error messages.
Parameters
----------
test
A `Test` that determines how this test should behave.
"""

def __init__(
self,
test: Test,
str: Callable[[Test], str] = lambda test: str(test),
repr: Callable[[Test], str] = lambda test: repr(test),
):
self.test: Test = test
self.__str = str
self.__repr = repr

def __repr__(self):
return self.__repr(self.test)

def __str__(self):
return self.__str(self.test)

def on_model(self, model: Model) -> bool:
return self.test.on_model(model)

def on_unsat(self, lower_bound: Sequence[int]) -> None:
self.test.on_unsat(lower_bound)

def on_core(self, core: Sequence[int]) -> None:
self.test.on_core(core)

def on_statistics(self, step: StatisticsMap, accumulated: StatisticsMap) -> None:
self.test.on_statistics(step, accumulated)

def on_finish(self, result: SolveResult) -> None:
self.test.on_finish(result)

def outcome(self) -> Outcome:
return self.test.outcome()


class Assert(Test):
"""
A test that asserts certain properties about the `clingo.model.Model`s of a program. This test
Expand Down

0 comments on commit 3f8b401

Please sign in to comment.