From 31e467d72bc9b3cc7a7bb8c5bec4d2998ca49916 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 12 Feb 2021 17:08:54 +0800 Subject: test: Add some helper function. --- .../IntegratedTests/TimelinePostTest.cs | 8 +--- BackEnd/Timeline.Tests/XUnitHelper.cs | 44 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 BackEnd/Timeline.Tests/XUnitHelper.cs (limited to 'BackEnd/Timeline.Tests') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index 0f264774..85db0908 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -401,13 +401,7 @@ namespace Timeline.Tests.IntegratedTests testDataList.AddRange(testData.Select(d => new List() { d! })); - foreach (var generatorTestData in TimelineNameGeneratorTestData()) - { - var generator = generatorTestData[0]; - - foreach (var d in testDataList) - yield return new object?[] { generator, d }; - } + return TimelineNameGeneratorTestData().AppendTestData(testDataList); } [Theory] diff --git a/BackEnd/Timeline.Tests/XUnitHelper.cs b/BackEnd/Timeline.Tests/XUnitHelper.cs new file mode 100644 index 00000000..a2812ad3 --- /dev/null +++ b/BackEnd/Timeline.Tests/XUnitHelper.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Timeline.Tests +{ + public static class XUnitHelper + { + public static IEnumerable ComposeTestData(params IEnumerable[] testDatas) + { + return ComposeTestData(new ArraySegment>(testDatas)); + } + + public static IEnumerable ComposeTestData(ArraySegment> testDatas) + { + if (testDatas.Count == 0) + throw new ArgumentException("Test data list can't be empty.", nameof(testDatas)); + + if (testDatas.Count == 1) + { + foreach (var d in testDatas[0]) + yield return d; + } + else + { + foreach (var head in testDatas[0]) + foreach (var rest in ComposeTestData(testDatas.Slice(1))) + yield return head.Concat(rest).ToArray(); + } + } + + public static IEnumerable AppendTestData(this IEnumerable origin, params IEnumerable[] toAppend) + { + IEnumerable result = origin; + + foreach (var oneToAppend in toAppend) + { + result = ComposeTestData(result, oneToAppend.Select(testData => new object?[] { testData })); + } + + return result; + } + } +} -- cgit v1.2.3