本文整理汇总了C#中IAttributeInfo.GetNamedArgument方法的典型用法代码示例。如果您正苦于以下问题:C# IAttributeInfo.GetNamedArgument方法的具体用法?C# IAttributeInfo.GetNamedArgument怎么用?C# IAttributeInfo.GetNamedArgument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAttributeInfo
的用法示例。
在下文中一共展示了IAttributeInfo.GetNamedArgument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
{
string displayNameBase = factAttribute.GetNamedArgument<string>("DisplayName") ?? type.Name + "." + method.Name;
ITypeInfo[] resolvedTypes = null;
if (arguments != null && method.IsGenericMethodDefinition)
{
resolvedTypes = ResolveGenericTypes(method, arguments);
method = method.MakeGenericMethod(resolvedTypes);
}
Assembly = assembly;
Class = type;
Method = method;
Arguments = arguments;
DisplayName = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
SkipReason = factAttribute.GetNamedArgument<string>("Skip");
Traits = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
TestCollection = testCollection;
foreach (IAttributeInfo traitAttribute in Method.GetCustomAttributes(typeof(TraitAttribute))
.Concat(Class.GetCustomAttributes(typeof(TraitAttribute))))
{
var ctorArgs = traitAttribute.GetConstructorArguments().ToList();
Traits.Add((string)ctorArgs[0], (string)ctorArgs[1]);
}
uniqueID = new Lazy<string>(GetUniqueID, true);
}
示例2: Discover
public virtual IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
var variations = testMethod.Method
.GetCustomAttributes(typeof(BenchmarkVariationAttribute))
.ToDictionary(
a => a.GetNamedArgument<string>(nameof(BenchmarkVariationAttribute.VariationName)),
a => a.GetNamedArgument<object[]>(nameof(BenchmarkVariationAttribute.Data)));
if (!variations.Any())
{
variations.Add("Default", new object[0]);
}
var tests = new List<IXunitTestCase>();
foreach (var variation in variations)
{
tests.Add(new BenchmarkTestCase(
factAttribute.GetNamedArgument<int>(nameof(BenchmarkAttribute.Iterations)),
factAttribute.GetNamedArgument<int>(nameof(BenchmarkAttribute.WarmupIterations)),
variation.Key,
_diagnosticMessageSink,
testMethod,
variation.Value));
}
return tests;
}
示例3: Initialize
void Initialize(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo type, IMethodInfo method, IAttributeInfo factAttribute, object[] arguments)
{
string displayNameBase = factAttribute.GetNamedArgument<string>("DisplayName") ?? type.Name + "." + method.Name;
ITypeInfo[] resolvedTypes = null;
if (arguments != null && method.IsGenericMethodDefinition)
{
resolvedTypes = ResolveGenericTypes(method, arguments);
method = method.MakeGenericMethod(resolvedTypes);
}
Assembly = assembly;
Class = type;
Method = method;
Arguments = arguments;
DisplayName = GetDisplayNameWithArguments(displayNameBase, arguments, resolvedTypes);
SkipReason = factAttribute.GetNamedArgument<string>("Skip");
Traits = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
TestCollection = testCollection;
foreach (var traitAttribute in Method.GetCustomAttributes(typeof(ITraitAttribute))
.Concat(Class.GetCustomAttributes(typeof(ITraitAttribute))))
{
var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).First();
var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(discovererAttribute);
if (discoverer != null)
foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
Traits.Add(keyValuePair.Key, keyValuePair.Value);
}
uniqueID = new Lazy<string>(GetUniqueID, true);
}
示例4: Discover
public virtual IEnumerable<IXunitTestCase> Discover(
ITestFrameworkDiscoveryOptions discoveryOptions,
ITestMethod testMethod,
IAttributeInfo factAttribute)
{
var variations = testMethod.Method
.GetCustomAttributes(typeof(BenchmarkVariationAttribute))
.Select(a => new
{
Name = a.GetNamedArgument<string>(nameof(BenchmarkVariationAttribute.VariationName)),
TestMethodArguments = a.GetNamedArgument<object[]>(nameof(BenchmarkVariationAttribute.Data)),
Framework = a.GetNamedArgument<string>(nameof(BenchmarkVariationAttribute.Framework))
})
.ToList();
if (!variations.Any())
{
variations.Add(new
{
Name = "Default",
TestMethodArguments = new object[0],
Framework = (string)null
});
}
var tests = new List<IXunitTestCase>();
foreach (var variation in variations)
{
if (BenchmarkConfig.Instance.RunIterations)
{
tests.Add(new BenchmarkTestCase(
factAttribute.GetNamedArgument<int>(nameof(BenchmarkAttribute.Iterations)),
factAttribute.GetNamedArgument<int>(nameof(BenchmarkAttribute.WarmupIterations)),
variation.Framework,
variation.Name,
_diagnosticMessageSink,
testMethod,
variation.TestMethodArguments));
}
else
{
tests.Add(new NonCollectingBenchmarkTestCase(
variation.Name,
_diagnosticMessageSink,
testMethod,
variation.TestMethodArguments));
}
}
return tests;
}
示例5: Discover
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();
return factAttribute.GetNamedArgument<string>("Skip") != null
? new[] { new XunitTestCase(_diagnosticMessageSink, defaultMethodDisplay, testMethod) }
: new XunitTestCase[] { new ScenarioTestCase(_diagnosticMessageSink, defaultMethodDisplay, testMethod) };
}
示例6: Discover
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
var maxRetries = factAttribute.GetNamedArgument<int>("MaxRetries");
if (maxRetries < 1)
maxRetries = 3;
yield return new RetryTestCase(diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod, maxRetries);
}
示例7: KuduXunitTheoryTestCase
public KuduXunitTheoryTestCase(IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay,
ITestMethod testMethod,
IAttributeInfo testAttribute)
: base(diagnosticMessageSink, defaultMethodDisplay, testMethod)
{
DisableRetry = testAttribute == null ? true : testAttribute.GetNamedArgument<bool>("DisableRetry");
}
示例8: KuduXunitTestCase
public KuduXunitTestCase(IMessageSink diagnosticMessageSink,
TestMethodDisplay testMethodDisplay,
ITestMethod testMethod,
object[] testMethodArguments,
IAttributeInfo testAttribute)
: base(diagnosticMessageSink, testMethodDisplay, testMethod, testMethodArguments)
{
DisableRetry = testAttribute == null ? true : testAttribute.GetNamedArgument<bool>("DisableRetry");
}
示例9: Discover
public virtual IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
var variations = testMethod.Method
.GetCustomAttributes(typeof(BenchmarkVariationAttribute))
.ToDictionary(
a => a.GetNamedArgument<string>(nameof(BenchmarkVariationAttribute.VariationName)),
a => a.GetNamedArgument<object[]>(nameof(BenchmarkVariationAttribute.Data)));
if (!variations.Any())
{
variations.Add("Default", new object[0]);
}
var tests = new List<IXunitTestCase>();
foreach (var variation in variations)
{
if (BenchmarkConfig.Instance.RunIterations)
{
tests.Add(new BenchmarkTestCase(
factAttribute.GetNamedArgument<int>(nameof(BenchmarkAttribute.Iterations)),
factAttribute.GetNamedArgument<int>(nameof(BenchmarkAttribute.WarmupIterations)),
variation.Key,
_diagnosticMessageSink,
testMethod,
variation.Value));
}
else
{
// TODO running a single iteration is slow under DNX (see #2574)
// disabling so that we don't add 10min to build.cmd
#if !DNX451 && !DNXCORE50
var args = new[] { new MetricCollector() }
.Concat(variation.Value)
.ToArray();
tests.Add(new XunitTestCase(_diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod, args));
#endif
}
}
return tests;
}
示例10: Discover
/// <summary>
/// Discover test cases from a test method.
/// </summary>
/// <remarks>
/// This method performs the following steps:
/// - If the theory attribute is marked with Skip, returns the single test case from <see cref="CreateTestCaseForSkip"/>;
/// - If pre-enumeration is off, or any of the test data is non serializable, returns the single test case from <see cref="CreateTestCaseForTheory"/>;
/// - If there is no theory data, returns a single test case of <see cref="ExecutionErrorTestCase"/> with the error in it;
/// - Otherwise, it returns one test case per data row, created by calling <see cref="CreateTestCaseForDataRow"/>.
/// </remarks>
/// <param name="discoveryOptions">The discovery options to be used.</param>
/// <param name="testMethod">The test method the test cases belong to.</param>
/// <param name="theoryAttribute">The theory attribute attached to the test method.</param>
/// <returns>Returns zero or more test cases represented by the test method.</returns>
public virtual IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo theoryAttribute)
{
// Special case Skip, because we want a single Skip (not one per data item); plus, a skipped test may
// not actually have any data (which is quasi-legal, since it's skipped).
var skipReason = theoryAttribute.GetNamedArgument<string>("Skip");
if (skipReason != null)
return new[] { CreateTestCaseForSkip(discoveryOptions, testMethod, theoryAttribute, skipReason) };
if (discoveryOptions.PreEnumerateTheoriesOrDefault())
{
try
{
var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));
var results = new List<IXunitTestCase>();
foreach (var dataAttribute in dataAttributes)
{
var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };
// GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
// down below so that we get the composite test case.
foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
{
// Determine whether we can serialize the test case, since we need a way to uniquely
// identify a test and serialization is the best way to do that. If it's not serializable,
// this will throw and we will fall back to a single theory test case that gets its data at runtime.
if (!SerializationHelper.IsSerializable(dataRow))
return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };
var testCase = CreateTestCaseForDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow);
results.Add(testCase);
}
}
if (results.Count == 0)
results.Add(new ExecutionErrorTestCase(diagnosticMessageSink,
discoveryOptions.MethodDisplayOrDefault(),
testMethod,
$"No data found for {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}"));
return results;
}
catch { } // If something goes wrong, fall through to return just the XunitTestCase
}
return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };
}
示例11: Discover
/// <inheritdoc/>
public IEnumerable<IXunitTestCase> Discover(ITestMethod testMethod, IAttributeInfo factAttribute)
{
// Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
// not actually have any data (which is quasi-legal, since it's skipped).
if (factAttribute.GetNamedArgument<string>("Skip") != null)
return new[] { new XunitTestCase(testMethod) };
var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));
try
{
using (var memoryStream = new MemoryStream())
{
var results = new List<XunitTestCase>();
foreach (var dataAttribute in dataAttributes)
{
var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(discovererAttribute);
if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
return new XunitTestCase[] { new XunitTheoryTestCase(testMethod) };
// GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
// down below so that we get the composite test case.
foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
{
// Attempt to serialize the test case, since we need a way to uniquely identify a test
// and serialization is the best way to do that. If it's not serializable, this will
// throw and we will fall back to a single theory test case that gets its data
// at runtime.
var testCase = new XunitTestCase(testMethod, dataRow);
SerializationHelper.Serialize(testCase);
results.Add(testCase);
}
}
// REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
if (results.Count == 0)
results.Add(new LambdaTestCase(testMethod,
() => { throw new InvalidOperationException(String.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)); }));
return results;
}
}
catch
{
return new XunitTestCase[] { new XunitTheoryTestCase(testMethod) };
}
}
示例12: Discover
/// <inheritdoc/>
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();
// Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
// not actually have any data (which is quasi-legal, since it's skipped).
if (factAttribute.GetNamedArgument<string>("Skip") != null)
return new[] { new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };
var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));
if (discoveryOptions.PreEnumerateTheoriesOrDefault())
{
try
{
var results = new List<XunitTestCase>();
foreach (var dataAttribute in dataAttributes)
{
var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };
// GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
// down below so that we get the composite test case.
foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
{
// Attempt to serialize the test case, since we need a way to uniquely identify a test
// and serialization is the best way to do that. If it's not serializable, this will
// throw and we will fall back to a single theory test case that gets its data
// at runtime.
var testCase = new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod, dataRow);
SerializationHelper.Serialize(testCase);
results.Add(testCase);
}
}
if (results.Count == 0)
results.Add(new ExecutionErrorTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod,
String.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)));
return results;
}
catch { } // If there are serialization issues with the theory data, fall through to return just the XunitTestCase
}
return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };
}
示例13: Discover
/// <inheritdoc/>
public IEnumerable<XunitTestCase> Discover(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo testClass, IMethodInfo testMethod, IAttributeInfo factAttribute)
{
// Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
// not actually have any data (which is quasi-legal, since it's skipped).
if (factAttribute.GetNamedArgument<string>("Skip") != null)
return new[] { new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute) };
try
{
using (var memoryStream = new MemoryStream())
{
List<XunitTestCase> results = new List<XunitTestCase>();
var dataAttributes = testMethod.GetCustomAttributes(typeof(DataAttribute));
foreach (var dataAttribute in dataAttributes)
{
var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
var args = discovererAttribute.GetConstructorArguments().Cast<string>().ToList();
var discovererType = Reflector.GetType(args[1], args[0]);
IDataDiscoverer discoverer = (IDataDiscoverer)Activator.CreateInstance(discovererType);
// GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
// down below so that we get the composite test case.
foreach (object[] dataRow in discoverer.GetData(dataAttribute, testMethod))
{
// Attempt to serialize the test case, since we need a way to uniquely identify a test
// and serialization is the best way to do that. If it's not serializable, this will
// throw and we will fall back to a single theory test case that gets its data
// at runtime.
var testCase = new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute, dataRow);
SerializationHelper.Serialize(testCase);
results.Add(testCase);
}
}
// REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
if (results.Count == 0)
results.Add(new LambdaTestCase(testCollection, assembly, testClass, testMethod, factAttribute, () => { throw new InvalidOperationException("No data found for " + testClass.Name + "." + testMethod.Name); }));
return results;
}
}
catch
{
return new XunitTestCase[] { new XunitTheoryTestCase(testCollection, assembly, testClass, testMethod, factAttribute) };
}
}
示例14: Discover
public override IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo benchmarkAttribute)
{
var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();
//
// Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
// not actually have any data (which is quasi-legal, since it's skipped).
//
if (benchmarkAttribute.GetNamedArgument<string>("Skip") != null)
{
yield return new XunitTestCase(_diagnosticMessageSink, defaultMethodDisplay, testMethod);
yield break;
}
//
// Use the TheoryDiscoverer to enumerate the cases. We can't do this, because
// xUnit doesn't expose everything we need (for example, the ability to ask if an
// object is xUnit-serializable).
//
foreach (var theoryCase in base.Discover(discoveryOptions, testMethod, benchmarkAttribute))
{
if (theoryCase is XunitTheoryTestCase)
{
//
// TheoryDiscoverer returns one of these if it cannot enumerate the cases now.
// We'll return a BenchmarkTestCase with no data associated.
//
yield return new BenchmarkTestCase(_diagnosticMessageSink, defaultMethodDisplay, testMethod, benchmarkAttribute);
}
else
{
//
// This is a test case with data
//
yield return new BenchmarkTestCase(_diagnosticMessageSink, defaultMethodDisplay, testMethod, benchmarkAttribute, theoryCase.TestMethodArguments);
}
}
}
示例15: GetSkipReason
/// <summary>
/// Gets the skip reason for the test case. By default, pulls the skip reason from the
/// <see cref="FactAttribute.Skip"/> property.
/// </summary>
/// <param name="factAttribute">The fact attribute the decorated the test case.</param>
/// <returns>The skip reason, if skipped; <c>null</c>, otherwise.</returns>
protected virtual string GetSkipReason(IAttributeInfo factAttribute)
=> factAttribute.GetNamedArgument<string>("Skip");