-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generic combinatorial attributes #95
Comments
In my implementation, I also introduced the following interface: /// <summary>
/// Defines a value provider that provides values for a parameter on a test method.
/// </summary>
public interface ICombinatorialValuesProvider
{
/// <summary>
/// Gets the values that should be passed to this parameter on the test method.
/// </summary>
/// <value>An array of values.</value>
object?[] Values { get; }
} This reduces the implementation in internal static IEnumerable<object?> GetValuesFor(ParameterInfo parameter)
{
Requires.NotNull(parameter, nameof(parameter));
ICombinatorialValuesProvider? valuesSource = parameter.GetCustomAttributes().OfType<ICombinatorialValuesProvider>().SingleOrDefault();
if (valuesSource is not null)
{
return valuesSource.Values;
}
{
CombinatorialMemberDataAttribute? attribute = parameter.GetCustomAttribute<CombinatorialMemberDataAttribute>();
if (attribute is not null)
{
return attribute.GetValues(parameter);
}
}
return GetValuesFor(parameter.ParameterType);
} Technically, the interface could define |
Related to #94 |
The existing Warning: Ramblings ahead... As I see it, allowing users to create a single class containing shared values used across tests makes sense. Using the values attribute, I imagine something like this: public void Test([CombinatorialValues(typeof(MySharedTestData), nameof(MySharedTestData.Values)] SharedTestData testData) And a new generic version (as this issue is about): public void Test([CombinatorialValues<MySharedTestData>(nameof(MySharedTestData.Values))] SharedTestData testData)
// Alternatively
public void Test([CombinatorialValues<MySharedTestData>("Values")] SharedTestData testData) An overload to public CombinatorialValues(Type valuesSource){} // New
public CombinatorialValues(params object?[]? values){} // Existing And the generic version public class CombinatorialValues<T> : CombinatorialValues
where T : class, IEnumerable
{
public CombinatorialValues()
: base(typeof(T))
{
}
} However, this might be confusing or even impossible to use since the attribute constructor already accepts public void Test[CombinatorialValues(typeof(SomeTypeToTest)] Type t) // This will resolve the new constructor I know this scenario makes no sense combinatorially, so if this is preferred, the params constructor should throw if only a single public void Test([CombinatorialValues(typeof(Type1), typeof(Type2))] Type someType)
{
// I'm getting Type1 and Type2 as expected
}
public void Test([CombinatorialValues(typeof(Type1))] Type someType)
{
// This change will break that behavior and throw since the type is now being treated as a value source that doesn't implement IEnumerable (or it might, who knows)
} As a final idea, the existing |
@siewers I approve of your If you can submit pull requests for these soon, we can get all your ideas in together in the next release. |
@AArnott, I've submitted the PR. I have to review my comments above since the PR is based on some of the thoughts we haven't agreed on yet. |
I'd like to propose a combinatorial attribute equivalent to the xUnit
ClassDataAttribute
allowing reuse of the same data source values across multiple (combinatorial) tests.I've implemented and tested the following:
In .NET Standard 2.0, there's the option to use generic attributes, which I've implemented like this:
This will allow users to write a test like this:
I think this could be a valuable addition to this great library.
The text was updated successfully, but these errors were encountered: