From e138b5cc72e7b7e05d7d14497e093802d1265112 Mon Sep 17 00:00:00 2001 From: Artur Lavrov Date: Sat, 27 Feb 2021 14:50:26 +0200 Subject: [PATCH] start working on #23 added overload with custom contract violation message --- .../Precondition/RequireTest.cs | 10 +++ .../Precondition/Require/That.cs | 89 +++++++++++++++++-- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/DeclarativeContracts.Tests/Precondition/RequireTest.cs b/src/DeclarativeContracts.Tests/Precondition/RequireTest.cs index 538128d..b00f3ee 100644 --- a/src/DeclarativeContracts.Tests/Precondition/RequireTest.cs +++ b/src/DeclarativeContracts.Tests/Precondition/RequireTest.cs @@ -189,6 +189,16 @@ public void That_FuncThatReturnsFalse_ContractViolationExceptionWasThrown() Assert.Throws(typeof(ContractViolationException), () => Require.That(functor)); } + + [Test] + public void That_FuncThatReturnsFalse_ContractViolationExceptionWasThrownWithCustomUserDefinedMessage() + { + Func functor = () => false; + + string contractViolationCustomMessage = "functor return false"; + + Assert.Throws(typeof(ContractViolationException), () => Require.That(functor, contractViolationCustomMessage), contractViolationCustomMessage); + } [Test] public void That_FuncIsNull_ArgumentExceptionWasThrown() diff --git a/src/DeclarativeContracts/Precondition/Require/That.cs b/src/DeclarativeContracts/Precondition/Require/That.cs index 603ce96..35d1788 100644 --- a/src/DeclarativeContracts/Precondition/Require/That.cs +++ b/src/DeclarativeContracts/Precondition/Require/That.cs @@ -27,6 +27,25 @@ public static void That(TEntity entity, Func InternalThat(member, predicate); } + /// + /// Method that verify that entity member satisfy passed predicate + /// + /// Entity that contains verifiable member + /// Func that returns entity member that needs to be checked + /// Predicate that check is entity member satisfy a condition + /// Custom text that will be throw as an exception text + /// Entity type + /// Entity member type + /// Throws ContractViolationException if element does not satisfy predicate + /// Throws ArgumentException if entityMemberSelector or predicate is null + public static void That(TEntity entity, Func entityMemberSelector, Predicate predicate, string contractViolationMsg) + { + ArgumentChecker.CheckArgumentsNull(entityMemberSelector, predicate); + + var member = entityMemberSelector(entity); + InternalThat(member, predicate, contractViolationMsg); + } + /// /// Method that verify that entity member satisfy passed predicate /// @@ -42,6 +61,22 @@ public static void That(TElement element, Predicate predicat InternalThat(element, predicate); } + /// + /// Method that verify that entity member satisfy passed predicate + /// + /// Element that needs to be checked + /// Predicated that checks member + /// Custom text that will be throw as an exception text + /// Element type + /// Throws ContractViolationException if element does not satisfy predicate + /// Throws ArgumentException if predicate is null + public static void That(TElement element, Predicate predicate, string contractViolationMsg) + { + ArgumentChecker.CheckArgumentsNull(predicate); + + InternalThat(element, predicate, contractViolationMsg); + } + /// /// Method that verify that entity member satisfy passed predicate /// @@ -58,7 +93,7 @@ public static void That(TElement element, Predicate /// Method that verify that execute predicate returns true /// @@ -84,6 +119,32 @@ public static void That(Func predicate) } } + /// + /// Method that verify that execute predicate returns true + /// + /// Predicate that should return true + /// + /// Throws ContractViolationException if element does not satisfy predicate + /// Throws ArgumentException if predicate is null + public static void That(Func predicate, string contractViolationMsg) + { + ArgumentChecker.CheckArgumentsNull(predicate); + + try + { + var result = predicate.Invoke(); + if(!result){ + throw new ContractViolationException(contractViolationMsg); + } + } + catch (Exception exception) + { + throw new ContractViolationException( + contractViolationMsg, + innerException: exception); + } + } + private static void InternalThat(TMember member, Predicate predicate, Exception exceptionToThrow = null) { bool predicateResult; @@ -95,15 +156,33 @@ private static void InternalThat(TMember member, Predicate pre catch(Exception ex) { throw exceptionToThrow ?? - new ContractViolationException( - "Exception occured during predicate execution.See inner exception for details", ex); + new ContractViolationException("Exception occured during predicate execution.See inner exception for details", ex); } if(!predicateResult) { throw exceptionToThrow ?? - new ContractViolationException( - "Contact precondition was violated. Expected that predicate returns true."); + new ContractViolationException("Contact precondition was violated. Expected that predicate returns true."); + } + } + + private static void InternalThat(TMember member, Predicate predicate, string contractViolationMsg) + { + bool predicateResult; + + try + { + predicateResult = predicate.Invoke(member); + } + catch(Exception ex) + { + throw new ContractViolationException( + contractViolationMsg ?? "Exception occured during predicate execution.See inner exception for details", ex); + } + + if(!predicateResult) + { + throw new ContractViolationException(contractViolationMsg ?? "Contact precondition was violated. Expected that predicate returns true."); } } }