Skip to content

Commit

Permalink
Assert.IsTrue() & False() to handle nullable bools (#690)
Browse files Browse the repository at this point in the history
* Added `Assert.IsTrue` and `Assert.IsFalse` for nullable booleans.

Co-authored-by: Maximilian Chaplin <MXCH@simcorp.com>
  • Loading branch information
HeroMaxPower and Maximilian Chaplin committed Sep 15, 2020
1 parent e58ecb9 commit 2d392c7
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
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
}
}

0 comments on commit 2d392c7

Please sign in to comment.