Skip to content

Commit

Permalink
Add ToString() for fluent setup API objects (#812)
Browse files Browse the repository at this point in the history
Issue 810:
* SetupPhrase does not return Expression in ToString()
* Fixing ToString() on SetupPhrase to return descriptive Expression
* Updating Changelog for ToString() fix for SetupPhrase
* SetupSequencePhrase does not return Expression in ToString()
* Fixing ToString() on SetupSequencePhrase to return descriptive Expression
* Removing Phrase from variable names
* Revert "Updating Changelog for ToString() fix for SetupPhrase"
  • Loading branch information
jacob-ewald authored and stakx committed Apr 18, 2019
1 parent c3def4d commit 3d7ac8c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Moq/Language/Flow/SetupPhrase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,10 @@ public void Verifiable(string failMessage)
{
this.setup.Verifiable(failMessage);
}

public override string ToString()
{
return setup.Expression.ToStringFixed();
}
}
}
10 changes: 10 additions & 0 deletions src/Moq/Language/Flow/SetupSequencePhrase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public ISetupSequentialAction Throws(Exception exception)
this.setup.AddThrows(exception);
return this;
}

public override string ToString()
{
return setup.Expression.ToStringFixed();
}
}

internal sealed class SetupSequencePhrase<TResult> : ISetupSequentialResult<TResult>
Expand Down Expand Up @@ -86,5 +91,10 @@ public ISetupSequentialResult<TResult> Throws(Exception exception)
public ISetupSequentialResult<TResult> Throws<TException>()
where TException : Exception, new()
=> this.Throws(new TException());

public override string ToString()
{
return setup.Expression.ToStringFixed();
}
}
}
43 changes: 43 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,47 @@ public void InvokeApplyRule(object obj)
}
}

#endregion

#region #810

public class _810
{
[Fact]
public void VoidSetupPhraseConvertsExpressionToDescriptiveString()
{
var voidSetup = new Mock<IFoo>().Setup(x => x.DoThings(null));

Assert.Equal("x => x.DoThings(null)", voidSetup.ToString());
}

[Fact]
public void NonVoidSetupPhraseConvertsExpressionToDescriptiveString()
{
var nonVoidSetup = new Mock<IFoo>().Setup(x => x.Property1);

Assert.Equal("x => x.Property1", nonVoidSetup.ToString());
}

[Fact]
public void SetterSetupPhraseConvertsExpressionToDescriptiveString()
{
var setterSetup = new Mock<IFoo>().SetupSet<object>(x => x.Property1 = null);

Assert.Equal("x => x.Property1 = null", setterSetup.ToString());
}

[Fact]
public void SetupSequencePhraseConvertsExpressionToDescriptiveString()
{
var setupSequence = new Mock<IFoo>().SetupSequence(x => x.DoThings(null));
var setupGenericSequence = new Mock<IFoo>().SetupSequence(x => x.DoThings<object>(null));

Assert.Equal("x => x.DoThings(null)", setupSequence.ToString());
Assert.Equal("x => x.DoThings<object>(null)", setupGenericSequence.ToString());
}
}

#endregion

// Old @ Google Code
Expand Down Expand Up @@ -2566,6 +2607,8 @@ public void CreatesMockWithGenericsConstraints()
public interface IFoo
{
void DoThings(object arg);
T DoThings<T>(object arg);
object Property1 { get; set; }
}

[Fact]
Expand Down

0 comments on commit 3d7ac8c

Please sign in to comment.