Skip to content

Commit

Permalink
start working on #23
Browse files Browse the repository at this point in the history
added overload with custom contract violation message
  • Loading branch information
ArturLavrov committed Feb 27, 2021
1 parent ec3ec19 commit e138b5c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/DeclarativeContracts.Tests/Precondition/RequireTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ public void That_FuncThatReturnsFalse_ContractViolationExceptionWasThrown()

Assert.Throws(typeof(ContractViolationException), () => Require.That(functor));
}

[Test]
public void That_FuncThatReturnsFalse_ContractViolationExceptionWasThrownWithCustomUserDefinedMessage()
{
Func<bool> functor = () => false;

string contractViolationCustomMessage = "functor return false";

Assert.Throws(typeof(ContractViolationException), () => Require.That(functor, contractViolationCustomMessage), contractViolationCustomMessage);
}

[Test]
public void That_FuncIsNull_ArgumentExceptionWasThrown()
Expand Down
89 changes: 84 additions & 5 deletions src/DeclarativeContracts/Precondition/Require/That.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ public static void That<TEntity, TMember>(TEntity entity, Func<TEntity, TMember>
InternalThat(member, predicate);
}

/// <summary>
/// Method that verify that entity member satisfy passed predicate
/// </summary>
/// <param name="entity">Entity that contains verifiable member</param>
/// <param name="entityMemberSelector">Func that returns entity member that needs to be checked</param>
/// <param name="predicate">Predicate that check is entity member satisfy a condition</param>
/// <param name="contractViolationMsg">Custom text that will be throw as an exception text</param>
/// <typeparam name="TEntity">Entity type</typeparam>
/// <typeparam name="TMember">Entity member type</typeparam>
/// <throws>Throws ContractViolationException if element does not satisfy predicate</throws>
/// <throws>Throws ArgumentException if entityMemberSelector or predicate is null</throws>
public static void That<TEntity, TMember>(TEntity entity, Func<TEntity, TMember> entityMemberSelector, Predicate<TMember> predicate, string contractViolationMsg)
{
ArgumentChecker.CheckArgumentsNull(entityMemberSelector, predicate);

var member = entityMemberSelector(entity);
InternalThat(member, predicate, contractViolationMsg);
}

/// <summary>
/// Method that verify that entity member satisfy passed predicate
/// </summary>
Expand All @@ -42,6 +61,22 @@ public static void That<TElement>(TElement element, Predicate<TElement> predicat
InternalThat(element, predicate);
}

/// <summary>
/// Method that verify that entity member satisfy passed predicate
/// </summary>
/// <param name="element">Element that needs to be checked</param>
/// <param name="predicate">Predicated that checks member</param>
/// <param name="contractViolationMsg">Custom text that will be throw as an exception text</param>
/// <typeparam name="TElement">Element type</typeparam>
/// <throws>Throws ContractViolationException if element does not satisfy predicate</throws>
/// <throws>Throws ArgumentException if predicate is null</throws>
public static void That<TElement>(TElement element, Predicate<TElement> predicate, string contractViolationMsg)
{
ArgumentChecker.CheckArgumentsNull(predicate);

InternalThat(element, predicate, contractViolationMsg);
}

/// <summary>
/// Method that verify that entity member satisfy passed predicate
/// </summary>
Expand All @@ -58,7 +93,7 @@ public static void That<TElement, TException>(TElement element, Predicate<TEleme

InternalThat(element, predicate, exceptionToThrow);
}

/// <summary>
/// Method that verify that execute predicate returns true
/// </summary>
Expand All @@ -84,6 +119,32 @@ public static void That(Func<bool> predicate)
}
}

/// <summary>
/// Method that verify that execute predicate returns true
/// </summary>
/// <param name="predicate">Predicate that should return true</param>
/// <param name="contractViolationMsg"></param>
/// <throws>Throws ContractViolationException if element does not satisfy predicate</throws>
/// <throws>Throws ArgumentException if predicate is null</throws>
public static void That(Func<bool> 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>(TMember member, Predicate<TMember> predicate, Exception exceptionToThrow = null)
{
bool predicateResult;
Expand All @@ -95,15 +156,33 @@ private static void InternalThat<TMember>(TMember member, Predicate<TMember> 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>(TMember member, Predicate<TMember> 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.");
}
}
}
Expand Down

0 comments on commit e138b5c

Please sign in to comment.