Skip to content

Commit

Permalink
Add an assertion for equivalence
Browse files Browse the repository at this point in the history
  • Loading branch information
teiesti committed Mar 21, 2024
1 parent 96f728c commit 3eeeafa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions clintest/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,33 @@ def __repr__(self):

def holds_for(self, model: Model) -> bool:
return not self.__antecedent.holds_for(model) or self.__consequent.holds_for(model)


class Equivalent(Assertion):
"""
The equivalence of a list of given assertions.
This assertion holds if all `args` simultaneously hold or not hold.
Parameters
----------
args
The `Assertion`s to be combined.
"""

def __init__(self, *args: Assertion) -> None:
self.__operands = args

def __repr__(self):
name = self.__class__.__name__
operands = ", ".join(repr(operand) for operand in self.__operands)
return f"{name}({operands})"

def holds_for(self, model: Model) -> bool:
operands = iter(self.__operands)

try:
first = next(operands).holds_for(model)
except StopIteration:
return True

return all((first == operand.holds_for(model) for operand in operands))
15 changes: 15 additions & 0 deletions tests/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ def test_implies(frame):
], [
Implies(True_(), False_()),
])

def test_equivalent(frame):
from clintest.assertion import Equivalent, True_, False_
frame(
[
Equivalent(),
Equivalent(False_()),
Equivalent(True_()),
Equivalent(False_(), False_()),
Equivalent(True_(), True_()),
], [
Equivalent(False_(), True_()),
Equivalent(True_(), False_()),
]
)

0 comments on commit 3eeeafa

Please sign in to comment.