| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
using System.Collections.Generic; |
| 5 |
using System.Globalization; |
| 6 |
using System.Linq; |
| 7 |
using System.Text; |
| 8 |
using System.Reactive.Disposables; |
| 9 |
using System.Reactive.Linq; |
| 10 |
|
| 11 |
#if NUNIT |
| 12 |
using NUnit.Framework; |
| 13 |
#elif WINDOWS8 |
| 14 |
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; |
| 15 |
#else |
| 16 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 17 |
#endif |
| 18 |
|
| 19 |
namespace Microsoft.Reactive.Testing |
| 20 |
{ |
| 21 |
/// <summary> |
| 22 |
/// Helper class to write asserts in unit tests for applications and libraries built using Reactive Extensions. |
| 23 |
/// </summary> |
| 24 |
public static class ReactiveAssert |
| 25 |
{ |
| 26 |
static string Message<T>(IEnumerable<T> actual, IEnumerable<T> expected) |
| 27 |
{ |
| 28 |
var sb = new StringBuilder(); |
| 29 |
sb.AppendLine(); |
| 30 |
sb.Append("Expected: ["); |
| 31 |
sb.Append(string.Join(", ", expected.Select(x => x.ToString()).ToArray())); |
| 32 |
sb.Append("]"); |
| 33 |
sb.AppendLine(); |
| 34 |
sb.Append("Actual..: ["); |
| 35 |
sb.Append(string.Join(", ", actual.Select(x => x.ToString()).ToArray())); |
| 36 |
sb.Append("]"); |
| 37 |
sb.AppendLine(); |
| 38 |
return sb.ToString(); |
| 39 |
} |
| 40 |
|
| 41 |
/// <summary> |
| 42 |
/// Asserts that both enumerable sequences have equal length and equal elements. |
| 43 |
/// </summary> |
| 44 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 45 |
/// <param name="expected">Expected sequence.</param> |
| 46 |
/// <param name="actual">Actual sequence to compare against the expected one.</param> |
| 47 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 48 |
public static void AreElementsEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual) |
| 49 |
{ |
| 50 |
if (expected == null) |
| 51 |
throw new ArgumentNullException("expected"); |
| 52 |
if (actual == null) |
| 53 |
throw new ArgumentNullException("actual"); |
| 54 |
|
| 55 |
if (!expected.SequenceEqual(actual)) |
| 56 |
Assert.Fail(Message(actual, expected)); |
| 57 |
} |
| 58 |
|
| 59 |
/// <summary> |
| 60 |
/// Asserts that both enumerable sequences have equal length and equal elements. |
| 61 |
/// </summary> |
| 62 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 63 |
/// <param name="expected">Expected sequence.</param> |
| 64 |
/// <param name="actual">Actual sequence to compare against the expected one.</param> |
| 65 |
/// <param name="message">Error message for assert failure.</param> |
| 66 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 67 |
public static void AreElementsEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string message) |
| 68 |
{ |
| 69 |
if (expected == null) |
| 70 |
throw new ArgumentNullException("expected"); |
| 71 |
if (actual == null) |
| 72 |
throw new ArgumentNullException("actual"); |
| 73 |
|
| 74 |
if (!expected.SequenceEqual(actual)) |
| 75 |
Assert.Fail(message); |
| 76 |
} |
| 77 |
|
| 78 |
/// <summary> |
| 79 |
/// Asserts that both observable sequences have equal length and equal notifications. |
| 80 |
/// </summary> |
| 81 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 82 |
/// <param name="expected">Expected sequence.</param> |
| 83 |
/// <param name="actual">Actual sequence to compare against the expected one.</param> |
| 84 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 85 |
public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual) |
| 86 |
{ |
| 87 |
if (expected == null) |
| 88 |
throw new ArgumentNullException("expected"); |
| 89 |
if (actual == null) |
| 90 |
throw new ArgumentNullException("actual"); |
| 91 |
|
| 92 |
AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable()); |
| 93 |
} |
| 94 |
|
| 95 |
/// <summary> |
| 96 |
/// Asserts that both observable sequences have equal length and equal elements. |
| 97 |
/// </summary> |
| 98 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 99 |
/// <param name="expected">Expected sequence.</param> |
| 100 |
/// <param name="actual">Actual sequence to compare against the expected one.</param> |
| 101 |
/// <param name="message">Error message for assert failure.</param> |
| 102 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 103 |
public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual, string message) |
| 104 |
{ |
| 105 |
if (expected == null) |
| 106 |
throw new ArgumentNullException("expected"); |
| 107 |
if (actual == null) |
| 108 |
throw new ArgumentNullException("actual"); |
| 109 |
|
| 110 |
AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable(), message); |
| 111 |
} |
| 112 |
|
| 113 |
/// <summary> |
| 114 |
/// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof. |
| 115 |
/// </summary> |
| 116 |
/// <typeparam name="TException">Type of the exception to check for.</typeparam> |
| 117 |
/// <param name="action">Action to run.</param> |
| 118 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 119 |
public static void Throws<TException>(Action action) where TException : Exception |
| 120 |
{ |
| 121 |
if (action == null) |
| 122 |
throw new ArgumentNullException("action"); |
| 123 |
|
| 124 |
var failed = false; |
| 125 |
try |
| 126 |
{ |
| 127 |
action(); |
| 128 |
failed = true; |
| 129 |
} |
| 130 |
catch (TException) |
| 131 |
{ |
| 132 |
} |
| 133 |
catch (Exception ex) |
| 134 |
{ |
| 135 |
Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0} threw {1}.\r\n\r\nStack trace:\r\n{2}", typeof(TException).Name, ex.GetType().Name, ex.StackTrace)); |
| 136 |
} |
| 137 |
|
| 138 |
if (failed) |
| 139 |
Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0}.", typeof(TException).Name)); |
| 140 |
} |
| 141 |
|
| 142 |
/// <summary> |
| 143 |
/// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof. |
| 144 |
/// </summary> |
| 145 |
/// <typeparam name="TException">Type of the exception to check for.</typeparam> |
| 146 |
/// <param name="action">Action to run.</param> |
| 147 |
/// <param name="message">Error message for assert failure.</param> |
| 148 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 149 |
public static void Throws<TException>(Action action, string message) where TException : Exception |
| 150 |
{ |
| 151 |
if (action == null) |
| 152 |
throw new ArgumentNullException("action"); |
| 153 |
|
| 154 |
var failed = false; |
| 155 |
try |
| 156 |
{ |
| 157 |
action(); |
| 158 |
failed = true; |
| 159 |
} |
| 160 |
catch (TException) |
| 161 |
{ |
| 162 |
} |
| 163 |
catch |
| 164 |
{ |
| 165 |
Assert.Fail(message); |
| 166 |
} |
| 167 |
|
| 168 |
if (failed) |
| 169 |
Assert.Fail(message); |
| 170 |
} |
| 171 |
|
| 172 |
/// <summary> |
| 173 |
/// Asserts that the given action throws the specified exception. |
| 174 |
/// </summary> |
| 175 |
/// <typeparam name="TException">Type of the exception to check for.</typeparam> |
| 176 |
/// <param name="exception">Exception to assert being thrown.</param> |
| 177 |
/// <param name="action">Action to run.</param> |
| 178 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 179 |
public static void Throws<TException>(TException exception, Action action) where TException : Exception |
| 180 |
{ |
| 181 |
if (action == null) |
| 182 |
throw new ArgumentNullException("action"); |
| 183 |
|
| 184 |
var failed = false; |
| 185 |
try |
| 186 |
{ |
| 187 |
action(); |
| 188 |
failed = true; |
| 189 |
} |
| 190 |
catch (TException ex) |
| 191 |
{ |
| 192 |
Assert.AreSame(exception, ex); |
| 193 |
} |
| 194 |
catch (Exception ex) |
| 195 |
{ |
| 196 |
Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0} threw {1}.\r\n\r\nStack trace:\r\n{2}", typeof(TException).Name, ex.GetType().Name, ex.StackTrace)); |
| 197 |
} |
| 198 |
|
| 199 |
if (failed) |
| 200 |
Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0}.", typeof(TException).Name)); |
| 201 |
} |
| 202 |
|
| 203 |
/// <summary> |
| 204 |
/// Asserts that the given action throws the specified exception. |
| 205 |
/// </summary> |
| 206 |
/// <typeparam name="TException">Type of the exception to check for.</typeparam> |
| 207 |
/// <param name="exception">Exception to assert being thrown.</param> |
| 208 |
/// <param name="action">Action to run.</param> |
| 209 |
/// <param name="message">Error message for assert failure.</param> |
| 210 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 211 |
public static void Throws<TException>(TException exception, Action action, string message) where TException : Exception |
| 212 |
{ |
| 213 |
if (action == null) |
| 214 |
throw new ArgumentNullException("action"); |
| 215 |
|
| 216 |
var failed = false; |
| 217 |
try |
| 218 |
{ |
| 219 |
action(); |
| 220 |
failed = true; |
| 221 |
} |
| 222 |
catch (TException ex) |
| 223 |
{ |
| 224 |
Assert.AreSame(exception, ex); |
| 225 |
} |
| 226 |
catch |
| 227 |
{ |
| 228 |
Assert.Fail(message); |
| 229 |
} |
| 230 |
|
| 231 |
if (failed) |
| 232 |
Assert.Fail(message); |
| 233 |
} |
| 234 |
|
| 235 |
/// <summary> |
| 236 |
/// Asserts that both enumerable sequences have equal length and equal elements. |
| 237 |
/// </summary> |
| 238 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 239 |
/// <param name="actual">Actual sequence to compare against the expected one.</param> |
| 240 |
/// <param name="expected">Expected sequence.</param> |
| 241 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 242 |
public static void AssertEqual<T>(this IEnumerable<T> actual, IEnumerable<T> expected) |
| 243 |
{ |
| 244 |
if (actual == null) |
| 245 |
throw new ArgumentNullException("actual"); |
| 246 |
if (expected == null) |
| 247 |
throw new ArgumentNullException("expected"); |
| 248 |
|
| 249 |
ReactiveAssert.AreElementsEqual(expected, actual); |
| 250 |
} |
| 251 |
|
| 252 |
/// <summary> |
| 253 |
/// Asserts the enumerable sequence has the expected elements. |
| 254 |
/// </summary> |
| 255 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 256 |
/// <param name="actual">Actual sequence to compare against the expected elements.</param> |
| 257 |
/// <param name="expected">Expected elements.</param> |
| 258 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 259 |
public static void AssertEqual<T>(this IEnumerable<T> actual, params T[] expected) |
| 260 |
{ |
| 261 |
if (actual == null) |
| 262 |
throw new ArgumentNullException("actual"); |
| 263 |
if (expected == null) |
| 264 |
throw new ArgumentNullException("expected"); |
| 265 |
|
| 266 |
ReactiveAssert.AreElementsEqual(expected, actual); |
| 267 |
} |
| 268 |
|
| 269 |
/// <summary> |
| 270 |
/// Asserts that both observable sequences have equal length and equal notifications. |
| 271 |
/// </summary> |
| 272 |
/// <typeparam name="T">The type of the elements in the sequence.</typeparam> |
| 273 |
/// <param name="actual">Actual sequence to compare against the expected one.</param> |
| 274 |
/// <param name="expected">Expected sequence.</param> |
| 275 |
/// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception> |
| 276 |
public static void AssertEqual<T>(this IObservable<T> actual, IObservable<T> expected) |
| 277 |
{ |
| 278 |
if (actual == null) |
| 279 |
throw new ArgumentNullException("actual"); |
| 280 |
if (expected == null) |
| 281 |
throw new ArgumentNullException("expected"); |
| 282 |
|
| 283 |
ReactiveAssert.AreElementsEqual(expected, actual); |
| 284 |
} |
| 285 |
} |
| 286 |
} |