-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from AArnott/v1.7_for_main
Merge v1.7 for main
- Loading branch information
Showing
18 changed files
with
554 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,3 +358,6 @@ MigrationBackup/ | |
|
||
# Analysis results | ||
*.sarif | ||
|
||
# JetBrains Rider | ||
.idea |
80 changes: 80 additions & 0 deletions
80
src/Xunit.Combinatorial/CombinatorialClassDataAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Andrew Arnott. All rights reserved. | ||
// Licensed under the Ms-PL license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections; | ||
using System.Globalization; | ||
using System.Reflection; | ||
|
||
namespace Xunit; | ||
|
||
/// <summary> | ||
/// Specifies a class that provides the values for a combinatorial test. | ||
/// </summary> | ||
public class CombinatorialClassDataAttribute : Attribute, ICombinatorialValuesProvider | ||
{ | ||
private readonly object?[] values; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CombinatorialClassDataAttribute" /> class. | ||
/// </summary> | ||
/// <param name="valuesSourceType">The type of the class that provides the values for a combinatorial test.</param> | ||
/// <param name="arguments">The arguments to pass to the constructor of <paramref name="valuesSourceType" />.</param> | ||
public CombinatorialClassDataAttribute(Type valuesSourceType, params object[]? arguments) | ||
{ | ||
this.values = GetValues(valuesSourceType, arguments); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public object?[] GetValues(ParameterInfo parameter) | ||
{ | ||
return this.values; | ||
} | ||
|
||
private static object?[] GetValues(Type valuesSourceType, object[]? args) | ||
{ | ||
Requires.NotNull(valuesSourceType, nameof(valuesSourceType)); | ||
|
||
EnsureValidValuesSourceType(valuesSourceType); | ||
|
||
IEnumerable? values; | ||
|
||
try | ||
{ | ||
values = (IEnumerable)Activator.CreateInstance( | ||
valuesSourceType, | ||
BindingFlags.CreateInstance | BindingFlags.OptionalParamBinding, | ||
null, | ||
args, | ||
CultureInfo.InvariantCulture); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Failed to create an instance of {valuesSourceType}. " + | ||
$"Please make sure the type has a public constructor and the arguments match.", | ||
ex); | ||
} | ||
|
||
if (TheoryDataHelper.TryGetTheoryDataValues(values, out object?[]? data)) | ||
{ | ||
return data; | ||
} | ||
|
||
return values.Cast<object[]>().SelectMany(rows => rows).ToArray(); | ||
} | ||
|
||
private static void EnsureValidValuesSourceType(Type valuesSourceType) | ||
{ | ||
if (typeof(IEnumerable<object[]>).IsAssignableFrom(valuesSourceType)) | ||
{ | ||
return; | ||
} | ||
|
||
if (TheoryDataHelper.IsTheoryDataType(valuesSourceType)) | ||
{ | ||
return; | ||
} | ||
|
||
throw new InvalidOperationException($"The values source {valuesSourceType} must be assignable to {typeof(IEnumerable<object[]>)}), {typeof(TheoryData<>)} or {typeof(TheoryDataBase<,>)}."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Andrew Arnott. All rights reserved. | ||
// Licensed under the Ms-PL license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
|
||
namespace Xunit; | ||
|
||
/// <summary> | ||
/// An interface 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> | ||
/// <param name="parameter">The parameter to get values for.</param> | ||
/// <returns>An array of values.</returns> | ||
object?[] GetValues(ParameterInfo parameter); | ||
} |
Oops, something went wrong.