123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Reactive.Disposables;
- using System.Reactive.Linq;
- using Xunit;
- namespace Microsoft.Reactive.Testing
- {
- /// <summary>
- /// Helper class to write asserts in unit tests for applications and libraries built using Reactive Extensions.
- /// </summary>
- public static class ReactiveAssert
- {
- static string Message<T>(IEnumerable<T> actual, IEnumerable<T> expected)
- {
- var sb = new StringBuilder();
- sb.AppendLine();
- sb.Append("Expected: [");
- sb.Append(string.Join(", ", expected.Select(x => x.ToString()).ToArray()));
- sb.Append("]");
- sb.AppendLine();
- sb.Append("Actual..: [");
- sb.Append(string.Join(", ", actual.Select(x => x.ToString()).ToArray()));
- sb.Append("]");
- sb.AppendLine();
- return sb.ToString();
- }
- /// <summary>
- /// Asserts that both enumerable sequences have equal length and equal elements.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="expected">Expected sequence.</param>
- /// <param name="actual">Actual sequence to compare against the expected one.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AreElementsEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
- {
- if (expected == null)
- throw new ArgumentNullException("expected");
- if (actual == null)
- throw new ArgumentNullException("actual");
- if (!expected.SequenceEqual(actual))
- Assert.True(false, Message(actual, expected));
- }
- /// <summary>
- /// Asserts that both enumerable sequences have equal length and equal elements.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="expected">Expected sequence.</param>
- /// <param name="actual">Actual sequence to compare against the expected one.</param>
- /// <param name="message">Error message for assert failure.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AreElementsEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string message)
- {
- if (expected == null)
- throw new ArgumentNullException("expected");
- if (actual == null)
- throw new ArgumentNullException("actual");
- if (!expected.SequenceEqual(actual))
- Assert.True(false, message);
- }
- /// <summary>
- /// Asserts that both observable sequences have equal length and equal notifications.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="expected">Expected sequence.</param>
- /// <param name="actual">Actual sequence to compare against the expected one.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual)
- {
- if (expected == null)
- throw new ArgumentNullException("expected");
- if (actual == null)
- throw new ArgumentNullException("actual");
- AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable());
- }
- /// <summary>
- /// Asserts that both observable sequences have equal length and equal elements.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="expected">Expected sequence.</param>
- /// <param name="actual">Actual sequence to compare against the expected one.</param>
- /// <param name="message">Error message for assert failure.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual, string message)
- {
- if (expected == null)
- throw new ArgumentNullException("expected");
- if (actual == null)
- throw new ArgumentNullException("actual");
- AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable(), message);
- }
- /// <summary>
- /// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof.
- /// </summary>
- /// <typeparam name="TException">Type of the exception to check for.</typeparam>
- /// <param name="action">Action to run.</param>
- /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
- public static void Throws<TException>(Action action) where TException : Exception
- {
- if (action == null)
- throw new ArgumentNullException("action");
- var failed = false;
- try
- {
- action();
- failed = true;
- }
- catch (TException)
- {
- }
- catch (Exception ex)
- {
- Assert.True(false, string.Format(CultureInfo.CurrentCulture, "Expected {0} threw {1}.\r\n\r\nStack trace:\r\n{2}", typeof(TException).Name, ex.GetType().Name, ex.StackTrace));
- }
- if (failed)
- Assert.True(false, string.Format(CultureInfo.CurrentCulture, "Expected {0}.", typeof(TException).Name));
- }
- /// <summary>
- /// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof.
- /// </summary>
- /// <typeparam name="TException">Type of the exception to check for.</typeparam>
- /// <param name="action">Action to run.</param>
- /// <param name="message">Error message for assert failure.</param>
- /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
- public static void Throws<TException>(Action action, string message) where TException : Exception
- {
- if (action == null)
- throw new ArgumentNullException("action");
- var failed = false;
- try
- {
- action();
- failed = true;
- }
- catch (TException)
- {
- }
- catch
- {
- Assert.True(false, message);
- }
- if (failed)
- Assert.True(false, message);
- }
- /// <summary>
- /// Asserts that the given action throws the specified exception.
- /// </summary>
- /// <typeparam name="TException">Type of the exception to check for.</typeparam>
- /// <param name="exception">Exception to assert being thrown.</param>
- /// <param name="action">Action to run.</param>
- /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
- public static void Throws<TException>(TException exception, Action action) where TException : Exception
- {
- if (action == null)
- throw new ArgumentNullException("action");
- var failed = false;
- try
- {
- action();
- failed = true;
- }
- catch (TException ex)
- {
- Assert.Same(exception, ex);
- }
- catch (Exception ex)
- {
- Assert.True(false, string.Format(CultureInfo.CurrentCulture, "Expected {0} threw {1}.\r\n\r\nStack trace:\r\n{2}", typeof(TException).Name, ex.GetType().Name, ex.StackTrace));
- }
- if (failed)
- Assert.True(false, string.Format(CultureInfo.CurrentCulture, "Expected {0}.", typeof(TException).Name));
- }
- /// <summary>
- /// Asserts that the given action throws the specified exception.
- /// </summary>
- /// <typeparam name="TException">Type of the exception to check for.</typeparam>
- /// <param name="exception">Exception to assert being thrown.</param>
- /// <param name="action">Action to run.</param>
- /// <param name="message">Error message for assert failure.</param>
- /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
- public static void Throws<TException>(TException exception, Action action, string message) where TException : Exception
- {
- if (action == null)
- throw new ArgumentNullException("action");
- var failed = false;
- try
- {
- action();
- failed = true;
- }
- catch (TException ex)
- {
- Assert.Same(exception, ex);
- }
- catch
- {
- Assert.True(false, message);
- }
- if (failed)
- Assert.True(false, message);
- }
- /// <summary>
- /// Asserts that both enumerable sequences have equal length and equal elements.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="actual">Actual sequence to compare against the expected one.</param>
- /// <param name="expected">Expected sequence.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AssertEqual<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
- {
- if (actual == null)
- throw new ArgumentNullException("actual");
- if (expected == null)
- throw new ArgumentNullException("expected");
- ReactiveAssert.AreElementsEqual(expected, actual);
- }
- /// <summary>
- /// Asserts the enumerable sequence has the expected elements.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="actual">Actual sequence to compare against the expected elements.</param>
- /// <param name="expected">Expected elements.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AssertEqual<T>(this IEnumerable<T> actual, params T[] expected)
- {
- if (actual == null)
- throw new ArgumentNullException("actual");
- if (expected == null)
- throw new ArgumentNullException("expected");
- ReactiveAssert.AreElementsEqual(expected, actual);
- }
- /// <summary>
- /// Asserts that both observable sequences have equal length and equal notifications.
- /// </summary>
- /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
- /// <param name="actual">Actual sequence to compare against the expected one.</param>
- /// <param name="expected">Expected sequence.</param>
- /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
- public static void AssertEqual<T>(this IObservable<T> actual, IObservable<T> expected)
- {
- if (actual == null)
- throw new ArgumentNullException("actual");
- if (expected == null)
- throw new ArgumentNullException("expected");
- ReactiveAssert.AreElementsEqual(expected, actual);
- }
- }
- }
|