public override IEnumerable GetTestCases() { #if CLR_2_0 || CLR_4_0 List<ParameterSet> testCases = new List<ParameterSet>(); #else ArrayList testCases = new ArrayList(); #endif for (; ; ) { bool gotData = false; object[] testdata = new object[Sources.Length]; for (int i = 0; i < Sources.Length; i++) if (Enumerators[i].MoveNext()) { testdata[i] = Enumerators[i].Current; gotData = true; } else testdata[i] = null; if (!gotData) break; ParameterSet testcase = new ParameterSet(); testcase.Arguments = testdata; testCases.Add(testcase); } return testCases; }
public override IEnumerable GetTestCases() { ArrayList[] valueSet = CreateValueSet(); int[] dimensions = CreateDimensions(valueSet); IEnumerable pairwiseTestCases = new PairwiseTestCaseGenerator(dimensions).GetTestCases(); #if NET_2_0 List<ParameterSet> testCases = new List<ParameterSet>(); #else ArrayList testCases = new ArrayList(); #endif foreach (TestCase pairwiseTestCase in pairwiseTestCases) { object[] testData = new object[pairwiseTestCase.Features.Length]; for (int i = 0; i < pairwiseTestCase.Features.Length; i++) { testData[i] = valueSet[i][pairwiseTestCase.Features[i]]; } ParameterSet testCase = new ParameterSet(); testCase.Arguments = testData; testCases.Add(testCase); } return testCases; }
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); }
/// <summary> /// Return an IEnumerable providing test cases for use in /// running a parameterized test. /// </summary> /// <param name="method"></param> /// <returns></returns> public IEnumerable GetTestCasesFor(MethodInfo method, Test parentSuite) { ArrayList parameterList = new ArrayList(); foreach (ProviderReference info in GetSourcesFor(method, parentSuite)) { foreach (object source in info.GetInstance()) { ParameterSet parms; if (source == null) { parms = new ParameterSet(); parms.Arguments = new object[] { null }; } else parms = source as ParameterSet; if (parms == null) { if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null) parms = ParameterSet.FromDataSource(source); else { parms = new ParameterSet(); ParameterInfo[] parameters = method.GetParameters(); Type sourceType = source.GetType(); if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType)) parms.Arguments = new object[] { source }; else if (source is object[]) parms.Arguments = (object[])source; else if (source is Array) { Array array = (Array)source; if (array.Rank == 1) { parms.Arguments = new object[array.Length]; for (int i = 0; i < array.Length; i++) parms.Arguments[i] = (object)array.GetValue(i); } } else parms.Arguments = new object[] { source }; } } if (info.Category != null) foreach (string cat in info.Category.Split(new char[] { ',' })) parms.Categories.Add(cat); parameterList.Add(parms); } } return parameterList; }
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))); }
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))); }
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); }
/// <summary> /// Builds a ParameterizedMetodSuite containing individual /// test cases for each set of parameters provided for /// this method. /// </summary> /// <param name="method">The MethodInfo for which a test is to be built</param> /// <returns>A ParameterizedMethodSuite populated with test cases</returns> public static Test BuildParameterizedMethodSuite(MethodInfo method, Test parentSuite) { ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method); NUnitFramework.ApplyCommonAttributes(method, methodSuite); foreach (object source in CoreExtensions.Host.TestCaseProviders.GetTestCasesFor(method, parentSuite)) { ParameterSet parms; if (source == null) { parms = new ParameterSet(); parms.Arguments = new object[] { null }; } else parms = source as ParameterSet; if (parms == null) { if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null) parms = ParameterSet.FromDataSource(source); else { parms = new ParameterSet(); ParameterInfo[] parameters = method.GetParameters(); Type sourceType = source.GetType(); if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType)) parms.Arguments = new object[] { source }; else if (source is object[]) parms.Arguments = (object[])source; else if (source is Array) { Array array = (Array)source; if (array.Rank == 1) { parms.Arguments = new object[array.Length]; for (int i = 0; i < array.Length; i++) parms.Arguments[i] = (object)array.GetValue(i); } } else parms.Arguments = new object[] { source }; } } TestMethod test = BuildSingleTestMethod(method, parentSuite, parms); methodSuite.Add(test); } return methodSuite; }
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)); }
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); }
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]); } }
/// <summary> /// Return an IEnumerable providing test cases for use in /// running a parameterized test. /// </summary> /// <param name="method"></param> /// <returns></returns> public IEnumerable GetTestCasesFor(MethodInfo method) { ArrayList list = new ArrayList(); Attribute[] attrs = Reflect.GetAttributes(method, NUnitFramework.TestCaseAttribute, false); ParameterInfo[] parameters = method.GetParameters(); int argsNeeded = parameters.Length; foreach (Attribute attr in attrs) { ParameterSet parms; try { parms = ParameterSet.FromDataSource(attr); //if (method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(object[])) // parms.Arguments = new object[]{parms.Arguments}; if (argsNeeded == 1 && method.GetParameters()[0].ParameterType == typeof(object[])) { if (parms.Arguments.Length > 1 || parms.Arguments.Length == 1 && parms.Arguments[0].GetType() != typeof(object[])) { parms.Arguments = new object[] { parms.Arguments }; } } if (parms.Arguments.Length == argsNeeded) PerformSpecialConversions(parms.Arguments, parameters); } catch (Exception ex) { parms = new ParameterSet( ex ); } list.Add( parms ); } return list; }
/// <summary> /// Return an IEnumerable providing test cases for use in /// running a parameterized test. /// </summary> /// <param name="method"></param> /// <param name="parentSuite"></param> /// <returns></returns> public IEnumerable GetTestCasesFor(MethodInfo method, Test parentSuite) { ArrayList parameterList = new ArrayList(); var attributes = Reflect.GetAttributes(method, Constants.AutoDataAttribute, false); foreach (DataAttribute attr in attributes) { foreach (var arguments in attr.GetData(method)) { ParameterSet parms = new ParameterSet(); parms.Arguments = arguments; parameterList.Add(parms); } } return parameterList; }
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)); } }
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)); }
public override IEnumerable GetTestCases() { IEnumerator[] enumerators = new IEnumerator[Sources.Length]; int index = -1; #if CLR_2_0 || CLR_4_0 List<ParameterSet> testCases = new List<ParameterSet>(); #else ArrayList testCases = new ArrayList(); #endif for (; ; ) { while (++index < Sources.Length) { enumerators[index] = Sources[index].GetEnumerator(); if (!enumerators[index].MoveNext()) return testCases; } object[] testdata = new object[Sources.Length]; for (int i = 0; i < Sources.Length; i++) testdata[i] = enumerators[i].Current; ParameterSet testCase = new ParameterSet(); testCase.Arguments = testdata; testCases.Add(testCase); index = Sources.Length; while (--index >= 0 && !enumerators[index].MoveNext()) ; if (index < 0) break; } return testCases; }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> private static bool CheckTestMethodSignature(TestMethod testMethod, ParameterSet parms) { if (testMethod.Method.IsAbstract) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is abstract"; return false; } if (!testMethod.Method.IsPublic) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is not public"; return false; } ParameterInfo[] parameters = testMethod.Method.GetParameters(); int argsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.arguments = parms.Arguments; testMethod.expectedResult = parms.Result; testMethod.hasExpectedResult = parms.HasExpectedResult; testMethod.RunState = parms.RunState; testMethod.IgnoreReason = parms.NotRunReason; testMethod.BuilderException = parms.ProviderException; arglist = parms.Arguments; if (arglist != null) argsProvided = arglist.Length; if (testMethod.RunState != RunState.Runnable) return false; } if (!testMethod.Method.ReturnType.Equals(typeof(void)) && (parms == null || !parms.HasExpectedResult && parms.ExpectedExceptionName == null)) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method has non-void return value"; return false; } if (argsProvided > 0 && argsNeeded == 0) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Arguments provided for method not taking any"; return false; } if (argsProvided == 0 && argsNeeded > 0) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "No arguments were provided"; return false; } //if (argsProvided > argsNeeded) //{ // ParameterInfo lastParameter = parameters[argsNeeded - 1]; // Type lastParameterType = lastParameter.ParameterType; // if (lastParameterType.IsArray && lastParameter.IsDefined(typeof(ParamArrayAttribute), false)) // { // object[] newArglist = new object[argsNeeded]; // for (int i = 0; i < argsNeeded; i++) // newArglist[i] = arglist[i]; // int length = argsProvided - argsNeeded + 1; // Array array = Array.CreateInstance(lastParameterType.GetElementType(), length); // for (int i = 0; i < length; i++) // array.SetValue(arglist[argsNeeded + i - 1], i); // newArglist[argsNeeded - 1] = array; // testMethod.arguments = arglist = newArglist; // argsProvided = argsNeeded; // } //} if (argsProvided != argsNeeded ) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Wrong number of arguments provided"; return false; } #if NET_2_0 if (testMethod.Method.IsGenericMethodDefinition) { Type[] typeArguments = GetTypeArgumentsForMethod(testMethod.Method, arglist); foreach (object o in typeArguments) if (o == null) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Unable to determine type arguments for fixture"; return false; } testMethod.method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (arglist[i].GetType() != parameters[i].ParameterType && arglist[i] is IConvertible) { try { arglist[i] = Convert.ChangeType(arglist[i], parameters[i].ParameterType); } catch (Exception) { // Do nothing - the incompatible argument will be reported below } } } } #endif return true; }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parms">The ParameterSet to be used, or null</param> /// <returns></returns> public static NUnitTestMethod BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms) { NUnitTestMethod testMethod = new NUnitTestMethod(method); string prefix = method.ReflectedType.FullName; if (parentSuite != null) { prefix = parentSuite.TestName.FullName; testMethod.TestName.FullName = prefix + "." + testMethod.TestName.Name; } if (CheckTestMethodSignature(testMethod, parms)) { if (parms == null) NUnitFramework.ApplyCommonAttributes(method, testMethod); NUnitFramework.ApplyExpectedExceptionAttribute(method, testMethod); } if (parms != null) { // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we reassign it here. method = testMethod.Method; if (parms.TestName != null) { testMethod.TestName.Name = parms.TestName; testMethod.TestName.FullName = prefix + "." + parms.TestName; } else if (parms.OriginalArguments != null) { string name = MethodHelper.GetDisplayName(method, parms.OriginalArguments); testMethod.TestName.Name = name; testMethod.TestName.FullName = prefix + "." + name; } if (parms.Ignored) { testMethod.RunState = RunState.Ignored; testMethod.IgnoreReason = parms.IgnoreReason; } if (parms.ExpectedExceptionName != null) testMethod.exceptionProcessor = new ExpectedExceptionProcessor(testMethod, parms); foreach (string key in parms.Properties.Keys) testMethod.Properties[key] = parms.Properties[key]; // Description is stored in parms.Properties if (parms.Description != null) testMethod.Description = parms.Description; } if (testMethod.BuilderException != null) testMethod.RunState = RunState.NotRunnable; return testMethod; }
/// <summary> /// Constructs a ParameterSet from another object, accessing properties /// by reflection. The object must expose at least an Arguments property /// in order for the test to be runnable. /// </summary> /// <param name="source"></param> public static ParameterSet FromDataSource(object source) { ParameterSet parms = new ParameterSet(); parms.Arguments = GetParm(source, PropertyNames.Arguments) as object[]; parms.ExpectedException = GetParm(source, PropertyNames.ExpectedException) as Type; if (parms.ExpectedException != null) parms.ExpectedExceptionName = parms.ExpectedException.FullName; else parms.ExpectedExceptionName = GetParm(source, PropertyNames.ExpectedExceptionName) as string; parms.ExpectedMessage = GetParm(source, PropertyNames.ExpectedMessage) as string; object matchEnum = GetParm(source, PropertyNames.MatchType); if ( matchEnum != null ) parms.MatchType = matchEnum.ToString(); // Note: pre-2.6 versions of some attributes don't have the HasExpectedResult property object hasResult = GetParm(source, PropertyNames.HasExpectedResult); object expectedResult = GetParm(source, PropertyNames.ExpectedResult); if (hasResult != null && (bool)hasResult || expectedResult != null) parms.Result = expectedResult; parms.Description = GetParm(source, PropertyNames.Description) as string; parms.TestName = GetParm(source, PropertyNames.TestName) as string; object objIgnore = GetParm(source, PropertyNames.Ignored); if (objIgnore != null) parms.Ignored = (bool)objIgnore; parms.IgnoreReason = GetParm(source, PropertyNames.IgnoreReason) as string; object objExplicit = GetParm(source, PropertyNames.Explicit); if (objExplicit != null) parms.Explicit = (bool)objExplicit; // Some sources may also implement Properties and/or Categories bool gotCategories = false; IDictionary props = GetParm(source, PropertyNames.Properties) as IDictionary; if ( props != null ) foreach (string key in props.Keys) { parms.Properties.Add(key, props[key]); if (key == CATEGORIES) gotCategories = true; } // Some sources implement Categories. They may have been // provided as properties or they may be separate. if (!gotCategories) { IList categories = GetParm(source, PropertyNames.Categories) as IList; if (categories != null) foreach (string cat in categories) parms.Categories.Add(cat); } return parms; }
/// <summary> /// Constructs a ParameterSet from another object, accessing properties /// by reflection. The object must expose at least an Arguments property /// in order for the test to be runnable. /// </summary> /// <param name="source"></param> public static ParameterSet FromDataSource(object source) { ParameterSet parms = new ParameterSet(); parms.Arguments = GetParm(source, PropertyNames.Arguments) as object[]; parms.ExpectedException = GetParm(source, PropertyNames.ExpectedException) as Type; if (parms.ExpectedException != null) parms.ExpectedExceptionName = parms.ExpectedException.FullName; else parms.ExpectedExceptionName = GetParm(source, PropertyNames.ExpectedExceptionName) as string; parms.ExpectedMessage = GetParm(source, PropertyNames.ExpectedMessage) as string; object matchEnum = GetParm(source, PropertyNames.MatchType); if ( matchEnum != null ) parms.MatchType = matchEnum.ToString(); parms.Result = GetParm(source, PropertyNames.Result); parms.Description = GetParm(source, PropertyNames.Description) as string; parms.TestName = GetParm(source, PropertyNames.TestName) as string; // Some sources may also implement Properties and/or Categories bool gotCategories = false; IDictionary props = GetParm(source, PropertyNames.Properties) as IDictionary; if ( props != null ) foreach (string key in props.Keys) { parms.Properties.Add(key, props[key]); if (key == CATEGORIES) gotCategories = true; } // Some sources implement Categories. They may have been // provided as properties or they may be separate. if (!gotCategories) { IList categories = GetParm(source, PropertyNames.Categories) as IList; if (categories != null && props[CATEGORIES] == null) foreach (string cat in categories) categories.Add(cat); } return parms; }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parms">The ParameterSet to be used, or null</param> /// <returns></returns> public static NUnitTestMethod BuildSingleTestMethod(MethodInfo method, ParameterSet parms) { NUnitTestMethod testMethod = new NUnitTestMethod(method); if (CheckTestMethodSignature(testMethod, parms)) { NUnitFramework.ApplyCommonAttributes(method, testMethod); NUnitFramework.ApplyExpectedExceptionAttribute(method, testMethod); } if (parms != null) { // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we reassign it here. method = testMethod.Method; if (parms.TestName != null) { testMethod.TestName.Name = parms.TestName; testMethod.TestName.FullName = method.ReflectedType.FullName + "." + parms.TestName; } else if (parms.Arguments != null) { string name = MethodHelper.GetDisplayName(method, parms.Arguments); testMethod.TestName.Name = name; testMethod.TestName.FullName = method.ReflectedType.FullName + "." + name; } if (parms.ExpectedExceptionName != null) testMethod.exceptionProcessor = new ExpectedExceptionProcessor(testMethod, parms); foreach (string key in parms.Properties.Keys) testMethod.Properties.Add(key, parms.Properties[key]); // Description is stored in parms.Properties if (parms.Description != null) testMethod.Description = parms.Description; } return testMethod; }
/// <summary> /// Return an IEnumerable providing test cases for use in /// running a parameterized test. /// </summary> /// <param name="method"></param> /// <returns></returns> public IEnumerable GetTestCasesFor(MethodInfo method) { ArrayList list = new ArrayList(); Attribute[] attrs = Reflect.GetAttributes(method, NUnitFramework.TestCaseAttribute, false); ParameterInfo[] parameters = method.GetParameters(); int argsNeeded = parameters.Length; foreach (Attribute attr in attrs) { ParameterSet parms; try { parms = ParameterSet.FromDataSource(attr); int argsProvided = parms.Arguments.Length; // Special handling for params arguments if (argsNeeded > 0 && argsProvided >= argsNeeded - 1) { ParameterInfo lastParameter = parameters[argsNeeded - 1]; Type lastParameterType = lastParameter.ParameterType; Type elementType = lastParameterType.GetElementType(); if (lastParameterType.IsArray && lastParameter.IsDefined(typeof(ParamArrayAttribute), false)) { if (argsProvided == argsNeeded) { Type lastArgumentType = parms.Arguments[argsProvided - 1].GetType(); if (!lastParameterType.IsAssignableFrom(lastArgumentType)) { Array array = Array.CreateInstance(elementType, 1); array.SetValue(parms.Arguments[argsProvided - 1], 0); parms.Arguments[argsProvided - 1] = array; } } else { object[] newArglist = new object[argsNeeded]; for (int i = 0; i < argsNeeded && i < argsProvided; i++) newArglist[i] = parms.Arguments[i]; int length = argsProvided - argsNeeded + 1; Array array = Array.CreateInstance(elementType, length); for (int i = 0; i < length; i++) array.SetValue(parms.Arguments[argsNeeded + i - 1], i); newArglist[argsNeeded - 1] = array; parms.Arguments = newArglist; argsProvided = argsNeeded; } } } //if (method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(object[])) // parms.Arguments = new object[]{parms.Arguments}; // Special handling when sole argument is an object[] if (argsNeeded == 1 && method.GetParameters()[0].ParameterType == typeof(object[])) { if (argsProvided > 1 || argsProvided == 1 && parms.Arguments[0].GetType() != typeof(object[])) { parms.Arguments = new object[] { parms.Arguments }; } } if (argsProvided == argsNeeded) PerformSpecialConversions(parms.Arguments, parameters); } catch (Exception ex) { parms = new ParameterSet( ex ); } list.Add( parms ); } return list; }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> private static bool CheckTestMethodSignature(TestMethod testMethod, ParameterSet parms) { if (testMethod.Method.IsAbstract) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is abstract"; return false; } if (!testMethod.Method.IsPublic) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is not public"; return false; } ParameterInfo[] parameters = testMethod.Method.GetParameters(); int argsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.arguments = parms.Arguments; testMethod.expectedResult = parms.Result; testMethod.RunState = parms.RunState; testMethod.IgnoreReason = parms.NotRunReason; testMethod.BuilderException = parms.ProviderException; arglist = parms.Arguments; if (arglist != null) argsProvided = arglist.Length; if (testMethod.RunState != RunState.Runnable) return false; } if (!testMethod.Method.ReturnType.Equals(typeof(void)) && (parms == null || parms.Result == null && parms.ExpectedExceptionName == null)) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method has non-void return value"; return false; } if (argsProvided > 0 && argsNeeded == 0) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Arguments provided for method not taking any"; return false; } if (argsProvided == 0 && argsNeeded > 0) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "No arguments were provided"; return false; } if (argsProvided != argsNeeded) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Wrong number of arguments provided"; return false; } #if NET_2_0 if (testMethod.Method.IsGenericMethodDefinition) { Type[] typeArguments = GetTypeArgumentsForMethod(testMethod.Method, arglist); foreach (object o in typeArguments) if (o == null) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Unable to determine type arguments for fixture"; return false; } testMethod.method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (arglist[i].GetType() != parameters[i].ParameterType && arglist[i] is IConvertible) { try { arglist[i] = Convert.ChangeType(arglist[i], parameters[i].ParameterType); } catch (Exception) { // Do nothing - the incompatible argument will be reported below } } } } #endif return true; }
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))); }
public TestMethodExtension BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms, ScreenCapture screenCapture, IDictionary properties) { //TODO : Here it doesn't support Async Await Method, BTW, the Async Await just be supported from .net 4.5 TestMethodExtension testMethod = new TestMethodExtension(method, screenCapture); string prefix = method.ReflectedType.FullName; if (parentSuite != null) { testMethod.Properties = properties; prefix = parentSuite.TestName.FullName; testMethod.TestName.FullName = prefix + "." + testMethod.TestName.Name; } if (CheckTestMethodSignature(testMethod, parms)) { if (parms == null) NUnitFramework.ApplyCommonAttributes(method, testMethod); NUnitFramework.ApplyExpectedExceptionAttribute(method, testMethod); } if (parms != null) { method = testMethod.Method; if (parms.TestName != null) { testMethod.TestName.Name = parms.TestName; testMethod.TestName.FullName = prefix + "." + parms.TestName; } else if (parms.OriginalArguments != null) { string name = MethodHelper.GetDisplayName(method, parms.OriginalArguments); testMethod.TestName.Name = name; testMethod.TestName.FullName = prefix + "." + name; } if (parms.Ignored) { testMethod.RunState = RunState.Ignored; testMethod.IgnoreReason = parms.IgnoreReason; } else if (parms.Explicit) { testMethod.RunState = RunState.Explicit; } foreach (string key in parms.Properties.Keys) testMethod.Properties[key] = parms.Properties[key]; if (parms.Description != null) testMethod.Description = parms.Description; } if (parentSuite != null) { if (parentSuite.RunState == RunState.NotRunnable && testMethod.RunState != RunState.NotRunnable) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = parentSuite.IgnoreReason; } if (parentSuite.RunState == RunState.Ignored && testMethod.RunState != RunState.Ignored && testMethod.RunState != RunState.NotRunnable) { testMethod.RunState = RunState.Ignored; testMethod.IgnoreReason = parentSuite.IgnoreReason; } } return testMethod; }
private static bool CheckTestMethodSignature(TestMethodExtension testMethod, ParameterSet parms) { if (testMethod.Method.IsAbstract) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is abstract"; return false; } if (!testMethod.Method.IsPublic) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is not public"; return false; } ParameterInfo[] parameters = testMethod.Method.GetParameters(); int argsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.arguments = parms.Arguments; testMethod.hasExpectedResult = parms.HasExpectedResult; if (testMethod.hasExpectedResult) testMethod.expectedResult = parms.Result; testMethod.RunState = parms.RunState; testMethod.IgnoreReason = parms.IgnoreReason; testMethod.BuilderException = parms.ProviderException; arglist = parms.Arguments; if (arglist != null) argsProvided = arglist.Length; if (testMethod.RunState != RunState.Runnable) return false; } #if CLR_2_0 || CLR_4_0 bool isAsyncMethod = Reflect.IsAsyncMethod(testMethod.Method); bool hasMeaningfulReturnType = isAsyncMethod ? testMethod.Method.ReturnType.IsGenericType : testMethod.Method.ReturnType != typeof(void); #else bool hasMeaningfulReturnType = testMethod.Method.ReturnType != typeof(void); #endif if (hasMeaningfulReturnType && (parms == null || !parms.HasExpectedResult && parms.ExpectedExceptionName == null)) return MarkAsNotRunnable(testMethod, "Test method has non-void return type, but no result is expected"); if (!hasMeaningfulReturnType && parms != null && parms.HasExpectedResult) return MarkAsNotRunnable(testMethod, "Test method has void return type, but a result is expected"); if (argsProvided > 0 && argsNeeded == 0) return MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any"); if (argsProvided == 0 && argsNeeded > 0) return MarkAsNotRunnable(testMethod, "No arguments were provided"); if (argsProvided != argsNeeded) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Wrong number of arguments provided"; return false; } #if CLR_2_0 || CLR_4_0 if (testMethod.Method.IsGenericMethodDefinition) { Type[] typeArguments = GetTypeArgumentsForMethod(testMethod.Method, arglist); foreach (object o in typeArguments) if (o == null) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Unable to determine type arguments for fixture"; return false; } testMethod.method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (arglist[i].GetType() != parameters[i].ParameterType && arglist[i] is IConvertible) { try { arglist[i] = Convert.ChangeType(arglist[i], parameters[i].ParameterType); } catch (Exception) { // Do nothing - the incompatible argument will be reported below } } } } #endif return true; }
public new Test BuildParameterizedMethodSuite(MethodInfo method, Test parentSuite) { ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method); NUnitFramework.ApplyCommonAttributes(method, methodSuite); if (parentSuite != null) { if (parentSuite.RunState == RunState.NotRunnable && methodSuite.RunState != RunState.NotRunnable) { methodSuite.RunState = RunState.NotRunnable; methodSuite.IgnoreReason = parentSuite.IgnoreReason; } if (parentSuite.RunState == RunState.Ignored && methodSuite.RunState != RunState.Ignored && methodSuite.RunState != RunState.NotRunnable) { methodSuite.RunState = RunState.Ignored; methodSuite.IgnoreReason = parentSuite.IgnoreReason; } } ITestCaseProvider2 testCaseProvider = Host.GetExtensionPoint("TestCaseProviders") as ITestCaseProvider2; foreach (object source in testCaseProvider.GetTestCasesFor(method, parentSuite)) { ParameterSet parms; if (source == null) { parms = new ParameterSet(); parms.Arguments = new object[] { null }; } else parms = source as ParameterSet; if (parms == null) { if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null) parms = ParameterSet.FromDataSource(source); else { parms = new ParameterSet(); ParameterInfo[] parameters = method.GetParameters(); Type sourceType = source.GetType(); if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType)) parms.Arguments = new object[] { source }; else if (source is object[]) parms.Arguments = (object[])source; else if (source is Array) { Array array = (Array)source; if (array.Rank == 1) { parms.Arguments = new object[array.Length]; for (int i = 0; i < array.Length; i++) parms.Arguments[i] = (object)array.GetValue(i); } } else parms.Arguments = new object[] { source }; } } TestMethod test = BuildSingleTestMethod(method, parentSuite, parms, this.screenCapture, methodSuite.Properties); methodSuite.Add(test); } return methodSuite; }