From 2d392c7daf0f90600a49e85b5d9d574d2038c304 Mon Sep 17 00:00:00 2001 From: Maximilian Chaplin Date: Wed, 16 Sep 2020 01:24:22 +0200 Subject: [PATCH] Assert.IsTrue() & False() to handle nullable bools (#690) * Added `Assert.IsTrue` and `Assert.IsFalse` for nullable booleans. Co-authored-by: Maximilian Chaplin --- .../MSTest.Core/Assertions/Assert.cs | 118 ++++++++++++++++++ .../Assertions/AssertTests.cs | 22 ++++ 2 files changed, 140 insertions(+) diff --git a/src/TestFramework/MSTest.Core/Assertions/Assert.cs b/src/TestFramework/MSTest.Core/Assertions/Assert.cs index 9665b9f0df..68f8b5d398 100644 --- a/src/TestFramework/MSTest.Core/Assertions/Assert.cs +++ b/src/TestFramework/MSTest.Core/Assertions/Assert.cs @@ -65,6 +65,21 @@ public static void IsTrue(bool condition) IsTrue(condition, string.Empty, null); } + /// + /// Tests whether the specified condition is true and throws an exception + /// if the condition is false. + /// + /// + /// The condition the test expects to be true. + /// + /// + /// Thrown if is false. + /// + public static void IsTrue(bool? condition) + { + IsTrue(condition, string.Empty, null); + } + /// /// Tests whether the specified condition is true and throws an exception /// if the condition is false. @@ -84,6 +99,25 @@ public static void IsTrue(bool condition, string message) IsTrue(condition, message, null); } + /// + /// Tests whether the specified condition is true and throws an exception + /// if the condition is false. + /// + /// + /// The condition the test expects to be true. + /// + /// + /// The message to include in the exception when + /// is false. The message is shown in test results. + /// + /// + /// Thrown if is false. + /// + public static void IsTrue(bool? condition, string message) + { + IsTrue(condition, message, null); + } + /// /// Tests whether the specified condition is true and throws an exception /// if the condition is false. @@ -109,6 +143,31 @@ public static void IsTrue(bool condition, string message, params object[] parame } } + /// + /// Tests whether the specified condition is true and throws an exception + /// if the condition is false. + /// + /// + /// The condition the test expects to be true. + /// + /// + /// The message to include in the exception when + /// is false. The message is shown in test results. + /// + /// + /// An array of parameters to use when formatting . + /// + /// + /// Thrown if is false. + /// + public static void IsTrue(bool? condition, string message, params object[] parameters) + { + if (condition == false || condition == null) + { + HandleFail("Assert.IsTrue", message, parameters); + } + } + /// /// Tests whether the specified condition is false and throws an exception /// if the condition is true. @@ -124,6 +183,21 @@ public static void IsFalse(bool condition) IsFalse(condition, string.Empty, null); } + /// + /// Tests whether the specified condition is false and throws an exception + /// if the condition is true. + /// + /// + /// The condition the test expects to be false. + /// + /// + /// Thrown if is true. + /// + public static void IsFalse(bool? condition) + { + IsFalse(condition, string.Empty, null); + } + /// /// Tests whether the specified condition is false and throws an exception /// if the condition is true. @@ -143,6 +217,25 @@ public static void IsFalse(bool condition, string message) IsFalse(condition, message, null); } + /// + /// Tests whether the specified condition is false and throws an exception + /// if the condition is true. + /// + /// + /// The condition the test expects to be false. + /// + /// + /// The message to include in the exception when + /// is true. The message is shown in test results. + /// + /// + /// Thrown if is true. + /// + public static void IsFalse(bool? condition, string message) + { + IsFalse(condition, message, null); + } + /// /// Tests whether the specified condition is false and throws an exception /// if the condition is true. @@ -168,6 +261,31 @@ public static void IsFalse(bool condition, string message, params object[] param } } + /// + /// Tests whether the specified condition is false and throws an exception + /// if the condition is true. + /// + /// + /// The condition the test expects to be false. + /// + /// + /// The message to include in the exception when + /// is true. The message is shown in test results. + /// + /// + /// An array of parameters to use when formatting . + /// + /// + /// Thrown if is true. + /// + public static void IsFalse(bool? condition, string message, params object[] parameters) + { + if (condition == true || condition == null) + { + HandleFail("Assert.IsFalse", message, parameters); + } + } + #endregion #region Null diff --git a/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs b/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs index dc64211915..6cabbc06ce 100644 --- a/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs +++ b/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs @@ -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 } }