Skip to content

Commit

Permalink
Ignore parameters with [ValuesAttribute]; itroduce parametrized set o…
Browse files Browse the repository at this point in the history
…f values

See more info about NUnit parametrized tests here:
http://nunit.org/index.php?p=values&r=2.6.4
  • Loading branch information
sshushliapin committed Jun 9, 2016
1 parent c04ee55 commit fd26dd2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Src/AutoFixture.NUnit2.UnitTest/AutoDataAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,18 @@ public void GetDataOrdersCustomizationAttributes(string methodName)
Assert.True(customizationLog[1] is FreezeOnMatchCustomization);
// Teardown
}

// TODO: To be implemented.
[Test, Ignore]
public void GetDataReturnsValuesAttributeData()
{
var method = typeof(TypeWithParametrizedTestMethods).GetMethod("MethodWithValuesAttribute");
var expected = new[] { new object[] { -1 }, new object[] { 1 } };
var sut = new AutoDataAttribute();

var actual = sut.GetData(method);

Assert.AreEqual(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scenario.cs" />
<Compile Include="TypeWithCustomizationAttributes.cs" />
<Compile Include="TypeWithParametrizedTestMethods.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutoFixture.NUnit2.Addins\AutoFixture.NUnit2.Addins.csproj">
Expand Down
6 changes: 6 additions & 0 deletions Src/AutoFixture.NUnit2.UnitTest/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,11 @@ public void FavorArraysCausesArrayConstructorToBeInjectedWithFrozenItems([Frozen
{
Assert.AreNotEqual(p1, p2.Field);
}

[Test, AutoData]
public void FixtureIgnoresParameterWithValuesAttribute([Values(1, -1)] int num)
{
Assert.True(num == -1 || num == 1);
}
}
}
11 changes: 11 additions & 0 deletions Src/AutoFixture.NUnit2.UnitTest/TypeWithParametrizedTestMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using NUnit.Framework;

namespace Ploeh.AutoFixture.NUnit2.UnitTest
{
internal class TypeWithParametrizedTestMethods
{
public void MethodWithValuesAttribute([Values(1, -1)] int num)
{
}
}
}
48 changes: 47 additions & 1 deletion Src/AutoFixture.NUnit2/AutoDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using Ploeh.AutoFixture.NUnit2.Addins;
using Ploeh.AutoFixture.Kernel;

Expand Down Expand Up @@ -92,6 +94,7 @@ public override IEnumerable<object[]> GetData(MethodInfo method)
}

var specimens = new List<object>();

foreach (var p in method.GetParameters())
{
CustomizeFixture(p);
Expand All @@ -100,7 +103,8 @@ public override IEnumerable<object[]> GetData(MethodInfo method)
specimens.Add(specimen);
}

return new[] { specimens.ToArray() };
var parametrizedData = GetParametrizedData(method, specimens.ToArray());
return parametrizedData;
}

private void CustomizeFixture(ParameterInfo p)
Expand Down Expand Up @@ -153,5 +157,47 @@ private static IFixture CreateFixture(Type type)

return (IFixture)ctor.Invoke(null);
}

private static IEnumerable<object[]> GetParametrizedData(MethodInfo method, object[] specimens)
{
var parametrized = method.GetParameters()
.Any(p => p.GetCustomAttributes(typeof(ValuesAttribute), false).Any());

if (!parametrized)
{
return new[] { specimens.ToArray() };
}

var parametrizedspecimens = new List<IEnumerable<object[]>>();

var parameters = method.GetParameters();
for (var i = 0; 0 < parameters.Length - 1; i++)
{
var p = parameters[i];
var valuesAttributes = p.GetCustomAttributes(typeof(ValuesAttribute), false);
if (!valuesAttributes.Any())
{
continue;
}

var valuesAttribute = (ValuesAttribute)valuesAttributes.First();
var data = valuesAttribute.GetData(p);
var set = GetParametrizedDataSet(specimens, i, data);
parametrizedspecimens.Add(set);
}

return new[] { parametrizedspecimens.ToArray() };
}

private static IEnumerable<object[]> GetParametrizedDataSet(object[] sourceSpecimens, int parameterIndex, IEnumerable data)
{
foreach (var parameterValue in data)
{
var ps = new object[sourceSpecimens.Length];
sourceSpecimens.CopyTo(ps, 0);
ps[parameterIndex] = parameterValue;
yield return ps;
}
}
}
}
8 changes: 7 additions & 1 deletion Src/AutoFixture.NUnit2/AutoFixture.NUnit2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\..\Packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -90,7 +94,9 @@
<Name>AutoFixture</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 4 additions & 0 deletions Src/AutoFixture.NUnit2/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.2" targetFramework="net40" />
</packages>

0 comments on commit fd26dd2

Please sign in to comment.