Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert.IsTrue() & False() to handle nullable bools #690

Merged
merged 3 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/TestFramework/MSTest.Core/Assertions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public static void IsTrue(bool condition)
IsTrue(condition, string.Empty, null);
}

/// <summary>
/// Tests whether the specified condition is true and throws an exception
/// if the condition is false.
/// </summary>
/// <param name="condition">
/// The condition the test expects to be true.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue(bool? condition)
{
IsTrue(condition, string.Empty, null);
}

/// <summary>
/// Tests whether the specified condition is true and throws an exception
/// if the condition is false.
Expand All @@ -84,6 +99,25 @@ public static void IsTrue(bool condition, string message)
IsTrue(condition, message, null);
}

/// <summary>
/// Tests whether the specified condition is true and throws an exception
/// if the condition is false.
/// </summary>
/// <param name="condition">
/// The condition the test expects to be true.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="condition"/>
/// is false. The message is shown in test results.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue(bool? condition, string message)
{
IsTrue(condition, message, null);
}

/// <summary>
/// Tests whether the specified condition is true and throws an exception
/// if the condition is false.
Expand All @@ -109,6 +143,31 @@ public static void IsTrue(bool condition, string message, params object[] parame
}
}

/// <summary>
/// Tests whether the specified condition is true and throws an exception
/// if the condition is false.
/// </summary>
/// <param name="condition">
/// The condition the test expects to be true.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="condition"/>
/// is false. The message is shown in test results.
/// </param>
/// <param name="parameters">
/// An array of parameters to use when formatting <paramref name="message"/>.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="condition"/> is false.
/// </exception>
public static void IsTrue(bool? condition, string message, params object[] parameters)
{
if (condition == false || condition == null)
{
HandleFail("Assert.IsTrue", message, parameters);
}
}

/// <summary>
/// Tests whether the specified condition is false and throws an exception
/// if the condition is true.
Expand All @@ -124,6 +183,21 @@ public static void IsFalse(bool condition)
IsFalse(condition, string.Empty, null);
}

/// <summary>
/// Tests whether the specified condition is false and throws an exception
/// if the condition is true.
/// </summary>
/// <param name="condition">
/// The condition the test expects to be false.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse(bool? condition)
{
IsFalse(condition, string.Empty, null);
}

/// <summary>
/// Tests whether the specified condition is false and throws an exception
/// if the condition is true.
Expand All @@ -143,6 +217,25 @@ public static void IsFalse(bool condition, string message)
IsFalse(condition, message, null);
}

/// <summary>
/// Tests whether the specified condition is false and throws an exception
/// if the condition is true.
/// </summary>
/// <param name="condition">
/// The condition the test expects to be false.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="condition"/>
/// is true. The message is shown in test results.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse(bool? condition, string message)
{
IsFalse(condition, message, null);
}

/// <summary>
/// Tests whether the specified condition is false and throws an exception
/// if the condition is true.
Expand All @@ -168,6 +261,31 @@ public static void IsFalse(bool condition, string message, params object[] param
}
}

/// <summary>
/// Tests whether the specified condition is false and throws an exception
/// if the condition is true.
/// </summary>
/// <param name="condition">
/// The condition the test expects to be false.
/// </param>
/// <param name="message">
/// The message to include in the exception when <paramref name="condition"/>
/// is true. The message is shown in test results.
/// </param>
/// <param name="parameters">
/// An array of parameters to use when formatting <paramref name="message"/>.
/// </param>
/// <exception cref="AssertFailedException">
/// Thrown if <paramref name="condition"/> is true.
/// </exception>
public static void IsFalse(bool? condition, string message, params object[] parameters)
{
if (condition == true || condition == null)
{
HandleFail("Assert.IsFalse", message, parameters);
}
}

#endregion

#region Null
Expand Down
22 changes: 22 additions & 0 deletions test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,27 @@ public void InstanceNotOfTypeShouldPassOnSubInstance()
}

#endregion

#region Nullable Booleans tests.

[TestMethod]
public void IsFalseNullableBooleansShouldFailWithNull()
{
bool? nullBool = null;
var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.IsFalse(nullBool));
Assert.IsNotNull(ex);
StringAssert.Contains(ex.Message, "Assert.IsFalse failed");
}

[TestMethod]
public void IsTrueNullableBooleansShouldFailWithNull()
{
bool? nullBool = null;

var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.IsTrue(nullBool));
Assert.IsNotNull(ex);
StringAssert.Contains(ex.Message, "Assert.IsTrue failed");
}
#endregion
}
}