Skip to content

Commit

Permalink
Add switch for collecting setup source file info
Browse files Browse the repository at this point in the history
Up until now, Moq has been collecting source file information for each
setups created. This comes at a slight performance penalty. At this
moment, there is a regression in the .NET Framework which means this
penalty becomes gigantic.

This commit turns source file information collection into an opt-in
feature. Towards that end, a `Switches` property is added to `Mock`
and `MockRepository` which allows users to opt in or out of certain
features. Source file info collection is controlled via the switch
`Switches.CollectDiagnosticFileInfoForSetups`.
  • Loading branch information
stakx committed Nov 6, 2017
1 parent 7a0f435 commit 2c2e0dd
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Source/AsInterface.cs
Expand Up @@ -95,6 +95,12 @@ public override TInterface Object
get { return this.owner.Object as TInterface; }
}

public override Switches Switches
{
get => this.owner.Switches;
set => this.owner.Switches = value;
}

public override Mock<TNewInterface> As<TNewInterface>()
{
return this.owner.As<TNewInterface>();
Expand Down
9 changes: 7 additions & 2 deletions Source/MethodCall.cs
Expand Up @@ -181,9 +181,14 @@ public Expression SetupExpression
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private void SetFileInfo()
{
#if !NETCORE
if ((this.Mock.Switches & Switches.CollectDiagnosticFileInfoForSetups) == 0)
{
return;
}

try
{
#if !NETCORE
var thisMethod = MethodBase.GetCurrentMethod();
var mockAssembly = Assembly.GetExecutingAssembly();
// Move 'till we're at the entry point into Moq API
Expand All @@ -199,12 +204,12 @@ private void SetFileInfo()
this.FileName = Path.GetFileName(frame.GetFileName());
this.TestMethod = frame.GetMethod();
}
#endif
}
catch
{
// Must NEVER fail, as this is a nice-to-have feature only.
}
#endif
}

public void SetOutParameters(ICallContext call)
Expand Down
12 changes: 12 additions & 0 deletions Source/Mock.cs
Expand Up @@ -60,12 +60,14 @@ public abstract partial class Mock : IFluentInterface
private bool isInitialized;
private DefaultValue defaultValue = DefaultValue.Empty;
private IDefaultValueProvider defaultValueProvider = new EmptyDefaultValueProvider();
private Switches switches;

/// <include file='Mock.xdoc' path='docs/doc[@for="Mock.ctor"]/*'/>
protected Mock()
{
this.ImplementedInterfaces = new List<Type>();
this.InnerMocks = new ConcurrentDictionary<MethodInfo, Mock>();
this.switches = Switches.Default;
}

/// <include file='Mock.xdoc' path='docs/doc[@for="Mock.Get"]/*'/>
Expand Down Expand Up @@ -228,6 +230,16 @@ internal IDefaultValueProvider DefaultValueProvider
/// </summary>
internal protected int InternallyImplementedInterfaceCount { get; protected set; }

/// <summary>
/// A set of switches that influence how this mock will operate.
/// You can opt in or out of certain features via this property.
/// </summary>
public virtual Switches Switches
{
get => this.switches;
set => this.switches = value;
}

#region Verify

/// <include file='Mock.xdoc' path='docs/doc[@for="Mock.Verify"]/*'/>
Expand Down
1 change: 1 addition & 0 deletions Source/MockDefaultValueProvider.cs
Expand Up @@ -71,6 +71,7 @@ public override object ProvideDefault(MethodInfo member)
Mock newMock = (Mock)Activator.CreateInstance(mockType, owner.Behavior);
newMock.DefaultValue = owner.DefaultValue;
newMock.CallBase = owner.CallBase;
newMock.Switches = owner.Switches;
return newMock;
});
}
Expand Down
13 changes: 13 additions & 0 deletions Source/Obsolete/MockFactory.cs
Expand Up @@ -137,6 +137,7 @@ public partial class MockFactory
{
List<Mock> mocks = new List<Mock>();
MockBehavior defaultBehavior;
private Switches switches;

/// <summary>
/// Initializes the factory with the given <paramref name="defaultBehavior"/>
Expand All @@ -148,6 +149,7 @@ public partial class MockFactory
public MockFactory(MockBehavior defaultBehavior)
{
this.defaultBehavior = defaultBehavior;
this.switches = Switches.Default;
}

/// <summary>
Expand All @@ -168,6 +170,16 @@ public MockFactory(MockBehavior defaultBehavior)
/// </summary>
protected internal IEnumerable<Mock> Mocks { get { return mocks; } }

/// <summary>
/// A set of switches that influence how mocks created by this factory will operate.
/// You can opt in or out of certain features via this property.
/// </summary>
public Switches Switches
{
get => this.switches;
set => this.switches = value;
}

/// <summary>
/// Creates a new mock with the default <see cref="MockBehavior"/>
/// specified at factory construction time.
Expand Down Expand Up @@ -292,6 +304,7 @@ protected virtual Mock<T> CreateMock<T>(MockBehavior behavior, object[] args)

mock.CallBase = this.CallBase;
mock.DefaultValue = this.DefaultValue;
mock.Switches = this.switches;

return mock;
}
Expand Down
63 changes: 63 additions & 0 deletions Source/Switches.cs
@@ -0,0 +1,63 @@
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD
//https://github.com/moq/moq4
//All rights reserved.

//Redistribution and use in source and binary forms,
//with or without modification, are permitted provided
//that the following conditions are met:

// * Redistributions of source code must retain the
// above copyright notice, this list of conditions and
// the following disclaimer.

// * Redistributions in binary form must reproduce
// the above copyright notice, this list of conditions
// and the following disclaimer in the documentation
// and/or other materials provided with the distribution.

// * Neither the name of Clarius Consulting, Manas Technology Solutions or InSTEDD nor the
// names of its contributors may be used to endorse
// or promote products derived from this software
// without specific prior written permission.

//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
//SUCH DAMAGE.

//[This is the BSD license, see
// http://www.opensource.org/licenses/bsd-license.php]

using System;

namespace Moq
{
/// <summary>
/// Represents a switch, or a combination of switches, that can be either enabled or disabled.
/// When set via <see cref="Mock.Switches"/> or <see cref="MockFactory.Switches"/>, they determine how a mock will operate.
/// </summary>
[Flags]
public enum Switches
{
/// <summary>
/// The default set of switches. The switches covered by this enumeration value may change between different versions of Moq.
/// </summary>
Default = 0,

/// <summary>
/// When enabled, specifies that source file information should be collected for each setup.
/// This results in more helpful error messages, but may affect performance.
/// </summary>
CollectDiagnosticFileInfoForSetups = 1 << 0,
}
}
11 changes: 11 additions & 0 deletions UnitTests/MockDefaultValueProviderFixture.cs
Expand Up @@ -102,6 +102,17 @@ public void DefaultValueIsNotChangedWhenPerformingInternalInvocation()
Assert.Equal(DefaultValue.Empty, mockBar.DefaultValue);
}

[Fact]
public void Inner_mocks_inherit_switches_of_parent_mock()
{
const Switches expectedSwitches = Switches.CollectDiagnosticFileInfoForSetups;

var parentMock = new Mock<IFoo>() { DefaultValue = DefaultValue.Mock, Switches = expectedSwitches };
var innerMock = Mock.Get(parentMock.Object.Bar);

Assert.Equal(expectedSwitches, actual: innerMock.Switches);
}

public interface IFoo
{
IBar Bar { get; set; }
Expand Down
8 changes: 8 additions & 0 deletions UnitTests/MockFixture.cs
Expand Up @@ -922,6 +922,14 @@ private static Foo MakeFoo(IMock<IBar> barMock)
return new Foo(barMock.Object);
}

[Fact]
public void Mock_initially_uses_default_switches()
{
var mock = new Mock<IFoo>();

Assert.Equal(Switches.Default, actual: mock.Switches);
}

public class Foo
{
public Foo() : this(new Bar()) { }
Expand Down
19 changes: 19 additions & 0 deletions UnitTests/MockRepositoryFixture.cs
Expand Up @@ -146,6 +146,25 @@ public void ShouldCreateMocksWithFactoryCallBase()
Assert.True(mock.Object.BaseCalled);
}

[Fact]
public void Repository_initially_uses_default_switches()
{
var repository = new MockRepository(MockBehavior.Default);

Assert.Equal(Switches.Default, actual: repository.Switches);
}

[Fact]
public void Mock_inherits_switches_from_repository()
{
const Switches expectedSwitches = Switches.CollectDiagnosticFileInfoForSetups;

var repository = new MockRepository(MockBehavior.Default) { Switches = expectedSwitches };
var mock = repository.Create<IFoo>();

Assert.Equal(expectedSwitches, actual: mock.Switches);
}

public interface IFoo
{
void Do();
Expand Down
30 changes: 30 additions & 0 deletions UnitTests/VerifyFixture.cs
Expand Up @@ -981,6 +981,36 @@ public void DoesNotThrowCollectionModifiedWhenMoreInvocationsInterceptedDuringVe
});
}

#if !NETCORE
[Fact]
public void Enabling_diagnostic_file_info_leads_to_that_information_in_verification_error_messages()
{
var repository = new MockRepository(MockBehavior.Default);
repository.Switches |= Switches.CollectDiagnosticFileInfoForSetups;

var mock = repository.Create<IFoo>();
mock.Setup(m => m.Submit());

var ex = Assert.Throws<MockException>(() => repository.VerifyAll());
Assert.Contains("in ", ex.Message);
Assert.Contains("VerifyFixture.cs: line ", ex.Message);
}
#endif

[Fact]
public void Disabling_diagnostic_file_info_leads_to_that_information_missing_in_verification_error_messages()
{
var repository = new MockRepository(MockBehavior.Default);
repository.Switches &= ~Switches.CollectDiagnosticFileInfoForSetups;

var mock = repository.Create<IFoo>();
mock.Setup(m => m.Submit());

var ex = Assert.Throws<MockException>(() => repository.VerifyAll());
Assert.DoesNotContain("in ", ex.Message);
Assert.DoesNotContain("VerifyFixture.cs: line ", ex.Message);
}

public interface IBar
{
int? Value { get; set; }
Expand Down

0 comments on commit 2c2e0dd

Please sign in to comment.