<PackageReference Include="NUnit" Version="4.1.0" />

DefaultTestCaseBuilder

Class to build ether a parameterized or a normal NUnitTestMethod. There are four cases that the builder must deal with: 1. The method needs no params and none are provided 2. The method needs params and they are provided 3. The method needs no params but they are provided in error 4. The method needs params but they are not provided This could have been done using two different builders, but it turned out to be simpler to have just one. The BuildFrom method takes a different branch depending on whether any parameters are provided, but all four cases are dealt with in lower-level methods
using NUnit.Framework.Interfaces; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; namespace NUnit.Framework.Internal.Builders { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class DefaultTestCaseBuilder : ITestCaseBuilder { private readonly NUnitTestCaseBuilder _nunitTestCaseBuilder = new NUnitTestCaseBuilder(); public bool CanBuildFrom(IMethodInfo method) { if (!method.IsDefined<ITestBuilder>(true)) return method.IsDefined<ISimpleTestBuilder>(true); return true; } public Test BuildFrom(IMethodInfo method) { return BuildFrom(method, null); } public bool CanBuildFrom(IMethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] Test parentSuite) { return CanBuildFrom(method); } public Test BuildFrom(IMethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] Test parentSuite) { List<TestMethod> list = new List<TestMethod>(); try { MethodInfoCache.TestMethodMetadata testMethodMetadata = MethodInfoCache.Get(method); List<ITestBuilder> list2 = new List<ITestBuilder>(testMethodMetadata.TestBuilderAttributes); if (method.MethodInfo.GetParameters().Any((ParameterInfo param) => param.HasAttribute<IParameterDataSource>(false)) && !list2.Any((ITestBuilder builder) => builder is CombiningStrategyAttribute)) list2.Add(new CombinatorialAttribute()); foreach (ITestBuilder item in list2) { foreach (TestMethod item2 in item.BuildFrom(method, parentSuite)) { list.Add(item2); } } return ((list2.Count > 0 && method.GetParameters().Length != 0) || list.Count > 0) ? BuildParameterizedMethodSuite(method, list) : BuildSingleTestMethod(method, parentSuite); } catch (Exception exception) { TestMethod testMethod = new TestMethod(method, parentSuite); testMethod.MakeInvalid(exception, "Failure building Test"); return testMethod; } } private Test BuildParameterizedMethodSuite(IMethodInfo method, IEnumerable<TestMethod> tests) { ParameterizedMethodSuite parameterizedMethodSuite = new ParameterizedMethodSuite(method); parameterizedMethodSuite.ApplyAttributesToTest(method.MethodInfo); foreach (TestMethod test in tests) { parameterizedMethodSuite.Add(test); } return parameterizedMethodSuite; } private Test BuildSingleTestMethod(IMethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] Test suite) { ISimpleTestBuilder[] customAttributes = method.GetCustomAttributes<ISimpleTestBuilder>(false); if (customAttributes.Length == 0) return _nunitTestCaseBuilder.BuildTestMethod(method, suite, null); return customAttributes[0].BuildFrom(method, suite); } } }