Skip to content

Commit

Permalink
adding support for non public constructors for issue #25
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Mar 30, 2017
1 parent 61a5176 commit 68359bd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 34 deletions.
5 changes: 5 additions & 0 deletions src/SimpleFixture/DefaultFixtureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ private void SetupDefaults()
/// Configure how to handle circular references
/// </summary>
public CircularReferenceHandlingAlgorithm CircularReferenceHandling { get; set; }

/// <summary>
/// Use non pubic constructors, false by default
/// </summary>
public bool UseNonPublicConstructors { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/SimpleFixture/IFixtureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@ public interface IFixtureConfiguration : IGContainer
/// How to handle circular references
/// </summary>
CircularReferenceHandlingAlgorithm CircularReferenceHandling { get; }

/// <summary>
/// Use non pubic constructors, false by default
/// </summary>
bool UseNonPublicConstructors { get; }
}
}
33 changes: 28 additions & 5 deletions src/SimpleFixture/Impl/ConstructorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace SimpleFixture.Impl
{
public interface IConstructorSelector
{
ConstructorInfo SelectConstructor(Type type);
ConstructorInfo SelectConstructor(DataRequest request, Type type);
}

public class ConstructorSelector : IConstructorSelector
{
public ConstructorInfo SelectConstructor(Type type)

public ConstructorInfo SelectConstructor(DataRequest request, Type type)
{
var constructors = new List<ConstructorInfo>();

Expand All @@ -22,8 +23,29 @@ public ConstructorInfo SelectConstructor(Type type)
.DeclaredConstructors
.Where(c => c.IsPublic && !c.IsStatic));

var constructor = PickConstructorInfo(allConstructors, maxParameters, constructors);

if (constructor != null)
{
return constructor;
}

if (request.Fixture.Configuration.UseNonPublicConstructors)
{
allConstructors.AddRange(type.GetTypeInfo()
.DeclaredConstructors
.Where(c => !c.IsPublic && !c.IsStatic));

return PickConstructorInfo(allConstructors, maxParameters, constructors);
}

return null;
}

private static ConstructorInfo PickConstructorInfo(List<ConstructorInfo> allConstructors, int maxParameters, List<ConstructorInfo> constructors)
{
allConstructors.Sort((x, y) => Comparer<int>.Default.Compare(y.GetParameters().Length,
x.GetParameters().Length));
x.GetParameters().Length));


foreach (var info in allConstructors)
Expand All @@ -43,8 +65,9 @@ public ConstructorInfo SelectConstructor(Type type)
}

constructors.Sort(
(x, y) => Comparer<int>.Default.Compare(y.GetParameters().Count(p => !p.ParameterType.GetTypeInfo().IsPrimitive),
x.GetParameters().Count(p => !p.ParameterType.GetTypeInfo().IsPrimitive)));
(x, y) =>
Comparer<int>.Default.Compare(y.GetParameters().Count(p => !p.ParameterType.GetTypeInfo().IsPrimitive),
x.GetParameters().Count(p => !p.ParameterType.GetTypeInfo().IsPrimitive)));

return constructors.FirstOrDefault();
}
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleFixture/Impl/TypeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public object CreateType(DataRequest request, ComplexModel model)
return model.New(request);
}

var constructorInfo = _selector.SelectConstructor(request.RequestedType);
var constructorInfo = _selector.SelectConstructor(request, request.RequestedType);

if (constructorInfo == null)
{
Expand Down
21 changes: 16 additions & 5 deletions tests/SimpleFixture.Tests/FixtureTests/StandardFixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private PrivateClass()

}
}

[Fact]
public void Fixture_Throws_Exception_When_No_Constructor_Found()
{
Expand All @@ -23,6 +23,17 @@ public void Fixture_Throws_Exception_When_No_Constructor_Found()
Assert.Throws<Exception>(() => fixture.Generate<PrivateClass>());
}

[Fact]
public void Fixture_UseNonPublicConstructor()
{
var fixture = new Fixture(new DefaultFixtureConfiguration { UseNonPublicConstructors = true });

var instance = fixture.Generate<PrivateClass>();

Assert.NotNull(instance);
Assert.IsType<PrivateClass>(instance);
}

public struct MyStruct
{
public int IntValue { get; set; }
Expand All @@ -48,9 +59,9 @@ public void Fixture_Null_Reference_Test()
Assert.Throws<ArgumentNullException>(() => fixture.Generate((Type)null));
Assert.Throws<ArgumentNullException>(() => fixture.Locate((Type)null));
Assert.Throws<ArgumentNullException>(() => fixture.Populate(null));
Assert.Throws<ArgumentNullException>(() => fixture.Return((Func<DataRequest, ISomeInterface>) null));
Assert.Throws<ArgumentNullException>(() => fixture.Return((Func<DataRequest, ISomeInterface>)null));
Assert.Throws<ArgumentNullException>(() => fixture.ReturnIEnumerable((ISomeInterface[])null));
Assert.Throws<ArgumentNullException>(() => fixture.Add((IConvention) null));
Assert.Throws<ArgumentNullException>(() => fixture.Add((IConvention)null));
Assert.Throws<ArgumentNullException>(() => fixture.Add((IFixtureCustomization)null));
}

Expand All @@ -59,9 +70,9 @@ public void Fixture_IEnumerable()
{
var fixture = new Fixture();

foreach (var variable in (IEnumerable) fixture)
foreach (var variable in (IEnumerable)fixture)
{

}
}
}
Expand Down
46 changes: 23 additions & 23 deletions tests/SimpleFixture.Tests/project.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2719,7 +2719,7 @@
"lib/netstandard1.1/xunit.runner.utility.dotnet.dll": {}
}
},
"SimpleFixture/2.0.2": {
"SimpleFixture/2.0.3": {
"type": "project",
"framework": ".NETStandard,Version=v1.0",
"dependencies": {
Expand All @@ -2738,13 +2738,13 @@
"netstandard1.0/SimpleFixture.dll": {}
}
},
"SimpleFixture.FakeItEasy/2.0.2": {
"SimpleFixture.FakeItEasy/2.0.3": {
"type": "project",
"framework": ".NETStandard,Version=v1.6",
"dependencies": {
"FakeItEasy": "3.0.0",
"NETStandard.Library": "1.6.0",
"SimpleFixture": "2.0.2"
"SimpleFixture": "2.0.3"
},
"compile": {
"netstandard1.6/SimpleFixture.FakeItEasy.dll": {}
Expand All @@ -2753,12 +2753,12 @@
"netstandard1.6/SimpleFixture.FakeItEasy.dll": {}
}
},
"SimpleFixture.Moq/2.0.2": {
"SimpleFixture.Moq/2.0.3": {
"type": "project",
"framework": ".NETStandard,Version=v1.3",
"dependencies": {
"Moq": "4.7.0",
"SimpleFixture": "2.0.2"
"SimpleFixture": "2.0.3"
},
"compile": {
"netstandard1.3/SimpleFixture.Moq.dll": {}
Expand All @@ -2767,12 +2767,12 @@
"netstandard1.3/SimpleFixture.Moq.dll": {}
}
},
"SimpleFixture.NSubstitute/2.0.2": {
"SimpleFixture.NSubstitute/2.0.3": {
"type": "project",
"framework": ".NETStandard,Version=v1.5",
"dependencies": {
"NSubstitute": "2.0.2",
"SimpleFixture": "2.0.2"
"SimpleFixture": "2.0.3"
},
"compile": {
"netstandard1.5/SimpleFixture.NSubstitute.dll": {}
Expand Down Expand Up @@ -3589,7 +3589,7 @@
"lib/net45/xunit.runner.utility.desktop.dll": {}
}
},
"SimpleFixture/2.0.2": {
"SimpleFixture/2.0.3": {
"type": "project",
"framework": ".NETFramework,Version=v4.5",
"frameworkAssemblies": [
Expand All @@ -3606,13 +3606,13 @@
"net45/SimpleFixture.dll": {}
}
},
"SimpleFixture.FakeItEasy/2.0.2": {
"SimpleFixture.FakeItEasy/2.0.3": {
"type": "project",
"framework": ".NETFramework,Version=v4.5",
"dependencies": {
"FakeItEasy": "3.0.0",
"NETStandard.Library": "1.6.0",
"SimpleFixture": "2.0.2"
"SimpleFixture": "2.0.3"
},
"frameworkAssemblies": [
"System.Data",
Expand All @@ -3628,12 +3628,12 @@
"net45/SimpleFixture.FakeItEasy.dll": {}
}
},
"SimpleFixture.Moq/2.0.2": {
"SimpleFixture.Moq/2.0.3": {
"type": "project",
"framework": ".NETFramework,Version=v4.5",
"dependencies": {
"Moq": "4.7.0",
"SimpleFixture": "2.0.2"
"SimpleFixture": "2.0.3"
},
"frameworkAssemblies": [
"System.Data",
Expand All @@ -3649,12 +3649,12 @@
"net45/SimpleFixture.Moq.dll": {}
}
},
"SimpleFixture.NSubstitute/2.0.2": {
"SimpleFixture.NSubstitute/2.0.3": {
"type": "project",
"framework": ".NETFramework,Version=v4.5",
"dependencies": {
"NSubstitute": "2.0.2",
"SimpleFixture": "2.0.2"
"SimpleFixture": "2.0.3"
},
"frameworkAssemblies": [
"System.Data",
Expand Down Expand Up @@ -9127,22 +9127,22 @@
"xunit.runner.utility.nuspec"
]
},
"SimpleFixture/2.0.2": {
"SimpleFixture/2.0.3": {
"type": "project",
"path": "../../src/SimpleFixture/project.json",
"msbuildProject": "../../src/SimpleFixture/SimpleFixture.xproj"
},
"SimpleFixture.FakeItEasy/2.0.2": {
"SimpleFixture.FakeItEasy/2.0.3": {
"type": "project",
"path": "../../src/SimpleFixture.FakeItEasy/project.json",
"msbuildProject": "../../src/SimpleFixture.FakeItEasy/SimpleFixture.FakeItEasy.xproj"
},
"SimpleFixture.Moq/2.0.2": {
"SimpleFixture.Moq/2.0.3": {
"type": "project",
"path": "../../src/SimpleFixture.Moq/project.json",
"msbuildProject": "../../src/SimpleFixture.Moq/SimpleFixture.Moq.xproj"
},
"SimpleFixture.NSubstitute/2.0.2": {
"SimpleFixture.NSubstitute/2.0.3": {
"type": "project",
"path": "../../src/SimpleFixture.NSubstitute/project.json",
"msbuildProject": "../../src/SimpleFixture.NSubstitute/SimpleFixture.NSubstitute.xproj"
Expand All @@ -9157,11 +9157,11 @@
"": [
"FluentAssertions >= 4.18.0",
"NETStandard.Library >= 1.6.0",
"SimpleFixture >= 2.0.2-*",
"SimpleFixture.FakeItEasy >= 2.0.2-*",
"SimpleFixture.Moq >= 2.0.2-*",
"SimpleFixture.NSubstitute >= 2.0.2-*",
"SimpleFixture.xUnit >= 2.0.2-*",
"SimpleFixture >= 2.0.3-*",
"SimpleFixture.FakeItEasy >= 2.0.3-*",
"SimpleFixture.Moq >= 2.0.3-*",
"SimpleFixture.NSubstitute >= 2.0.3-*",
"SimpleFixture.xUnit >= 2.0.3-*",
"dotnet-test-xunit >= 2.2.0-preview2-build1029",
"xunit >= 2.2.0"
],
Expand Down

0 comments on commit 68359bd

Please sign in to comment.