Skip to content

Commit

Permalink
Apply Try pattern to verification methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Mar 28, 2020
1 parent db0f208 commit 37053f1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/Moq/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@ public void SetThrowExceptionResponse(Exception exception)
this.returnOrThrowResponse = new ThrowExceptionResponse(exception);
}

protected override MockException TryVerifySelf()
protected override bool TryVerifySelf(out MockException error)
{
return (this.flags & Flags.Invoked) != 0 ? null : MockException.UnmatchedSetup(this);
error = (this.flags & Flags.Invoked) != 0 ? null : MockException.UnmatchedSetup(this);
return error == null;
}

public override void Uninvoke()
Expand Down
16 changes: 8 additions & 8 deletions src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,13 @@ public void VerifyAll()

private void Verify(Func<Setup, bool> predicate)
{
var error = this.TryVerify(predicate);
if (error?.IsVerificationError == true)
if (!this.TryVerify(predicate, out var error) && error.IsVerificationError)
{
throw error;
}
}

internal MockException TryVerify(Func<Setup, bool> predicate)
internal bool TryVerify(Func<Setup, bool> predicate, out MockException error)
{
foreach (Invocation invocation in this.MutableInvocations)
{
Expand All @@ -308,22 +307,23 @@ internal MockException TryVerify(Func<Setup, bool> predicate)

foreach (var setup in this.Setups.ToArrayLive(_ => true))
{
var error = setup.TryVerify(predicate);
if (error?.IsVerificationError == true)
if (!setup.TryVerify(predicate, out var e) && e.IsVerificationError)
{
errors.Add(error);
errors.Add(e);
}
}

if (errors.Count > 0)
{
return MockException.Combined(
error = MockException.Combined(
errors,
preamble: string.Format(CultureInfo.CurrentCulture, Resources.VerificationErrorsOfMock, this));
return false;
}
else
{
return null;
error = null;
return true;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Moq/SequenceSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public override void Execute(Invocation invocation)
}
}

protected override MockException TryVerifySelf()
protected override bool TryVerifySelf(out MockException error)
{
return this.invoked ? null : MockException.UnmatchedSetup(this);
error = this.invoked ? null : MockException.UnmatchedSetup(this);
return error == null;
}

public override void Uninvoke()
Expand Down
27 changes: 16 additions & 11 deletions src/Moq/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,40 @@ public override string ToString()
return builder.ToString();
}

public MockException TryVerify(Func<Setup, bool> predicate)
public bool TryVerify(Func<Setup, bool> predicate, out MockException error)
{
if (predicate(this))
{
var error = this.TryVerifySelf();
if (error?.IsVerificationError == true)
if (!this.TryVerifySelf(out var e) && e.IsVerificationError)
{
return error;
error = e;
return false;
}
}

return this.TryVerifyInnerMock(predicate);
return this.TryVerifyInnerMock(predicate, out error);
}

protected virtual MockException TryVerifyInnerMock(Func<Setup, bool> predicate)
protected virtual bool TryVerifyInnerMock(Func<Setup, bool> predicate, out MockException error)
{
if (this.ReturnsInnerMock(out var innerMock))
{
var error = innerMock.TryVerify(predicate);
if (error?.IsVerificationError == true)
if (!innerMock.TryVerify(predicate, out var e) && e.IsVerificationError)
{
return MockException.FromInnerMockOf(this, error);
error = MockException.FromInnerMockOf(this, e);
return false;
}
}

return null;
error = null;
return true;
}

protected virtual MockException TryVerifySelf() => null;
protected virtual bool TryVerifySelf(out MockException error)
{
error = null;
return true;
}

public virtual void Uninvoke()
{
Expand Down

0 comments on commit 37053f1

Please sign in to comment.