Update for NUnit 2.5 compatibility (RowTestExtension 2.0)

This commit is contained in:
charliepoole 2008-07-06 21:28:09 +00:00
Родитель 9b6ca78729
Коммит f5c249bded
23 изменённых файлов: 899 добавлений и 312 удалений

Просмотреть файл

@ -0,0 +1,90 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using NUnitExtension.RowTest.AddIn;
using NUnit.Core.Extensibility;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class ArgumentListParameterSetFilterTest : ParameterSetFilterTestBase
{
private ArgumentListParameterSetFilter filter;
[SetUp]
public void SetUp()
{
filter = new ArgumentListParameterSetFilter();
}
[Test]
public void Filter_TwoArguments()
{
string arg1 = "arg1";
int arg2 = 42;
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { arg1, arg2 };
filter.Filter(parameterSet, GetMethod("MethodWithArgumentList"));
Assert.That(parameterSet.Arguments.Length, Is.EqualTo(3));
Assert.That(parameterSet.Arguments[0], Is.EqualTo(arg1));
Assert.That(parameterSet.Arguments[1], Is.EqualTo(arg2));
Assert.That(parameterSet.Arguments[2], Is.TypeOf(typeof(object[])));
Assert.That(parameterSet.Arguments[2], Is.Empty);
}
[Test]
public void Filter_FourArguments()
{
string arg1 = "arg1";
int arg2 = 42;
string arg3 = "arg3";
string arg4 = "arg4";
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { arg1, arg2, arg3, arg4 };
filter.Filter(parameterSet, GetMethod("MethodWithArgumentList"));
Assert.That(parameterSet.Arguments.Length, Is.EqualTo(3));
Assert.That(parameterSet.Arguments[0], Is.EqualTo(arg1));
Assert.That(parameterSet.Arguments[1], Is.EqualTo(arg2));
Assert.That(parameterSet.Arguments[2], Is.TypeOf(typeof(object[])));
object[] argumentList = (object[])parameterSet.Arguments[2];
Assert.That(argumentList.Length, Is.EqualTo(2));
Assert.That(argumentList[0], Is.EqualTo(arg3));
Assert.That(argumentList[1], Is.EqualTo(arg4));
}
[Test]
public void Filter_WithoutArgumentList()
{
string arg1 = "arg1";
int arg2 = 42;
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { arg1, arg2 };
filter.Filter(parameterSet, GetMethod("MethodWithoutArgumentList"));
Assert.That(parameterSet.Arguments.Length, Is.EqualTo(2));
Assert.That(parameterSet.Arguments[0], Is.EqualTo(arg1));
Assert.That(parameterSet.Arguments[1], Is.EqualTo(arg2));
}
#region Methods for tests
public void MethodWithArgumentList(string arg1, int arg2, object[] argumentList)
{
}
public void MethodWithoutArgumentList(string arg1, int arg2)
{
}
#endregion
}
}

Просмотреть файл

@ -0,0 +1,66 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core.Extensibility;
using NUnitExtension.RowTest.AddIn;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class ConvertParameterSetFilterTest : ParameterSetFilterTestBase
{
private ConvertParameterSetFilter filter;
[SetUp]
public void SetUp()
{
filter = new ConvertParameterSetFilter();
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Filter_ParameterSetIsNull()
{
filter.Filter(null, GetMethod("DateTimeMethod"));
}
[Test]
public void Filter_DateTime()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { "2008-04-13 16:12:00" };
filter.Filter(parameterSet, GetMethod("DateTimeMethod"));
Assert.That(parameterSet.Arguments[0], Is.EqualTo(new DateTime(2008, 4, 13, 16, 12, 0)));
}
[Test]
public void Filter_Decimal()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { "4.1266794625719769673704414587" };
filter.Filter(parameterSet, GetMethod("DecimalMethod"));
Assert.That(parameterSet.Arguments[0], Is.EqualTo(4.1266794625719769673704414587m));
}
#region Methods for tests
public void DateTimeMethod(DateTime dateTime)
{
}
public void DecimalMethod(Decimal decimalArgument)
{
}
#endregion
}
}

Просмотреть файл

@ -39,19 +39,23 @@
<HintPath>..\References\nunit.framework.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="nunit.mocks, Version=2.5.0.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="nunit.mocks">
<HintPath>..\References\nunit.mocks.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentListParameterSetFilterTest.cs" />
<Compile Include="AssemblyInfo.cs" />
<Compile Include="BaseTestFixture.cs" />
<Compile Include="ConvertParameterSetFilterTest.cs" />
<Compile Include="ParameterSetFilterTestBase.cs" />
<Compile Include="RowAttributeTest.cs" />
<Compile Include="RowTestAddInTest.cs" />
<Compile Include="RowTestCaseTest.cs" />
<Compile Include="RowTestFrameworkTest.cs" />
<Compile Include="RowTestNameBuilderTest.cs" />
<Compile Include="RowTestSuiteTest.cs" />
<Compile Include="RowTestParameterProviderTest.cs" />
<Compile Include="SpecialValueParameterSetFilterTest.cs" />
<Compile Include="TestClass.cs" />
<Compile Include="TypeParameterSetFilterTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NUnitExtension.RowTest.AddIn\NUnitExtension.RowTest.AddIn.csproj">

Просмотреть файл

@ -0,0 +1,17 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
public class ParameterSetFilterTestBase
{
protected MethodInfo GetMethod(string methodName)
{
return this.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
}
}
}

Просмотреть файл

@ -0,0 +1,46 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using NUnitExtension.RowTest;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class RowAttributeTest
{
[Test]
public void ArgumentsAreProvided()
{
object[] arguments = new object[] { 4, 5, 6 };
RowAttribute row = new RowAttribute(arguments);
Assert.That(row.Arguments, Is.SameAs(arguments));
}
[Test]
public void NullWasPassedAsArguments()
{
object[] arguments = null;
RowAttribute row = new RowAttribute(arguments);
Assert.That(row.Arguments, Is.Not.Null);
Assert.That(row.Arguments.Length, Is.EqualTo(1));
Assert.That(row.Arguments[0], Is.Null);
}
[Test]
public void WithoutArguments()
{
RowAttribute row = new RowAttribute();
Assert.That(row.Arguments, Is.Not.Null);
Assert.That(row.Arguments, Is.Empty);
}
}
}

Просмотреть файл

@ -8,276 +8,50 @@ using NUnit.Core;
using NUnit.Core.Extensibility;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
#if !NMOCK2
using NUnit.Mocks;
#endif
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class RowTestAddInTest : BaseTestFixture
public class RowTestAddInTest
{
private Test dummy = null; // OK unless builder starts using the suite argument
#if NMOCK2
private NMock2.Mockery _mocks;
private DynamicMock extensionHostMock;
private IExtensionHost extensionHost;
private RowTestAddIn addIn;
[SetUp]
public void SetUp()
{
_mocks = new NMock2.Mockery();
extensionHostMock = new DynamicMock(typeof(IExtensionHost));
extensionHost = (IExtensionHost) extensionHostMock.MockInstance;
addIn = new RowTestAddIn();
}
#endif
[Test]
public void Install_Successful()
{
#if NMOCK2
IExtensionHost extensionHostMock = (IExtensionHost)_mocks.NewMock(typeof(IExtensionHost));
IExtensionPoint extensionPointMock = (IExtensionPoint)_mocks.NewMock(typeof(IExtensionPoint));
RowTestAddIn addIn = new RowTestAddIn();
NMock2.Expect.Once.On(extensionHostMock)
.Method("GetExtensionPoint").With("TestCaseBuilders")
.Will(NMock2.Return.Value(extensionPointMock));
NMock2.Expect.Once.On(extensionPointMock)
.Method("Install").With(addIn);
bool installed = addIn.Install(extensionHost);
_mocks.VerifyAllExpectationsHaveBeenMet();
Assert.That(installed, Is.True);
#else
DynamicMock extensionHostMock = new DynamicMock(typeof(IExtensionHost));
IExtensionHost extensionHost = (IExtensionHost)extensionHostMock.MockInstance;
DynamicMock extensionPointMock = new DynamicMock(typeof(IExtensionPoint));
IExtensionPoint extensionPoint = (IExtensionPoint) extensionPointMock.MockInstance;
RowTestAddIn addIn = new RowTestAddIn();
extensionHostMock.ExpectAndReturn("GetExtensionPoint", extensionPointMock.MockInstance, "TestCaseBuilders");
extensionPointMock.Expect("Install", addIn);
extensionHostMock.ExpectAndReturn("GetExtensionPoint", extensionPoint, "ParameterProviders");
extensionPointMock.Expect("Install");
bool installed = addIn.Install(extensionHost);
extensionPointMock.Verify();
extensionHostMock.Verify();
extensionPointMock.Verify();
Assert.That(installed, Is.True);
#endif
}
[Test]
public void Install_Failure()
{
#if NMOCK2
IExtensionHost extensionHostMock = (IExtensionHost)_mocks.NewMock(typeof(IExtensionHost));
RowTestAddIn addIn = new RowTestAddIn();
NMock2.Expect.Once.On(extensionHostMock)
.Method("GetExtensionPoint").With("TestCaseBuilders")
.Will(NMock2.Return.Value(null));
bool installed = addIn.Install(extensionHostMock);
_mocks.VerifyAllExpectationsHaveBeenMet();
Assert.That(installed, Is.False);
#else
DynamicMock extensionHostMock = new DynamicMock(typeof(IExtensionHost));
IExtensionHost extensionHost = (IExtensionHost) extensionHostMock.MockInstance;
RowTestAddIn addIn = new RowTestAddIn();
extensionHostMock.ExpectAndReturn("GetExtensionPoint",null,"TestCaseBuilders");
extensionHostMock.ExpectAndReturn("GetExtensionPoint", null, "ParameterProviders");
bool installed = addIn.Install(extensionHost);
extensionHostMock.Verify();
Assert.That(installed, Is.False);
#endif
}
[Test]
public void CanBuildFrom_False()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetTestClassMethod("MethodWithoutRowTestAttribute");
bool canBuildFrom = addIn.CanBuildFrom(method);
Assert.That(canBuildFrom, Is.False);
}
[Test]
public void CanBuildFrom_True()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
bool canBuildFrom = addIn.CanBuildFrom(method);
Assert.That(canBuildFrom, Is.True);
}
[Test]
public void BuildFrom_WithoutRows()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(0));
}
[Test]
public void BuildFrom_WithRows()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetRowTestMethodWith2Rows();
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(2));
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
Assert.That(suite.Tests[1], Is.InstanceOfType(typeof(RowTestCase)));
RowTestCase testCase1 = (RowTestCase) suite.Tests[0];
RowTestCase testCase2 = (RowTestCase) suite.Tests[1];
Assert.That(testCase1.Arguments.Length, Is.EqualTo(2));
Assert.That(testCase2.Arguments.Length, Is.EqualTo(2));
}
[Test]
public void BuildFrom_WithTestName()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetRowTestMethodWithTestName();
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(1));
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
RowTestCase testCase = (RowTestCase) suite.Tests[0];
Assert.That(testCase.TestName.Name, Is.EqualTo("UnitTest(4, 5)"));
}
[Test]
public void BuildFrom_WithExpectedException()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetRowTestMethodWithExpectedException();
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(1));
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
RowTestCase testCase = (RowTestCase) suite.Tests[0];
Assert.That(testCase.ExceptionExpected, Is.True);
Assert.That(testCase.ExpectedExceptionType, Is.EqualTo(typeof(InvalidOperationException)));
}
[Test]
public void BuildFrom_WithExpectedExceptionAndExceptionMessage()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetRowTestMethodWithExpectedExceptionAndExceptionMessage();
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(1));
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
RowTestCase testCase = (RowTestCase) suite.Tests[0];
Assert.That(testCase.ExceptionExpected, Is.True);
Assert.That(testCase.ExpectedExceptionType, Is.EqualTo(typeof(InvalidOperationException)));
Assert.That(testCase.ExpectedMessage, Is.EqualTo("Expected Exception Message."));
}
[Test]
public void BuildFrom_SetsNameCorrectly()
{
RowTestAddIn addIn = new RowTestAddIn();
Type testClass = typeof(TestClass);
MethodInfo method = testClass.GetMethod(Method_RowTestMethodWith2Rows, BindingFlags.Public | BindingFlags.Instance);
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestName.Name, Is.EqualTo(method.Name));
Assert.That(suite.TestName.FullName, Is.EqualTo(testClass.FullName + "." + method.Name));
}
[Test]
public void BuildFrom_SetsCommonNUnitAttributes()
{
RowTestAddIn addin = new RowTestAddIn();
MethodInfo method = GetTestClassMethod("RowTestMethodWithCategory");
Test test = addin.BuildFrom(method);
Assert.That(test.Categories, Is.Not.Null);
Assert.That(test.Categories.Count, Is.EqualTo(1));
Assert.That(test.Categories[0], Is.EqualTo("Category"));
}
[Test]
public void BuildFrom_WithSpecialValueNull()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetTestClassMethod("RowTestMethodWithSpecialValue");
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(1));
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
RowTestCase testCase = (RowTestCase) suite.Tests[0];
Assert.That(testCase.Arguments.Length, Is.EqualTo(1));
Assert.That(testCase.Arguments[0], Is.Null);
}
[Test]
public void BuildFrom_WithNull()
{
RowTestAddIn addIn = new RowTestAddIn();
MethodInfo method = GetTestClassMethod("RowTestMethodWithNullArgument");
Test test = addIn.BuildFrom(method);
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
RowTestSuite suite = (RowTestSuite) test;
Assert.That(suite.TestCount, Is.EqualTo(1));
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
RowTestCase testCase = (RowTestCase) suite.Tests[0];
Assert.That(testCase.Arguments, Is.Null);
}
}
}

Просмотреть файл

@ -0,0 +1,131 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Collections;
using System.Reflection;
using NUnit.Core.Extensibility;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using NUnit.Mocks;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class RowTestParameterProviderTest
{
private RowTestParameterProvider parameterProvider;
private IList GetParametersForMethodAsList(MethodInfo method)
{
ArrayList list = new ArrayList();
foreach (object o in parameterProvider.GetParametersFor(method))
list.Add(o);
return list;
}
[SetUp]
public void SetUp()
{
parameterProvider = new RowTestParameterProvider();
}
[Test]
public void HasParametersFor_WithoutRows()
{
MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
bool hasParameters = parameterProvider.HasParametersFor(method);
Assert.That(hasParameters, Is.False);
}
[Test]
public void HasParametersFor_WithRows()
{
MethodInfo method = GetTestClassMethod("RowTestMethodWith2Rows");
bool hasParameters = parameterProvider.HasParametersFor(method);
Assert.That(hasParameters, Is.True);
}
[Test]
public void HasParametersFor_WithoutRowTestAttribute()
{
MethodInfo method = GetTestClassMethod("MethodWithRowAttribute");
bool hasParameters = parameterProvider.HasParametersFor(method);
Assert.That(hasParameters, Is.False);
}
[Test]
public void GetParametersFor_WithoutRows()
{
MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
IList parameterSets = GetParametersForMethodAsList(method);
Assert.That(parameterSets.Count, Is.EqualTo(0));
}
[Test]
public void GetParametersFor_WithRows()
{
MethodInfo method = GetTestClassMethod("RowTestMethodWith2Rows");
IList parameterSets = GetParametersForMethodAsList(method);
Assert.That(parameterSets.Count, Is.EqualTo(2));
ParameterSet parameterSet1 = (ParameterSet) parameterSets[0];
ParameterSet parameterSet2 = (ParameterSet) parameterSets[1];
Assert.That(parameterSet1.Arguments.Length, Is.EqualTo(2));
Assert.That(parameterSet2.Arguments.Length, Is.EqualTo(2));
}
[Test]
public void GetParametersFor_Null()
{
MethodInfo method = GetTestClassMethod("RowTestMethodWithNullArgument");
IList parameterSets = GetParametersForMethodAsList(method);
Assert.That(parameterSets.Count, Is.EqualTo(1));
ParameterSet parameterSet = (ParameterSet) parameterSets[0];
Assert.That(parameterSet.Arguments.Length, Is.EqualTo(1));
Assert.That(parameterSet.Arguments[0], Is.Null);
}
[Test]
public void GetParametersFor_CallsParameterSetFilter()
{
DynamicMock parameterSetFilterMock = new DynamicMock(typeof(IParameterSetFilter));
IParameterSetFilter parameterSetFilter = (IParameterSetFilter) parameterSetFilterMock.MockInstance;
parameterProvider.AddParameterSetFilter(parameterSetFilter);
parameterSetFilterMock.Expect("Filter", Is.TypeOf(typeof(ParameterSet)), Is.Not.Null);
parameterSetFilterMock.Expect("Filter", Is.TypeOf(typeof(ParameterSet)), Is.Not.Null);
parameterProvider.GetParametersFor(GetTestClassMethod("RowTestMethodWith2Rows"));
parameterSetFilterMock.Verify();
}
[Test]
[ExpectedException (typeof(ArgumentNullException))]
public void AddParameterSetFilter_FilterIsNull()
{
parameterProvider.AddParameterSetFilter(null);
}
private MethodInfo GetTestClassMethod(string methodName)
{
Type testClass = typeof(TestClass);
return testClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
}
}
}

Просмотреть файл

@ -35,7 +35,7 @@ namespace NUnitExtension.RowTest.AddIn.UnitTests
RowTestSuite rowTestSuite = new RowTestSuite(GetRowTestMethodWith2Rows());
parentSuite.Add(rowTestSuite);
rowTestSuite.Run(new NullListener());
rowTestSuite.Run(new NullListener(),TestFilter.Empty);
Assert.That(rowTestSuite.Fixture, Is.SameAs(fixture));
}
@ -45,25 +45,9 @@ namespace NUnitExtension.RowTest.AddIn.UnitTests
{
RowTestSuite rowTestSuite = new RowTestSuite(GetRowTestMethodWith2Rows());
rowTestSuite.Run(new NullListener());
rowTestSuite.Run(new NullListener(),TestFilter.Empty);
Assert.That(rowTestSuite.Fixture, Is.Null);
}
[Test]
public void Run_WithTestFilter()
{
TestClass fixture = new TestClass();
TestSuite parentSuite = new TestSuite("ParentSuiteName", "Name");
parentSuite.Fixture = fixture;
RowTestSuite rowTestSuite = new RowTestSuite(GetRowTestMethodWith2Rows());
parentSuite.Add(rowTestSuite);
rowTestSuite.Run(new NullListener(), TestFilter.Empty);
Assert.That(rowTestSuite.Fixture, Is.SameAs(fixture));
}
}
}

Просмотреть файл

@ -0,0 +1,55 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core.Extensibility;
using NUnitExtension.RowTest.AddIn;
using NUnitExtension.RowTest;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class SpecialValueParameterSetFilterTest : ParameterSetFilterTestBase
{
[Test]
public void Filter_SpecialValueNull()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { 42, SpecialValue.Null, 21 };
SpecialValueParameterSetFilter filter = new SpecialValueParameterSetFilter();
filter.Filter(parameterSet, GetMethod());
Assert.That(parameterSet.Arguments, Is.EqualTo(new object[] { 42, null, 21 }).AsCollection);
}
[Test]
public void Filter_Null()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { 42, null, 42 };
SpecialValueParameterSetFilter filter = new SpecialValueParameterSetFilter();
filter.Filter(parameterSet, GetMethod());
Assert.That(parameterSet.Arguments, Is.EqualTo(new object[] { 42, null, 42 }).AsCollection);
}
private MethodInfo GetMethod()
{
return GetMethod("MethodWith3Parameters");
}
#region Methods for tests
public void MethodWith3Parameters(object a1, object a2, object a3)
{
}
#endregion
}
}

Просмотреть файл

@ -22,6 +22,7 @@ namespace NUnitExtension.RowTest.AddIn.UnitTests
public void MethodWithoutRowTestAttribute()
{
Assert.Fail("Should not be called.");
}
[RowTest]
@ -29,6 +30,12 @@ namespace NUnitExtension.RowTest.AddIn.UnitTests
{
}
[Row(42, 21)]
public void MethodWithRowAttribute(int a, int b)
{
Assert.Fail("Should not be called.");
}
[RowTest]
[Row(4, 5)]
[Row(5, 6)]
@ -80,7 +87,7 @@ namespace NUnitExtension.RowTest.AddIn.UnitTests
_arguments = new object[] { a };
}
#if NET_2_0
#if !FX1_1 && !NET_1_1
[RowTest]
[Row(9, null)]
public void RowTestMethodWithNormalAndNullArgument(int a, object b)

Просмотреть файл

@ -0,0 +1,111 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Collections;
using System.Reflection;
using NUnit.Core.Extensibility;
using NUnitExtension.RowTest.AddIn;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace NUnitExtension.RowTest.AddIn.UnitTests
{
[TestFixture]
public class TypeParameterSetFilterTest : ParameterSetFilterTestBase
{
public class TypeWithDefaultConstructor
{
public TypeWithDefaultConstructor()
{
}
}
public class TypeWithoutDefaultConstructor
{
public TypeWithoutDefaultConstructor(string arg)
{
}
}
TypeParameterSetFilter filter;
[SetUp]
public void SetUp()
{
filter = new TypeParameterSetFilter();
}
[Test]
public void Filter_CreatesInstance()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { typeof(TypeWithDefaultConstructor) };
filter.Filter(parameterSet, GetMethod("MethodWithParameter"));
Assert.That(parameterSet.Arguments[0], Is.TypeOf(typeof(TypeWithDefaultConstructor)));
}
[Test]
public void Filter_TypeWithoutDefaultConstructor()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { typeof(TypeWithoutDefaultConstructor) };
try
{
filter.Filter(parameterSet, GetMethod("MethodWithWrongParameter"));
Assert.Fail("ArgumentException was expected.");
}
catch (ArgumentException exception)
{
string expectedMessage = string.Format(
"Cannot create an instance of type '{0}', "
+ "because it does not have a default constructor.",
parameterSet.Arguments[0]);
Assert.That(exception.Message, Is.EqualTo(expectedMessage));
}
}
[Test]
public void Filter_BaseType()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { typeof(ArrayList) };
filter.Filter(parameterSet, GetMethod("MethodWithIEnumerableParameter"));
Assert.That(parameterSet.Arguments[0], Is.TypeOf(typeof(ArrayList)));
}
[Test]
public void Filter_NotAssignableType()
{
ParameterSet parameterSet = new ParameterSet();
parameterSet.Arguments = new object[] { typeof(Version) };
filter.Filter(parameterSet, GetMethod("MethodWithIEnumerableParameter"));
Assert.That(parameterSet.Arguments[0], Is.SameAs(typeof(Version)));
}
#region Methods for tests
public void MethodWithParameter(TypeWithDefaultConstructor instance)
{
}
public void MethodWithWrongParameter(TypeWithoutDefaultConstructor instance)
{
}
public void MethodWithIEnumerableParameter(IEnumerable enumerable)
{
}
#endregion
}
}

Просмотреть файл

@ -0,0 +1,42 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
public class ArgumentListParameterSetFilter : IParameterSetFilter
{
public ArgumentListParameterSetFilter()
{
}
public void Filter(ParameterSet parameterSet, MethodInfo method)
{
object[] parameters = parameterSet.Arguments;
ParameterInfo[] methodParameters = method.GetParameters();
if (methodParameters[methodParameters.Length - 1].ParameterType == typeof(object[]))
parameterSet.Arguments = GetArguments(parameters, methodParameters);
}
private object[] GetArguments(object[] parameters, ParameterInfo[] methodParameters)
{
object[] newParameterList = new object[methodParameters.Length];
int argumentListPosition = methodParameters.Length - 1;
for (int i = 0; i < argumentListPosition; i++)
newParameterList[i] = parameters[i];
int argumentListLength = parameters.Length - methodParameters.Length + 1;
object[] argumentList = new object[argumentListLength];
Array.Copy(parameters, argumentListPosition, argumentList, 0, argumentListLength);
newParameterList[argumentListPosition] = argumentList;
return newParameterList;
}
}
}

Просмотреть файл

@ -0,0 +1,55 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Globalization;
using System.Reflection;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
public class ConvertParameterSetFilter : ParameterSetFilterBase
{
public ConvertParameterSetFilter()
{
}
protected override bool CanFilter(object argument, ParameterInfo parameter)
{
return argument is IConvertible;
}
protected override object FilterArgument(object argument, ParameterInfo parameter)
{
IFormatProvider formatProvider = GetFormatProvider(parameter.ParameterType);
return Convert.ChangeType(argument, parameter.ParameterType, formatProvider);
}
private IFormatProvider GetFormatProvider(Type type)
{
if (type == typeof(DateTime))
return DateTimeFormatInfo.InvariantInfo;
if (IsNumericType(type))
return NumberFormatInfo.InvariantInfo;
return null;
}
private bool IsNumericType(Type type)
{
return type == typeof(byte)
|| type == typeof(sbyte)
|| type == typeof(decimal)
|| type == typeof(double)
|| type == typeof(float)
|| type == typeof(int)
|| type == typeof(uint)
|| type == typeof(long)
|| type == typeof(ulong)
|| type == typeof(short)
|| type == typeof(ushort);
}
}
}

Просмотреть файл

@ -0,0 +1,15 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
public interface IParameterSetFilter
{
void Filter(ParameterSet parameterSet, MethodInfo method);
}
}

Просмотреть файл

@ -41,12 +41,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentListParameterSetFilter.cs" />
<Compile Include="AssemblyInfo.cs" />
<Compile Include="ConvertParameterSetFilter.cs" />
<Compile Include="IParameterSetFilter.cs" />
<Compile Include="ParameterSetFilterBase.cs" />
<Compile Include="RowTestAddIn.cs" />
<Compile Include="RowTestCase.cs" />
<Compile Include="RowTestFactory.cs" />
<Compile Include="RowTestFramework.cs" />
<Compile Include="RowTestNameBuilder.cs" />
<Compile Include="RowTestSuite.cs" />
<Compile Include="RowTestParameterProvider.cs" />
<Compile Include="SpecialValueParameterSetFilter.cs" />
<Compile Include="TypeParameterSetFilter.cs" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,36 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
public abstract class ParameterSetFilterBase : IParameterSetFilter
{
public ParameterSetFilterBase()
{
}
public void Filter(ParameterSet parameterSet, MethodInfo method)
{
if (parameterSet == null)
throw new ArgumentNullException("parameterSet");
ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < parameterSet.Arguments.Length; i++)
{
object argument = parameterSet.Arguments[i];
if (CanFilter(argument, parameters[i]))
parameterSet.Arguments[i] = FilterArgument(argument, parameters[i]);
}
}
protected abstract bool CanFilter(object argument, ParameterInfo parameter);
protected abstract object FilterArgument(object argument, ParameterInfo parameter);
}
}

Просмотреть файл

@ -3,20 +3,15 @@
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
[NUnitAddin(Name = "Row Test Extension")]
public class RowTestAddIn : IAddin, ITestCaseBuilder
public class RowTestAddIn : IAddin
{
private RowTestFactory _testFactory;
public RowTestAddIn()
{
_testFactory = new RowTestFactory();
}
public bool Install(IExtensionHost host)
@ -24,41 +19,22 @@ namespace NUnitExtension.RowTest.AddIn
if (host == null)
throw new ArgumentNullException("host");
IExtensionPoint testCaseBuilders = host.GetExtensionPoint("TestCaseBuilders");
if (testCaseBuilders == null)
IExtensionPoint parameterProviders = host.GetExtensionPoint("ParameterProviders");
if (parameterProviders == null)
return false;
testCaseBuilders.Install(this);
parameterProviders.Install(CreateParameterProvider());
return true;
}
public bool CanBuildFrom(MethodInfo method)
private IParameterProvider CreateParameterProvider()
{
return CanBuildFrom(method, null);
}
RowTestParameterProvider parameterProvider = new RowTestParameterProvider();
parameterProvider.AddParameterSetFilter(new SpecialValueParameterSetFilter());
parameterProvider.AddParameterSetFilter(new ConvertParameterSetFilter());
parameterProvider.AddParameterSetFilter(new TypeParameterSetFilter());
public bool CanBuildFrom(MethodInfo method, Test suite)
{
return RowTestFramework.IsRowTest(method);
}
public Test BuildFrom(MethodInfo method)
{
return BuildFrom(method, null);
}
public Test BuildFrom(MethodInfo method, Test suite)
{
if (method == null)
throw new ArgumentNullException("method");
RowTestSuite methods = _testFactory.CreateRowTestSuite(method);
Attribute[] rows = RowTestFramework.GetRowAttributes(method);
foreach (Attribute row in rows)
methods.Add(_testFactory.CreateRowTestCase(row, method));
return methods;
return parameterProvider;
}
}
}

Просмотреть файл

@ -0,0 +1,63 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Collections;
using System.Reflection;
using NUnit.Core;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
public class RowTestParameterProvider : IParameterProvider
{
public const string RowTestAttribute = "NUnitExtension.RowTest.RowTestAttribute";
public const string RowAttribute = "NUnitExtension.RowTest.RowAttribute";
private ArrayList parameterSetFilters;
public RowTestParameterProvider()
{
parameterSetFilters = new ArrayList();
}
public void AddParameterSetFilter(IParameterSetFilter filter)
{
if (filter == null)
throw new ArgumentNullException("filter");
parameterSetFilters.Add(filter);
}
public bool HasParametersFor(MethodInfo method)
{
if (method == null)
throw new ArgumentNullException("method");
return Reflect.HasAttribute(method, RowAttribute, false)
&& Reflect.HasAttribute(method, RowTestAttribute, false);
}
public IEnumerable GetParametersFor(MethodInfo method)
{
if (method == null)
throw new ArgumentNullException("method");
Attribute[] rowAttributes = Reflect.GetAttributes(method, RowAttribute, false);
ArrayList parameterSets = new ArrayList();
foreach (Attribute rowAttribute in rowAttributes)
{
ParameterSet parameterSet = ParameterSet.FromDataSource(rowAttribute);
foreach (IParameterSetFilter filter in parameterSetFilters)
filter.Filter(parameterSet, method);
parameterSets.Add(parameterSet);
}
return (ParameterSet[])parameterSets.ToArray(typeof(ParameterSet));
}
}
}

Просмотреть файл

@ -0,0 +1,32 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
using NUnit.Core.Extensibility;
namespace NUnitExtension.RowTest.AddIn
{
public class SpecialValueParameterSetFilter : ParameterSetFilterBase
{
public const string SpecialValueEnum = "NUnitExtension.RowTest.SpecialValue";
public SpecialValueParameterSetFilter()
{
}
protected override bool CanFilter(object argument, ParameterInfo parameter)
{
return argument != null && argument.GetType().FullName == SpecialValueEnum;
}
protected override object FilterArgument(object argument, ParameterInfo parameter)
{
if (argument.ToString() == "Null")
return null;
return argument;
}
}
}

Просмотреть файл

@ -0,0 +1,44 @@
// *********************************************************************
// Copyright 2007, Andreas Schlapsi
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Reflection;
namespace NUnitExtension.RowTest.AddIn
{
public class TypeParameterSetFilter : ParameterSetFilterBase
{
public TypeParameterSetFilter()
{
}
protected override bool CanFilter(object argument, ParameterInfo parameter)
{
Type type = argument as Type;
return type != null && parameter.ParameterType.IsAssignableFrom(type);
}
protected override object FilterArgument(object argument, ParameterInfo parameter)
{
Type type = argument as Type;
ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
throw CreateArgumentException(
"Cannot create an instance of type '{0}', because it does not have a default constructor.",
type);
}
return constructor.Invoke (new object[0]);
}
private static ArgumentException CreateArgumentException(string message, params object[] args)
{
return new ArgumentException(string.Format(message, args));
}
}
}

Просмотреть файл

@ -3,6 +3,8 @@
// This is free software licensed under the MIT license.
// *********************************************************************
using System;
using System.Collections;
using System.Collections.Specialized;
using NUnit.Framework;
using NUnitExtension.RowTest;
@ -75,6 +77,31 @@ namespace NUnitExtension.RowTest.Tests
Assert.IsNull(argument);
}
[RowTest]
[Row("2008-04-13 17:52:42")]
public void DateTimeArgument(DateTime dateTime)
{
Assert.AreEqual(new DateTime(2008, 4, 13, 17, 52, 42), dateTime);
}
[RowTest]
[Row(typeof(ArrayList))]
[Row(typeof(StringCollection))]
public void IListTest(IList list)
{
string item = "Text";
Assert.AreEqual(0, list.Count);
list.Add(item);
Assert.AreEqual(1, list.Count);
Assert.IsTrue(list.Contains(item));
list.Remove(item);
Assert.AreEqual(0, list.Count);
}
private int Sum(int x, int y)
{
int sum = x + y;

Просмотреть файл

@ -32,6 +32,9 @@ namespace NUnitExtension.RowTest
public RowAttribute(params object[] arguments)
{
if (arguments == null)
_arguments = new object[] { null };
else
_arguments = arguments;
}

Просмотреть файл

@ -1,3 +1,10 @@
Version 2.0.0
-------------
* Implemented RowTestParameterProvider. RowTestAddIn installs an instance of this class at the
extension point "ParameterProviders", instead of a TestCaseBuilder.
Version 1.2.2 - (2008-03-27)
----------------------------