/// <summary> /// Verifies that the specified action throws a specific exception type. /// </summary> /// <typeparam name="T">The exception type.</typeparam> /// <param name="action">The exception.</param> /// <param name="additionalAssertion">The additional assertion.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="action"/> or it's value is <see langword="null"/>.</exception> public static void Throws <T>( this ActualValue <Action> action, Action <T> additionalAssertion, string message, params object[] parameters) where T : Exception { Argument.NotNull(action, nameof(action)); Argument.NotNull(action.Value, nameof(action)); try { action.Value(); } catch (T e) { try { additionalAssertion?.Invoke(e); } catch (Exception additionalException) { Throw(nameof(Throws), additionalException, AdditionalAssertionsFailed(typeof(T)), message, parameters); } return; } catch (Exception e) { Throw(nameof(Throws), UnexpectedException(typeof(T), e.GetType()), message, parameters); } Throw(nameof(Throws), DidNotThrow(typeof(T)), message, parameters); }
/// <summary> /// Verifies that the specified action throws a specific exception type. /// </summary> /// <typeparam name="T">The exception type.</typeparam> /// <param name="action">The exception.</param> /// <exception cref="ArgumentNullException"><paramref name="action"/> or it's value is <see langword="null"/>.</exception> public static void Throws <T>(this ActualValue <Action> action) where T : Exception { Argument.NotNull(action, nameof(action)); Argument.NotNull(action.Value, nameof(action)); action.Throws <T>(null, null, null); }
/// <summary> /// Verifies that a <see cref="Task" /> completes. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify completes.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <remarks> /// <para>If this assertion fails you may have a deadlock or the task simply is taking too long /// to complete. If you need control over how long to wait before failing use one of the other overloads /// of this assertion.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void Completes <T>(this ActualValue <T> task, string message) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); task.Completes(DeadlockTimeout, message, null); }
/// <summary> /// Verifies that a <see cref="Task"/> is busy. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify is busy.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message" />.</param> /// <remarks> /// <para>This assertion verifies that the task doesn't complete within a specified time.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void IsBusy <T>(this ActualValue <T> task, string message, params object[] parameters) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); task.IsBusy(QuickTimeout, message, parameters); }
/// <summary> /// Verifies that a <see cref="Task" /> completes. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify completes.</param> /// <param name="millisecondsTimeout">The amount of time in milliseconds to wait for the <see cref="Task"/> to complete.</param> /// <remarks> /// <para>If this assertion fails you may have a deadlock or the task simply is taking too long /// to complete.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void Completes <T>(this ActualValue <T> task, int millisecondsTimeout) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); task.Completes(millisecondsTimeout, null); }
/// <summary> /// Verifies that a <see cref="Task"/> is busy. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify is busy.</param> /// <param name="millisecondsTimeout">The amount of time in milliseconds to wait for the <see cref="Task"/> /// to complete to consider it busy.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <remarks> /// <para>This assertion verifies that the task doesn't complete within a specified time.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void IsBusy <T>(this ActualValue <T> task, int millisecondsTimeout, string message) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); task.IsBusy(millisecondsTimeout, message, null); }
/// <summary> /// Verifies that the specified action throws a specific exception type. /// </summary> /// <typeparam name="T">The exception type.</typeparam> /// <param name="action">The exception.</param> /// <param name="additionalAssertion">The additional assertion.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <exception cref="ArgumentNullException"><paramref name="action"/> or it's value is <see langword="null"/>.</exception> public static void Throws <T>(this ActualValue <Action> action, Action <T> additionalAssertion, string message) where T : Exception { Argument.NotNull(action, nameof(action)); Argument.NotNull(action.Value, nameof(action)); action.Throws <T>(additionalAssertion, message, null); }
/// <summary> /// Verifies that a <see cref="Task"/> is busy. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify is busy.</param> /// <remarks> /// <para>This assertion verifies that the task doesn't complete within a specified time.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void IsBusy <T>(this ActualValue <T> task) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); task.IsBusy(QuickTimeout, null); }
/// <summary> /// Verifies that the specified action throws a specific exception type. /// </summary> /// <typeparam name="T">The exception type.</typeparam> /// <param name="action">The exception.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="action"/> or it's value is <see langword="null"/>.</exception> public static void Throws <T>(this ActualValue <Action> action, string message, params object[] parameters) where T : Exception { Argument.NotNull(action, nameof(action)); Argument.NotNull(action.Value, nameof(action)); action.Throws <T>(null, message, parameters); }
/// <summary> /// Verifies that a <see cref="Task"/> is busy. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify is busy.</param> /// <param name="millisecondsTimeout">The amount of time in milliseconds to wait for the <see cref="Task"/> /// to complete to consider it busy.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message" />.</param> /// <remarks> /// <para>This assertion verifies that the task doesn't complete within a specified time.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void IsBusy <T>(this ActualValue <T> task, int millisecondsTimeout, string message, params object[] parameters) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); if (task.Value.WaitForCompletion(millisecondsTimeout)) { Throw(nameof(IsBusy), "Task completed unexpectedly.", message, parameters); } }
/// <summary> /// Verifies that a <see cref="Task" /> completes. /// </summary> /// <typeparam name="T">The <see cref="Task" /> type.</typeparam> /// <param name="task">The task to verify completes.</param> /// <param name="millisecondsTimeout">The amount of time in milliseconds to wait for the <see cref="Task"/> to complete.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message" />.</param> /// <remarks> /// <para>If this assertion fails you may have a deadlock or the task simply is taking too long /// to complete.</para> /// <para>There are no overloads for working with <see cref="Task"/> collections, but you can easily /// make assertions by using the <see cref="M:Task.WhenAny"/> or <see cref="M:Task.WhenAll"/> methods. /// For example: <code>Assert(Task.WhenAll(tasks)).Complete();</code></para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="task"/> (or it's value) is <c>null</c>.</exception> /// <exception cref="AssertionException">The <paramref name="task"/> did not complete in the alloted time.</exception> public static void Completes <T>(this ActualValue <T> task, int millisecondsTimeout, string message, params object[] parameters) where T : Task { Argument.NotNull(task, nameof(task)); Argument.NotNull(task.Value, nameof(task)); if (!task.Value.WaitForCompletion(millisecondsTimeout)) { Throw(nameof(Completes), TaskDidNotComplete(), message, parameters); } }
/// <summary> /// Verifies that the specified string matches the regular expression. Displays a message if the assertion /// fails, and applies the specified formatting to it. /// </summary> /// <param name="value">The actual value.</param> /// <param name="pattern">The regular expression that <paramref name="value"/> is expected to match.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"><paramref name="value"/> does not match <paramref name="pattern"/>.</exception> public static void Matches(this ActualValue <string> value, Regex pattern, string message, params object[] parameters) { Argument.NotNull(value, nameof(value)); if (pattern.IsMatch(value.Value)) { return; } Throw(nameof(Matches), MatchesFailMsg(value.Value, pattern), message, parameters); }
/// <summary> /// Verifies that the string contains the specified substring. Displays a message if the assertion /// fails, and applies the specified formatting to it. This method is case sensitive. /// </summary> /// <param name="value">The actual value.</param> /// <param name="substring">The string expected to occur within <paramref name="value"/>.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"> /// <paramref name="value"/> does not contain <paramref name="substring"/>. /// </exception> public static void Contains(this ActualValue <string> value, string substring, string message, params object[] parameters) { Argument.NotNull(value, nameof(value)); if (value.Value.IndexOf(substring, StringComparison.Ordinal) >= 0) { return; } Throw(nameof(Contains), ContainsFailMsg(value.Value, substring), message, parameters); }
/// <summary> /// Verifies that the specified string starts with the specified substring. Displays a message if the assertion /// fails, and applies the specified formatting to it. /// </summary> /// <param name="value">The actual value.</param> /// <param name="substring">The string expected to be a suffix of <paramref name="value"/>.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <param name="parameters">An array of parameters to use when formatting <paramref name="message"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"> /// <paramref name="value"/> does not start with <paramref name="substring"/>. /// </exception> public static void StartsWith(this ActualValue <string> value, string substring, string message, params object[] parameters) { Argument.NotNull(value, nameof(value)); if (value.Value.StartsWith(substring, StringComparison.Ordinal)) { return; } Throw(nameof(StartsWith), StartsWithFailMsg(value.Value, substring), message, parameters); }
/// <summary> /// Verifies that the specified string ends with the specified substring. Displays a message if the assertion /// fails, and applies the specified formatting to it. /// </summary> /// <param name="value">The actual value.</param> /// <param name="substring">The string expected to be a suffix of <paramref name="value"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"> /// <paramref name="value"/> does not end with <paramref name="substring"/>. /// </exception> public static void EndsWith(this ActualValue <string> value, string substring) { Argument.NotNull(value, nameof(value)); value.EndsWith(substring, null, null); }
/// <summary> /// Verifies that the specified string matches the regular expression. Displays a message if the assertion /// fails, and applies the specified formatting to it. /// </summary> /// <param name="value">The actual value.</param> /// <param name="pattern">The regular expression that <paramref name="value"/> is expected to match.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"> /// <paramref name="value"/> does not match <paramref name="pattern"/>. /// </exception> public static void Matches(this ActualValue <string> value, Regex pattern, string message) { Argument.NotNull(value, nameof(value)); value.Matches(pattern, message, null); }
/// <summary> /// Verifies that the specified string does not match the regular expression. Displays a message if the /// assertion fails, and applies the specified formatting to it. /// </summary> /// <param name="value">The actual value.</param> /// <param name="pattern">The regular expression that <paramref name="value"/> is not expected to match.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"><paramref name="value"/> matches <paramref name="pattern"/>.</exception> public static void DoesNotMatch(this ActualValue <string> value, Regex pattern) { Argument.NotNull(value, nameof(value)); value.DoesNotMatch(pattern, null, null); }
/// <summary> /// Verifies that the string contains the specified substring. Displays a message if the assertion /// fails, and applies the specified formatting to it. This method is case sensitive. /// </summary> /// <param name="value">The actual value.</param> /// <param name="substring">The string expected to occur within <paramref name="value"/>.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"> /// <paramref name="value"/> does not contain <paramref name="substring"/>. /// </exception> public static void Contains(this ActualValue <string> value, string substring, string message) { Argument.NotNull(value, nameof(value)); value.Contains(substring, message, (object[])null); }
/// <summary> /// Verifies that the specified string starts with the specified substring. Displays a message if the assertion /// fails, and applies the specified formatting to it. /// </summary> /// <param name="value">The actual value.</param> /// <param name="substring">The string expected to be a suffix of <paramref name="value"/>.</param> /// <param name="message">A message to display if the assertion fails. This message can /// be seen in the unit test results.</param> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="AssertionException"> /// <paramref name="value"/> does not start with <paramref name="substring"/>. /// </exception> public static void StartsWith(this ActualValue <string> value, string substring, string message) { Argument.NotNull(value, nameof(value)); value.StartsWith(substring, message, null); }