-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathTestExtensions.cs
107 lines (94 loc) · 3.97 KB
/
TestExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
namespace MoreLinq.Test
{
using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Constraints;
public enum SourceKind
{
Sequence,
BreakingList,
BreakingReadOnlyList,
BreakingCollection,
BreakingReadOnlyCollection
}
static class SourceKinds
{
public static readonly IEnumerable<SourceKind> Sequence = [
SourceKind.Sequence
];
public static readonly IEnumerable<SourceKind> Collection = [
SourceKind.BreakingCollection,
SourceKind.BreakingReadOnlyCollection
];
public static readonly IEnumerable<SourceKind> List = [
SourceKind.BreakingList,
SourceKind.BreakingReadOnlyList
];
}
static partial class TestExtensions
{
/// <summary>
/// Just to make our testing easier so we can chain the assertion call.
/// </summary>
internal static void AssertSequenceEqual<T>(this IEnumerable<T> actual, IEnumerable<T> expected) =>
Assert.That(actual, Is.EqualTo(expected));
/// <summary>
/// Make testing even easier - a params array makes for readable tests :)
/// The sequence should be evaluated exactly once.
/// </summary>
internal static void AssertSequenceEqual<T>(this IEnumerable<T> actual, params T[] expected) =>
Assert.That(actual, Is.EqualTo(expected));
internal static void AssertSequence<T>(this IEnumerable<T> actual, params IResolveConstraint[] expectations)
{
var i = 0;
foreach (var item in actual)
{
Assert.That(i, Is.LessThan(expectations.Length), "Actual sequence has more items than expected.");
var expectation = expectations[i];
Assert.That(item, expectation, "Unexpected element in sequence at index " + i);
i++;
}
Assert.That(i, Is.EqualTo(expectations.Length), "Actual sequence has fewer items than expected.");
}
internal static IEnumerable<string> GenerateSplits(this string str, params char[] separators)
{
foreach (var split in str.Split(separators))
yield return split;
}
internal static IEnumerable<T> ToSourceKind<T>(this IEnumerable<T> input, SourceKind sourceKind) =>
#pragma warning disable IDE0072 // Add missing cases
sourceKind switch
#pragma warning restore IDE0072 // Add missing cases
{
SourceKind.Sequence => input.Select(x => x),
var kind => input.ToList().AsSourceKind(kind)
};
internal static IEnumerable<T> AsSourceKind<T>(this List<T> input, SourceKind sourceKind) =>
sourceKind switch
{
SourceKind.Sequence => input.Select(x => x),
SourceKind.BreakingList => new BreakingList<T>(input),
SourceKind.BreakingReadOnlyList => new BreakingReadOnlyList<T>(input),
SourceKind.BreakingCollection => new BreakingCollection<T>(input),
SourceKind.BreakingReadOnlyCollection => new BreakingReadOnlyCollection<T>(input),
_ => throw new ArgumentException(null, nameof(sourceKind))
};
}
}