-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathTestHelper.cs
136 lines (117 loc) · 4.92 KB
/
TestHelper.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
namespace Azure.Storage.Test
{
internal static partial class TestHelper
{
public static byte[] GetRandomBuffer(long size, Random random = null)
{
random ??= new Random(Environment.TickCount);
var buffer = new byte[size];
random.NextBytes(buffer);
return buffer;
}
public static void AssertSequenceEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
{
Assert.AreEqual(expected.Count(), actual.Count(), "Actual sequence length does not match expected sequence length");
(int index, T expected, T actual)[] firstErrors = expected.Zip(actual, (e, a) => (expected: e, actual: a)).Select((x, i) => (index: i, x.expected, x.actual)).Where(x => !x.expected.Equals(x.actual)).Take(5).ToArray();
Assert.IsFalse(firstErrors.Any(), $"Actual sequence does not match expected sequence at locations\n{string.Join("\n", firstErrors.Select(e => $"{e.index} => expected = {e.expected}, actual = {e.actual}"))}");
}
public static IEnumerable<byte> AsBytes(this Stream s)
{
while (s.ReadByte() is var b && b != -1)
{
yield return (byte)b;
}
}
internal static Action<T, T> GetDefaultExceptionAssertion<T>(Func<T, T, bool> predicate)
where T : Exception
{
predicate = predicate ?? new Func<T, T, bool>((e, a) => true);
return
(e, a) =>
{
if (predicate(e, a))
{
Assert.AreEqual(e.Message, a.Message);
}
else
{
Assert.Fail($"Unexpected exception: {a.Message}");
}
}
;
}
public static void AssertExpectedException<T>(Action action, T expectedException, Func<T, T, bool> predicate = null)
where T : Exception
=> AssertExpectedException(action, expectedException, GetDefaultExceptionAssertion(predicate));
public static void AssertExpectedException<T>(Action action, Func<T, bool> predicate = null)
where T : Exception
=> AssertExpectedException(action, default, GetDefaultExceptionAssertion<T>((_, a) => predicate(a)));
public static void AssertExpectedException<T>(Action action, T expectedException, Action<T, T> assertion)
where T : Exception
{
Assert.IsNotNull(expectedException);
Assert.IsNotNull(assertion);
try
{
action();
Assert.Fail("Expected exception not found");
}
catch (T actualException)
{
assertion(expectedException, actualException);
}
}
public static Task AssertExpectedExceptionAsync<T>(Task task, T expectedException, Func<T, T, bool> predicate = null)
where T : Exception
=> AssertExpectedExceptionAsync(task, expectedException, GetDefaultExceptionAssertion(predicate));
public static Task AssertExpectedExceptionAsync<T>(Task task, Action<T> assertion)
where T : Exception
=> AssertExpectedExceptionAsync<T>(task, default, (_, a) => assertion(a));
public static async Task AssertExpectedExceptionAsync<T>(Task task, T expectedException, Action<T, T> assertion)
where T : Exception
{
Assert.IsNotNull(assertion);
try
{
await task.ConfigureAwait(false);
Assert.Fail("Expected exception not found");
}
catch (T actualException)
{
assertion(expectedException, actualException);
}
}
public static async Task<T> CatchAsync<T>(Func<Task> action)
where T : Exception
{
try
{
await action().ConfigureAwait(false);
Assert.Fail("Expected exception not found");
}
catch (T ex)
{
return ex;
}
catch (Exception other)
{
Assert.Fail($"Expected exception of type {typeof(T).Name}, not {other.ToString()}");
}
throw new InvalidOperationException("Won't ever get here!");
}
public static void AssertCacheableProperty<T>(T expected, Func<T> property)
{
T actual = property();
Assert.AreEqual(expected, actual); // first call calculates and caches value
Assert.AreSame(actual, property()); // subsequent calls use cached value
}
}
}