From 2d2eb959bb12b7933a7a5a311484376c2c59e9a4 Mon Sep 17 00:00:00 2001 From: Maximilian Chaplin Date: Wed, 26 Feb 2020 22:05:12 +0100 Subject: [PATCH 1/3] added nullable booleans to Assert.IsTrue() & Assert.IsFalse() --- .../MSTest.Core/Assertions/Assert.cs | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/src/TestFramework/MSTest.Core/Assertions/Assert.cs b/src/TestFramework/MSTest.Core/Assertions/Assert.cs index 25176aecf7..b09ebfc69c 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,36 @@ 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) + { + HandleFail("Assert.IsTrue", message, parameters); + } + + if (condition == null) + { + HandleFail("Assert.IsNull", message, parameters); + } + } + /// /// Tests whether the specified condition is false and throws an exception /// if the condition is true. @@ -124,6 +188,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 +222,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 +266,36 @@ 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) + { + HandleFail("Assert.IsFalse", message, parameters); + } + + if (condition == null) + { + HandleFail("Assert.IsNull", message, parameters); + } + } + #endregion #region Null From 5071f7243a2f2a9773f8cac00aab5f1fc759da67 Mon Sep 17 00:00:00 2001 From: Maximilian Chaplin Date: Wed, 26 Feb 2020 22:45:10 +0100 Subject: [PATCH 2/3] re thought logic --- src/TestFramework/MSTest.Core/Assertions/Assert.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/TestFramework/MSTest.Core/Assertions/Assert.cs b/src/TestFramework/MSTest.Core/Assertions/Assert.cs index b09ebfc69c..bc9b88d1f8 100644 --- a/src/TestFramework/MSTest.Core/Assertions/Assert.cs +++ b/src/TestFramework/MSTest.Core/Assertions/Assert.cs @@ -162,15 +162,10 @@ public static void IsTrue(bool condition, string message, params object[] parame /// public static void IsTrue(bool? condition, string message, params object[] parameters) { - if (condition == false) + if (condition == false || condition == null) { HandleFail("Assert.IsTrue", message, parameters); } - - if (condition == null) - { - HandleFail("Assert.IsNull", message, parameters); - } } /// @@ -285,15 +280,10 @@ public static void IsFalse(bool condition, string message, params object[] param /// public static void IsFalse(bool? condition, string message, params object[] parameters) { - if (condition == true) + if (condition == true || condition == null) { HandleFail("Assert.IsFalse", message, parameters); } - - if (condition == null) - { - HandleFail("Assert.IsNull", message, parameters); - } } #endregion From 3cc39a0cc08b03fe07523c4f52c6ab763f045160 Mon Sep 17 00:00:00 2001 From: Maximilian Chaplin Date: Wed, 26 Feb 2020 22:45:25 +0100 Subject: [PATCH 3/3] added tests --- .../Assertions/AssertTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 } }