Migrate Test Method With Generics #945
Answered
by
thomhurst
viceroypenguin
asked this question in
Q&A
-
Current XUnit Code: [Theory]
[MemberData(nameof(AggregateBy_TestData))]
public static void AggregateBy_HasExpectedOutput<TSource, TKey, TAccumulate>(
IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TKey, TAccumulate> seedSelector,
Func<TAccumulate, TSource, TAccumulate> func,
IEqualityComparer<TKey>? comparer,
IEnumerable<KeyValuePair<TKey, TAccumulate>> expected) where TKey : notnull
{
using var ts = source.AsTestingSequence();
SuperEnumerable
.AggregateBy(ts, keySelector, seedSelector, func, comparer)
.AssertCollectionEqual(expected);
}
public static IEnumerable<object?[]> AggregateBy_TestData()
{
yield return WrapArgs(
source: Enumerable.Range(0, 10),
keySelector: x => x,
seedSelector: x => 0,
func: (x, y) => x + y,
comparer: null,
expected: Enumerable.Range(0, 10).ToDictionary(x => x, x => x));
yield return WrapArgs(
source: ["Bob", "bob", "tim", "Bob", "Tim"],
keySelector: x => x,
seedSelector: x => "",
func: (x, y) => x + y,
comparer: null,
expected: new Dictionary<string, string>(StringComparer.Ordinal)
{
{ "Bob", "BobBob" },
{ "bob", "bob" },
{ "tim", "tim" },
{ "Tim", "Tim" },
});
// .. other test cases
static object?[] WrapArgs<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TAccumulate>> expected)
=> [source, keySelector, seedSelector, func, comparer, expected];
} What would be the best way to migrate this to TUnit |
Beta Was this translation helpful? Give feedback.
Answered by
thomhurst
Oct 20, 2024
Replies: 1 comment 10 replies
-
That is some complicated test haha, just trying to wrap my head around what it's doing! I see you've raised an issue, is that to sort this? I think extending method data source to allow inputs seems like a good idea so I'm happy to try and incorporate that |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@viceroypenguin I had to make some changes to TUnit to support generic tests like this, but it works!
Example here: https://github.com/thomhurst/TUnit/blob/main/TUnit.TestProject/GenericMethodTests.cs