Skip to content

Commit

Permalink
Use TheoryData
Browse files Browse the repository at this point in the history
Increases type safety.
Reduces some cruft/boilerplate
  • Loading branch information
jnyrup committed Dec 20, 2022
1 parent c611a94 commit ce207ca
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 181 deletions.
36 changes: 18 additions & 18 deletions Tests/FluentAssertions.Specs/AssertionExtensionsSpecs.cs
Expand Up @@ -36,26 +36,26 @@ private static bool OverridesEquals(Type t)
return equals is not null;
}

public static object[][] ClassesWithGuardEquals() => new object[][]
public static TheoryData<object> ClassesWithGuardEquals => new()
{
new object[] { new ObjectAssertions<object, ObjectAssertions>(default) },
new object[] { new BooleanAssertions<BooleanAssertions>(default) },
new object[] { new DateTimeAssertions<DateTimeAssertions>(default) },
new object[] { new DateTimeRangeAssertions<DateTimeAssertions>(default, default, default, default) },
new object[] { new DateTimeOffsetAssertions<DateTimeOffsetAssertions>(default) },
new object[] { new DateTimeOffsetRangeAssertions<DateTimeOffsetAssertions>(default, default, default, default) },
new object[] { new ExecutionTimeAssertions(new ExecutionTime(() => { }, () => new StopwatchTimer())) },
new object[] { new GuidAssertions<GuidAssertions>(default) },
new object[] { new MethodInfoSelectorAssertions() },
new object[] { new NumericAssertions<int, NumericAssertions<int>>(default) },
new object[] { new PropertyInfoSelectorAssertions() },
new object[] { new SimpleTimeSpanAssertions<SimpleTimeSpanAssertions>(default) },
new object[] { new TaskCompletionSourceAssertions<int>(default) },
new object[] { new TypeSelectorAssertions() },
new object[] { new EnumAssertions<StringComparison, EnumAssertions<StringComparison>>(default) },
new ObjectAssertions<object, ObjectAssertions>(default),
new BooleanAssertions<BooleanAssertions>(default),
new DateTimeAssertions<DateTimeAssertions>(default),
new DateTimeRangeAssertions<DateTimeAssertions>(default, default, default, default),
new DateTimeOffsetAssertions<DateTimeOffsetAssertions>(default),
new DateTimeOffsetRangeAssertions<DateTimeOffsetAssertions>(default, default, default, default),
new ExecutionTimeAssertions(new ExecutionTime(() => { }, () => new StopwatchTimer())),
new GuidAssertions<GuidAssertions>(default),
new MethodInfoSelectorAssertions(),
new NumericAssertions<int, NumericAssertions<int>>(default),
new PropertyInfoSelectorAssertions(),
new SimpleTimeSpanAssertions<SimpleTimeSpanAssertions>(default),
new TaskCompletionSourceAssertions<int>(default),
new TypeSelectorAssertions(),
new EnumAssertions<StringComparison, EnumAssertions<StringComparison>>(default),
#if NET6_0_OR_GREATER
new object[] { new DateOnlyAssertions<DateOnlyAssertions>(default) },
new object[] { new TimeOnlyAssertions<TimeOnlyAssertions>(default) },
new DateOnlyAssertions<DateOnlyAssertions>(default),
new TimeOnlyAssertions<TimeOnlyAssertions>(default),
#endif
};

Expand Down
Expand Up @@ -2681,9 +2681,9 @@ public static object[] Dictionaries()
{
return new object[]
{
new Dictionary<int, int>() { [1] = 42 },
new TrueReadOnlyDictionary<int, int>(new Dictionary<int, int>() { [1] = 42 }),
new List<KeyValuePair<int, int>> { new KeyValuePair<int, int>(1, 42) }
new Dictionary<int, int>() { [1] = 42 },
new TrueReadOnlyDictionary<int, int>(new Dictionary<int, int>() { [1] = 42 }),
new List<KeyValuePair<int, int>> { new KeyValuePair<int, int>(1, 42) }
};
}

Expand Down
Expand Up @@ -200,7 +200,7 @@ public async Task When_the_expected_exception_is_not_wrapped_on_UI_thread_async_
}
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters

public static IEnumerable<object[]> AggregateExceptionTestData()
public static TheoryData<Func<Task>, Exception> AggregateExceptionTestData()
{
var tasks = new Func<Task>[]
{
Expand All @@ -215,15 +215,19 @@ public static IEnumerable<object[]> AggregateExceptionTestData()
new InvalidOperationException()
};

var data = new TheoryData<Func<Task>, Exception>();

foreach (var task in tasks)
{
foreach (var type in types)
{
yield return new object[] { task, type };
data.Add(task, type);
}
}

yield return new object[] { (Func<Task>)EmptyAggregateException, new AggregateException() };
data.Add(EmptyAggregateException, new AggregateException());

return data;
}

private static Task AggregateExceptionWithLeftNestedException()
Expand Down
Expand Up @@ -50,7 +50,7 @@ public void When_the_expected_exception_is_not_wrapped_it_should_fail<T>(Action
}
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters

public static IEnumerable<object[]> AggregateExceptionTestData()
public static TheoryData<Action, Exception> AggregateExceptionTestData()
{
var tasks = new Action[]
{
Expand All @@ -65,15 +65,19 @@ public static IEnumerable<object[]> AggregateExceptionTestData()
new InvalidOperationException()
};

var data = new TheoryData<Action, Exception>();

foreach (var task in tasks)
{
foreach (var type in types)
{
yield return new object[] { task, type };
data.Add(task, type);
}
}

yield return new object[] { (Action)EmptyAggregateException, new AggregateException() };
data.Add(EmptyAggregateException, new AggregateException());

return data;
}

private static void AggregateExceptionWithLeftNestedException()
Expand Down
Expand Up @@ -63,7 +63,7 @@ public void When_the_expected_exception_is_not_wrapped_it_should_fail<T>(Func<in
}
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters

public static IEnumerable<object[]> AggregateExceptionTestData()
public static TheoryData<Func<int>, Exception> AggregateExceptionTestData()
{
var tasks = new Func<int>[]
{
Expand All @@ -78,15 +78,19 @@ public static IEnumerable<object[]> AggregateExceptionTestData()
new InvalidOperationException()
};

var data = new TheoryData<Func<int>, Exception>();

foreach (var task in tasks)
{
foreach (var type in types)
{
yield return new object[] { task, type };
data.Add(task, type);
}
}

yield return new object[] { (Func<int>)EmptyAggregateException, new AggregateException() };
data.Add(EmptyAggregateException, new AggregateException());

return data;
}

private static int AggregateExceptionWithLeftNestedException()
Expand Down
37 changes: 19 additions & 18 deletions Tests/FluentAssertions.Specs/Extensions/ObjectExtensionsSpecs.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions.Common;
using FluentAssertions.Specs.Common;
using Xunit;

namespace FluentAssertions.Specs.Extensions;
Expand All @@ -22,25 +23,25 @@ public void When_comparing_non_equivalent_boxed_numerics_it_should_fail(object a
success.Should().BeFalse();
}

public static IEnumerable<object[]> GetNonEquivalentNumericData()
public static TheoryData<object, object> GetNonEquivalentNumericData => new()
{
yield return new object[] { double.Epsilon, 0M }; // double.Epsilon cannot be represented in Decimal
yield return new object[] { 0M, double.Epsilon };
yield return new object[] { double.Epsilon, 0.3M }; // 0.3M cannot be represented in double
yield return new object[] { 0.3M, double.Epsilon };
yield return new object[] { (byte)2, 256 }; // 256 cannot be represented in byte
yield return new object[] { 256, (byte)2 };
yield return new object[] { -1, (ushort)65535 }; // 65535 is -1 casted to ushort
yield return new object[] { (ushort)65535, -1 };
yield return new object[] { 0.02d, 0 };
yield return new object[] { 0, 0.02d };
yield return new object[] { 0.02f, 0 };
yield return new object[] { 0, 0.02f };
yield return new object[] { long.MaxValue, 9.22337204E+18 };
yield return new object[] { 9.22337204E+18, long.MaxValue };
yield return new object[] { 9223372030000000000L, 9.22337204E+18 };
yield return new object[] { 9.22337204E+18, 9223372030000000000L };
}
{ double.Epsilon, 0M }, // double.Epsilon cannot be represented in Decimal
{ 0M, double.Epsilon },
{ double.Epsilon, 0.3M }, // 0.3M cannot be represented in double
{ 0.3M, double.Epsilon },
{ (byte)2, 256 }, // 256 cannot be represented in byte
{ 256, (byte)2 },
{ -1, (ushort)65535 }, // 65535 is -1 casted to ushort
{ (ushort)65535, -1 },
{ 0.02d, 0 },
{ 0, 0.02d },
{ 0.02f, 0 },
{ 0, 0.02f },
{ long.MaxValue, 9.22337204E+18 },
{ 9.22337204E+18, long.MaxValue },
{ 9223372030000000000L, 9.22337204E+18 },
{ 9.22337204E+18, 9223372030000000000L }
};

[Theory]
[MemberData(nameof(GetNumericAndNumericData))]
Expand Down
Expand Up @@ -6,17 +6,14 @@ namespace FluentAssertions.Specs.Formatting;

public class EnumerableExtensionsSpecs
{
public static IEnumerable<object[]> JoinUsingWritingStyleTestCases
public static TheoryData<IEnumerable<object>, string> JoinUsingWritingStyleTestCases => new()
{
get
{
yield return new object[] { new object[0], "" };
yield return new object[] { new object[] { "test" }, "test" };
yield return new object[] { new object[] { "test", "test2" }, "test and test2" };
yield return new object[] { new object[] { "test", "test2", "test3" }, "test, test2 and test3" };
yield return new object[] { new object[] { "test", "test2", "test3", "test4" }, "test, test2, test3 and test4" };
}
}
{ new object[0], "" },
{ new object[] { "test" }, "test" },
{ new object[] { "test", "test2" }, "test and test2" },
{ new object[] { "test", "test2", "test3" }, "test, test2 and test3" },
{ new object[] { "test", "test2", "test3", "test4" }, "test, test2, test3 and test4" }
};

[Theory]
[MemberData(nameof(JoinUsingWritingStyleTestCases))]
Expand Down
Expand Up @@ -21,39 +21,35 @@ public void When_formatting_a_multi_dimensional_array_it_should_show_structure(o
result.Should().Match(expected);
}

public static IEnumerable<object[]> MultiDimensionalArrayData =>
new List<object[]>
public static TheoryData<object, string> MultiDimensionalArrayData => new()
{
{
new int[0, 0],
"{empty}"
},
{
new object[]
new int[,]
{
new int[0, 0],
"{empty}"
{ 1, 2 },
{ 3, 4 }
},
new object[]
"{{1, 2}, {3, 4}}"
},
{
new int[,,]
{
new int[,]
{
{ 1, 2 },
{ 3, 4 }
{ 1, 2, 3 },
{ 4, 5, 6 }
},
"{{1, 2}, {3, 4}}"
},
new object[]
{
new int[,,]
{
{
{ 1, 2, 3 },
{ 4, 5, 6 }
},
{
{ 7, 8, 9 },
{ 10, 11, 12 }
}
},
"{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}"
{ 7, 8, 9 },
{ 10, 11, 12 }
}
},
};
"{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}"
},
};

[Fact]
public void When_formatting_a_multi_dimensional_array_with_bounds_it_should_show_structure()
Expand Down

0 comments on commit ce207ca

Please sign in to comment.