Skip to content

Commit

Permalink
refactor check, mark python/mypy#14622 bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
magwas committed Mar 1, 2023
1 parent 13ea527 commit 4b2106c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
39 changes: 18 additions & 21 deletions src/shall/Check.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@

from shall.ShallEntity import ShallEntity, P, R
from injector import inject
from src.shall.CheckReturnConstraints import CheckReturnConstraints
from src.shall.CheckReturnValue import CheckReturnValue
from src.shall.CheckSideEffects import CheckSideEffects

class Check():
@inject
def __init__(self,
checkReturnValue:CheckReturnValue[P,R],
checkReturnConstraints: CheckReturnConstraints[P,R],
checkSideEffects: CheckSideEffects[P,R]
) -> None:
self.checkReturnValue = checkReturnValue
self.checkReturnConstraints = checkReturnConstraints
self.checkSideEffects = checkSideEffects

class Check(ShallEntity[P, R]):
def check(self) -> None:
paramlist = self.parameters[0]
paramkwargs = self.parameters[1]
print(self.explanation)
callResult = self.callable(*paramlist, **paramkwargs)
if self.returnValue == callResult:
print("/return value PASSED")
else:
raise AssertionError(self.explanation +
" did return " + str(callResult) + " instead of " + str(self.returnValue))
for (explanation, checker) in self.returnConstraints:
checked = checker(self.returnValue, *paramlist, **paramkwargs)
if checked:
print("+"+explanation+": PASSED")
else:
raise AssertionError(explanation+" did not hold")
for (explanation, sideEffectChecker) in self.sideEffectCheckers:
sideEffectChecker.setUp(self.callable, *paramlist, **paramkwargs)
result = sideEffectChecker.runTest()
sideEffectChecker.tearDown()
if result:
print("-"+explanation+": PASSED")
else:
raise AssertionError(
explanation+" did not hold: " + str(result))
self.checkReturnValue.checkReturnValue(callResult)
self.checkReturnConstraints.checkReturnConstraints( paramlist, paramkwargs)
self.checkSideEffects.checkSideEffects(paramlist,paramkwargs)
11 changes: 11 additions & 0 deletions src/shall/CheckReturnConstraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from src.shall.ShallEntity import ShallEntity, P, R


class CheckReturnConstraints(ShallEntity[P, R]):
def checkReturnConstraints(self, paramlist:P.args, paramkwargs: P.kwargs) -> None: #type:ignore # https://github.com/python/mypy/issues/14622
for (explanation, checker) in self.returnConstraints:
checked = checker(self.returnValue, *paramlist, **paramkwargs)
if checked:
print("+"+explanation+": PASSED")
else:
raise AssertionError(explanation+" did not hold")
10 changes: 10 additions & 0 deletions src/shall/CheckReturnValue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.shall.ShallEntity import ShallEntity, P, R


class CheckReturnValue(ShallEntity[P, R]):
def checkReturnValue(self, callResult: R) ->None:
if self.returnValue == callResult:
print("/return value PASSED")
else:
raise AssertionError(self.explanation +
" did return " + str(callResult) + " instead of " + str(self.returnValue))
14 changes: 14 additions & 0 deletions src/shall/CheckSideEffects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from src.shall.ShallEntity import ShallEntity, P, R


class CheckSideEffects(ShallEntity[P, R]):
def checkSideEffects(self, paramlist:P.args, paramkwargs: P.kwargs) ->None: #type:ignore #https://github.com/python/mypy/issues/14622
for (explanation, sideEffectChecker) in self.sideEffectCheckers:
sideEffectChecker.setUp(self.callable, *paramlist, **paramkwargs)
result = sideEffectChecker.runTest()
sideEffectChecker.tearDown()
if result:
print("-"+explanation+": PASSED")
else:
raise AssertionError(
explanation+" did not hold: " + str(result))
4 changes: 3 additions & 1 deletion test/testcontract.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import sys
from unittest import TestCase
from CheckContract import CheckContract

from injector import Inject
from ExampleContract import ExampleContract
from ThenreturnContract import ThenreturnContract
from WhenContract import WhenContract
from ShallConstructorContract import ShallConstructorContract
from IfCalledWithContract import IfCalledWithContract
from MeanWhileContract import MeanWhileContract
from SuchThatContract import SuchThatContract
from src.shall.Check import CheckReturnValue

inject = Inject([CheckReturnValue])

class testContract(TestCase):

Expand Down

0 comments on commit 4b2106c

Please sign in to comment.