From 340ebc0d7b9d8d80886d812f6354a7f71e1bd2e2 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 10:56:09 -0700 Subject: [PATCH 01/23] Moved deprecated methods to a separate file --- source/TestFramework/Assert.Obsolete.cs | 428 ++++++++++++ source/TestFramework/Assert.cs | 644 +----------------- .../nanoFramework.TestFramework.nfproj | 3 + 3 files changed, 432 insertions(+), 643 deletions(-) create mode 100644 source/TestFramework/Assert.Obsolete.cs diff --git a/source/TestFramework/Assert.Obsolete.cs b/source/TestFramework/Assert.Obsolete.cs new file mode 100644 index 0000000..5184d0e --- /dev/null +++ b/source/TestFramework/Assert.Obsolete.cs @@ -0,0 +1,428 @@ +using System; +using System.Collections; +using TestFrameworkShared; + +namespace nanoFramework.TestFramework +{ + public sealed partial class Assert + { + /// + /// Tests whether the specified collection is empty. + /// + /// The collection the test expects to be empty. + /// The message to include in the exception when the collection is empty. The message is shown in test results. + /// Raises an exception if the collection is not empty. + [Obsolete("This method is deprecated and will be removed in a future version. Use the method with the same name in CollectionAssert class.")] + public static void Empty(ICollection collection, string message = "") + { + if (collection.Count != 0) + { + HandleFail("Assert.Empty", message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(Array expected, Array actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(bool expected, bool actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(byte expected, byte actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(char expected, char actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(DateTime expected, DateTime actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(double expected, double actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(float expected, float actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(int expected, int actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(long expected, long actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(sbyte expected, sbyte actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(short expected, short actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(string expected, string actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(uint expected, uint actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(ulong expected, ulong actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] + public static void Equal(ushort expected, ushort actual, string message = "") => AreEqual(expected, actual, message); + + /// + /// 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 condition is true. The message is shown in test results. + /// Thrown if condition is . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsFalse.")] + public static void False(bool condition, string message = "") => IsFalse(condition, message); + + /// + /// Tests whether the specified object is not an instance of the wrong type and throws an exception if the specified type is in the inheritance hierarchy of the object. + /// + /// The object the test expects not to be of the specified type. + /// The type that value should not be. + /// The message to include in the exception when value is an instance of wrongType. The message is shown in test results. + /// Thrown if is not null and is in the inheritance hierarchy of value. + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsNotInstanceOfType.")] + public static void IsNotType(Type wrongType, object value, string message = "") => IsNotInstanceOfType(value, wrongType, message); + + /// + /// Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of the object. + /// + /// The expected type of value. + /// The object the test expects to be of the specified type. + /// The message to include in the exception when value is not an instance of expectedType. The message is shown in test results. + /// Thrown if is or is not in the inheritance hierarchy of . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsInstanceOfType.")] + public static void IsType(Type expectedType, object value, string message = "") => IsInstanceOfType(value, expectedType, message); + + /// + /// Tests whether the specified collection is not empty. + /// + /// The collection the test expects not to be empty. + /// The message to include in the exception when the collection is not empty. The message is shown in test results. + /// Raises an exception if the collection is not empty. + [Obsolete("This method is deprecated and will be removed in a future version. Use the method with the same name in CollectionAssert class.")] + public static void NotEmpty(ICollection collection, string message = "") + { + if (collection.Count == 0) + { + HandleFail("Assert.NotEmpty", message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(Array notExpected, Array actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(bool notExpected, bool actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(byte notExpected, byte actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(char notExpected, char actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match . + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(DateTime notExpected, DateTime actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(double notExpected, double actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(float notExpected, float actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(int notExpected, int actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(long notExpected, long actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(sbyte notExpected, sbyte actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(short notExpected, short actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(string notExpected, string actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(uint notExpected, uint actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(ulong notExpected, ulong actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] + public static void NotEqual(ushort notExpected, ushort actual, string message = "") => AreNotEqual(notExpected, actual, message); + + /// + /// Tests whether the specified object is non-null and throws an exception if it is null. + /// + /// The object the test expects not to be null. + /// The message to include in the exception when value is null. The message is shown in test results. + /// Thrown if value is null. + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsNotNull.")] + public static void NotNull(object value, string message = "") => IsNotNull(value, message); + + /// + /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. + /// + /// The first object to compare. This is the value the test expects not to match actual. + /// The second object to compare. This is the value produced by the code under test. + /// The message to include in the exception when is the same as . The message is shown in test results. + /// Thrown if refers to the same object as . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotSame.")] + public static void NotSame(object notExpected, object actual, string message = "") => AreNotSame(notExpected, actual, message); + + /// + /// Tests whether the specified object is null and throws an exception if it is not. + /// + /// The object the test expects to be null. + /// The message to include in the exception when value is not null. The message is shown in test results. + /// Thrown if value is not null. + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsNull.")] + public static void Null(object value, string message = "") => IsNull(value, message); + + /// + /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. + /// + /// The first object to compare. This is the value the test expects. + /// The second object to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not the same as . The message is shown in test results. + /// Thrown if does not refer to the same object as . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreSame.")] + public static void Same(object expected, object actual, string message = "") => AreSame(expected, actual, message); + + /// + /// Tests whether the code specified by delegate action throws exact given exception + /// of type (and not of derived type) and throws if code + /// does not throw exception or throws exception of type other than . + /// + /// Type of exception expected to be thrown. + /// Delegate to code to be tested and which is expected to throw exception. + /// The message to include in the exception when action does not throw exception of type . + /// Thrown if action does not throw exception of type . + /// Thrown if is . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method ThrowsException.")] + public static void Throws(Type exceptionType, Action action, string message = "") => ThrowsException(exceptionType, action, message); + + /// + /// 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 condition is false. The message is shown in test results. + /// Thrown if condition is . + [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsTrue.")] + public static void True(bool condition, string message = "") => IsTrue(condition, message); + } +} diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index c45963c..0e48cd7 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -5,7 +5,6 @@ // using System; -using System.Collections; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using TestFrameworkShared; @@ -15,7 +14,7 @@ namespace nanoFramework.TestFramework /// /// A collection of helper classes to test various conditions within unit tests. If the condition being tested is not met, an exception is thrown. /// - public sealed class Assert + public sealed partial class Assert { private const string AssertionFailed = "{0} failed. {1}"; private const string Common_NullInMessages = "(null)"; @@ -63,19 +62,6 @@ public static void IsTrue(bool condition, [CallerArgumentExpression(nameof(condi } } - /// - /// 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 condition is false. The message is shown in test results. - /// Thrown if condition is . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsTrue.")] - public static void True( - bool condition, - string message = "") => IsTrue( - condition, - message); - /// /// Tests whether the specified condition is false and throws an exception if the condition is true. /// @@ -89,19 +75,6 @@ public static void IsFalse(bool condition, [CallerArgumentExpression(nameof(cond HandleFail("Assert.IsFalse", message); } } - /// - /// 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 condition is true. The message is shown in test results. - /// Thrown if condition is . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsFalse.")] - public static void False( - bool condition, - string message = "") => IsFalse( - condition, - message); - #endregion #region Equal @@ -148,22 +121,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - bool expected, - bool actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -185,22 +142,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - int expected, - int actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -222,22 +163,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - Array expected, - Array actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -259,22 +184,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - uint expected, - uint actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -296,22 +205,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - short expected, - short actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -333,22 +226,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - ushort expected, - ushort actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -370,22 +247,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - long expected, - long actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -407,22 +268,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - ulong expected, - ulong actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -444,22 +289,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - byte expected, - byte actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -481,22 +310,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - char expected, - char actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -518,22 +331,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - sbyte expected, - sbyte actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -555,22 +352,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - double expected, - double actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -592,22 +373,6 @@ public static void AreEqual( } } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - float expected, - float actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -631,22 +396,6 @@ public static void AreEqual( message); } - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - string expected, - string actual, - string message = "") => AreEqual( - expected, - actual, - message); - /// /// Tests whether the specified values are equal and throws an exception if the two values are unequal. /// @@ -667,23 +416,6 @@ public static void AreEqual( message); } } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreEqual.")] - public static void Equal( - DateTime expected, - DateTime actual, - string message = "") => AreEqual( - expected, - actual, - message); - #endregion #region NotEqual @@ -730,22 +462,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - bool notExpected, - bool actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -767,22 +483,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - int notExpected, - int actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -804,22 +504,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - Array notExpected, - Array actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -841,22 +525,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - uint notExpected, - uint actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -878,22 +546,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - short notExpected, - short actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -915,22 +567,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - ushort notExpected, - ushort actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -952,22 +588,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - long notExpected, - long actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. @@ -990,22 +610,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - ulong notExpected, - ulong actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1027,22 +631,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - byte notExpected, - byte actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1064,22 +652,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - char notExpected, - char actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1101,22 +673,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - sbyte notExpected, - sbyte actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1138,22 +694,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - double notExpected, - double actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1175,22 +715,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - float notExpected, - float actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1212,22 +736,6 @@ public static void AreNotEqual( } } - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - string notExpected, - string actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - /// /// Tests whether the specified values are unequal and throws an exception if the two values are equal. /// @@ -1248,23 +756,6 @@ public static void AreNotEqual( message); } } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match . - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotEqual.")] - public static void NotEqual( - DateTime notExpected, - DateTime actual, - string message = "") => AreNotEqual( - notExpected, - actual, - message); - #endregion #region string @@ -1388,40 +879,6 @@ public static void StartsWith( #endregion - #region collection - - /// - /// Tests whether the specified collection is empty. - /// - /// The collection the test expects to be empty. - /// The message to include in the exception when the collection is empty. The message is shown in test results. - /// Raises an exception if the collection is not empty. - [Obsolete("This method is deprecated and will be removed in a future version. Use the method with the same name in CollectionAssert class.")] - public static void Empty(ICollection collection, string message = "") - { - if (collection.Count != 0) - { - HandleFail("Assert.Empty", message); - } - } - - /// - /// Tests whether the specified collection is not empty. - /// - /// The collection the test expects not to be empty. - /// The message to include in the exception when the collection is not empty. The message is shown in test results. - /// Raises an exception if the collection is not empty. - [Obsolete("This method is deprecated and will be removed in a future version. Use the method with the same name in CollectionAssert class.")] - public static void NotEmpty(ICollection collection, string message = "") - { - if (collection.Count == 0) - { - HandleFail("Assert.NotEmpty", message); - } - } - - #endregion region - #region types, objects /// @@ -1454,21 +911,6 @@ public static void IsInstanceOfType( } } - /// - /// Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of the object. - /// - /// The expected type of value. - /// The object the test expects to be of the specified type. - /// The message to include in the exception when value is not an instance of expectedType. The message is shown in test results. - /// Thrown if is or is not in the inheritance hierarchy of . - public static void IsType( - Type expectedType, - object value, - string message = "") => IsInstanceOfType( - value, - expectedType, - message); - /// /// Tests whether the specified object is not an instance of the wrong type and throws an exception if the specified type is in the inheritance hierarchy of the object. /// @@ -1502,22 +944,6 @@ public static void IsNotInstanceOfType( } } - /// - /// Tests whether the specified object is not an instance of the wrong type and throws an exception if the specified type is in the inheritance hierarchy of the object. - /// - /// The object the test expects not to be of the specified type. - /// The type that value should not be. - /// The message to include in the exception when value is an instance of wrongType. The message is shown in test results./param> - /// Thrown if is not null and is in the inheritance hierarchy of value. - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsNotInstanceOfType.")] - public static void IsNotType( - Type wrongType, - object value, - string message = "") => IsNotInstanceOfType( - value, - wrongType, - message); - /// /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. /// @@ -1543,22 +969,6 @@ public static void AreSame( } } - /// - /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. - /// - /// The first object to compare. This is the value the test expects. - /// The second object to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not the same as . The message is shown in test results. - /// Thrown if does not refer to the same object as . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreSame.")] - public static void Same( - object expected, - object actual, - string message = "") => AreSame( - expected, - actual, - message); - /// /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. /// @@ -1576,21 +986,6 @@ public static void AreNotSame( HandleFail("Assert.AreNotSame", message); } } - /// - /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. - /// - /// The first object to compare. This is the value the test expects not to match actual. - /// The second object to compare. This is the value produced by the code under test. - /// The message to include in the exception when is the same as . The message is shown in test results. - /// Thrown if refers to the same object as . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method AreNotSame.")] - public static void NotSame( - object notExpected, - object actual, - string message = "") => AreNotSame( - notExpected, - actual, - message); /// /// Tests whether the specified object is null and throws an exception if it is not. @@ -1606,15 +1001,6 @@ public static void IsNull(object value, [CallerArgumentExpression(nameof(value)) } } - /// - /// Tests whether the specified object is null and throws an exception if it is not. - /// - /// The object the test expects to be null. - /// The message to include in the exception when value is not null. The message is shown in test results. - /// Thrown if value is not null. - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsNull.")] - public static void Null(object value, string message = "") => IsNull(value, message); - /// /// Tests whether the specified object is non-null and throws an exception if it is null. /// @@ -1629,15 +1015,6 @@ public static void IsNotNull([NotNull] object value, [CallerArgumentExpression(n } } - /// - /// Tests whether the specified object is non-null and throws an exception if it is null. - /// - /// The object the test expects not to be null. - /// The message to include in the exception when value is null. The message is shown in test results. - /// Thrown if value is null. - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method IsNotNull.")] - public static void NotNull(object obj, string message = "") => IsNotNull(obj, message); - /// /// Tests whether the code specified by delegate action throws exact given exception /// of type (and not of derived type) and throws if code @@ -1683,25 +1060,6 @@ public static void ThrowsException( HandleFail("Assert.ThrowsException", empty); } - /// - /// Tests whether the code specified by delegate action throws exact given exception - /// of type (and not of derived type) and throws if code - /// does not throw exception or throws exception of type other than . - /// - /// Type of exception expected to be thrown. - /// Delegate to code to be tested and which is expected to throw exception. - /// The message to include in the exception when action does not throw exception of type . - /// Thrown if action does not throw exception of type . - /// Thrown if is . - [Obsolete("This method is deprecated and will be removed in a future version. Use the new method ThrowsException.")] - public static void Throws( - Type exceptionType, - Action action, - string message = "") => ThrowsException( - exceptionType, - action, - message); - #endregion internal static void HandleFail(string assertionName, string message) diff --git a/source/TestFramework/nanoFramework.TestFramework.nfproj b/source/TestFramework/nanoFramework.TestFramework.nfproj index 732f2bb..510d2d5 100644 --- a/source/TestFramework/nanoFramework.TestFramework.nfproj +++ b/source/TestFramework/nanoFramework.TestFramework.nfproj @@ -31,6 +31,9 @@ + + Assert.cs + From d9977fd3dfdffcb0af0f02f81301769b6c2a688f Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 11:18:26 -0700 Subject: [PATCH 02/23] Simplify SkipTestException --- source/TestFramework/Assert.cs | 11 +--------- .../TestFrameworkShared/SkipTestException.cs | 20 ++----------------- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 0e48cd7..a8f66a6 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -32,20 +32,11 @@ public sealed partial class Assert private const string AreNotEqualFailMsg = "Expected any value except:<{1}>. Actual:<{2}>. {0}"; private const string NullParameterToAssert = "The parameter '{0}' is invalid. The value cannot be null. {1}."; - #region SkipTest - - public static void SkipTest(string message = "") + public static void SkipTest(string message = null) { - if (string.IsNullOrEmpty(message)) - { - throw new SkipTestException(); - } - throw new SkipTestException(message); } - #endregion - #region true/false /// diff --git a/source/TestFrameworkShared/SkipTestException.cs b/source/TestFrameworkShared/SkipTestException.cs index 02f6ee2..8b80c62 100644 --- a/source/TestFrameworkShared/SkipTestException.cs +++ b/source/TestFrameworkShared/SkipTestException.cs @@ -9,33 +9,17 @@ namespace nanoFramework.TestFramework { /// - /// To skip a test, raise this exception thru the Assert.SkipTest("some message"); + /// To skip a test, raise this exception through the Assert.SkipTest("some message"); /// public class SkipTestException : Exception { - /// - /// Initializes a new instance of the SkipTestException class. - /// - public SkipTestException() - : base() - { } - - /// - /// Initializes a new instance of the SkipTestException class with a specified error message. - /// - /// The message that describes the error. - public SkipTestException(string message) - : base(message) - { } - /// /// Initializes a new instance of the SkipTestException class with a specified error message /// and a reference to the inner SkipTestException that is the cause of this exception. /// /// The message that describes the error. /// - public SkipTestException(string message, Exception innerException) - : base(message, innerException) + public SkipTestException(string message = null, Exception innerException = null) : base(message, innerException) { } } } From 38dda7cce9695e47d1bd24bdcaaf163844983b51 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 11:31:06 -0700 Subject: [PATCH 03/23] Improve parameter null check --- source/TestFramework/Assert.cs | 45 ++++++++++-------------- source/TestFramework/CollectionAssert.cs | 5 +-- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index a8f66a6..cdf8c76 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -30,7 +30,16 @@ public sealed partial class Assert private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; private const string AreEqualFailMsg = "Expected:<{1}>. Actual:<{2}>. {0}"; private const string AreNotEqualFailMsg = "Expected any value except:<{1}>. Actual:<{2}>. {0}"; - private const string NullParameterToAssert = "The parameter '{0}' is invalid. The value cannot be null. {1}."; + + internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) + { + if (value is not null) + { + return; + } + + HandleFail(assertion, $"The parameter '{parameter}' is invalid. The value cannot be null."); + } public static void SkipTest(string message = null) { @@ -763,8 +772,8 @@ public static void Contains( string other, string message = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(expected, "Assert.Contains"); + EnsureParameterIsNotNull(other, "Assert.Contains"); if (other.IndexOf(expected) < 0) { @@ -791,8 +800,8 @@ public static void DoesNotContains( string other, string message = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(expected, "Assert.Contains"); + EnsureParameterIsNotNull(other, "Assert.Contains"); if (other.IndexOf(expected) >= 0) { @@ -819,8 +828,8 @@ public static void EndsWith( string other, string message = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(expected, "Assert.Contains"); + EnsureParameterIsNotNull(other, "Assert.Contains"); // We have to take the last index as the text can contains multiple times the same word if (other.LastIndexOf(expected) == other.Length - expected.Length) @@ -850,8 +859,8 @@ public static void StartsWith( string other, string message = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(expected, "Assert.Contains"); + EnsureParameterIsNotNull(other, "Assert.Contains"); if (other.IndexOf(expected) == 0) { @@ -1134,23 +1143,5 @@ private static void HandleAreNotEqualFail( HandleFail("Assert.AreNotEqual", message2); } - - internal static void CheckParameterNotNull( - object param, - string assertionName, - string parameterName, - string message) - { - if (param == null) - { - HandleFail( - assertionName, - string.Format(NullParameterToAssert, new object[2] - { - parameterName, - message - })); - } - } } } diff --git a/source/TestFramework/CollectionAssert.cs b/source/TestFramework/CollectionAssert.cs index aa0a97b..5c74432 100644 --- a/source/TestFramework/CollectionAssert.cs +++ b/source/TestFramework/CollectionAssert.cs @@ -6,6 +6,7 @@ using System.Collections; +using TestFrameworkShared; namespace nanoFramework.TestFramework { @@ -33,7 +34,7 @@ public sealed class CollectionAssert /// public static void Empty(ICollection collection, string message = "") { - Assert.CheckParameterNotNull(collection, "CollectionAssert.Empty", "collection", string.Empty); + Assert.EnsureParameterIsNotNull(collection, "CollectionAssert.Empty"); if (collection.Count != 0) { @@ -49,7 +50,7 @@ public static void Empty(ICollection collection, string message = "") /// Raises an exception if the collection is not empty. public static void NotEmpty(ICollection collection, string message = "") { - Assert.CheckParameterNotNull(collection, "CollectionAssert.NotEmpty", "collection", string.Empty); + Assert.EnsureParameterIsNotNull(collection, "CollectionAssert.NotEmpty"); if (collection.Count == 0) { From 9ea450e64021e64e2447ce7c96ed54b27d3dec82 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:16:27 -0700 Subject: [PATCH 04/23] Improve IsInstanceOfType/IsNotInstanceOfType with CallerArgumentExpression --- source/TestFramework/Assert.cs | 148 ++++++++++++++------------------- 1 file changed, 64 insertions(+), 84 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index cdf8c76..4b5df7a 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -17,13 +17,12 @@ namespace nanoFramework.TestFramework public sealed partial class Assert { private const string AssertionFailed = "{0} failed. {1}"; - private const string Common_NullInMessages = "(null)"; + private const string NullAsString = "(null)"; + private const string Common_ObjectString = "(object)"; private const string WrongExceptionThrown = "Threw exception {2}, but exception {1} was expected. {0}\r\nException Message: {3}"; private const string NoExceptionThrown = "No exception thrown. {1} exception was expected. {0}"; private const string AreSameGivenValues = "Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}"; - private const string IsNotInstanceOfFailMsg = "Wrong Type:<{1}>. Actual type:<{2}>. {0}"; - private const string IsInstanceOfFailMsg = "{0} Expected type:<{1}>. Actual type:<{2}>."; private const string StringContainsFailMsg = "{2} does not contains {1}. {0}"; private const string StringDoesNotContainsFailMsg = "{2} should not contain {1}. {0}"; private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; @@ -41,12 +40,62 @@ internal static void EnsureParameterIsNotNull(object value, string assertion, [C HandleFail(assertion, $"The parameter '{parameter}' is invalid. The value cannot be null."); } - public static void SkipTest(string 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 condition is true. The message is shown in test results. + /// Thrown if condition is . + public static void IsFalse(bool condition, [CallerArgumentExpression(nameof(condition))] string message = "") { - throw new SkipTestException(message); + if (condition) + { + HandleFail("Assert.IsFalse", message); + } + } + + /// + /// Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of the object. + /// + /// The expected type of value. + /// The object the test expects to be of the specified type. + /// The message to include in the exception when value is not an instance of expected. The message is shown in test results. + /// Thrown if is or is not in the inheritance hierarchy of . + public static void IsInstanceOfType(object value, Type expected, [CallerArgumentExpression(nameof(value))] string message = "") + { + EnsureParameterIsNotNull(expected, "Assert.IsInstanceOfType"); + + if (value is not null && expected == value.GetType()) + { + return; + } + + // ReSharper disable once MergeConditionalExpression + #pragma warning disable IDE0031 // IDE keeps suggesting I change this to value?.GetType() but since we don't have Nullable this won't work in all cases. + var actual = value is null ? null : value.GetType(); + #pragma warning restore IDE0031 + + HandleFail("Assert.IsInstanceOfType", $"Expected type:<{expected}>. Actual type:<{ReplaceNulls(actual)}>. {ReplaceNulls(message)}"); } - #region true/false + /// + /// Tests whether the specified object is not an instance of the wrong type and throws an exception if the specified type is in the inheritance hierarchy of the object. + /// + /// The object the test expects not to be of the specified type. + /// The type that value should not be. + /// The message to include in the exception when value is an instance of notExpected. The message is shown in test results. + /// Thrown if is not and is in the inheritance hierarchy of . + public static void IsNotInstanceOfType(object value, Type notExpected, [CallerArgumentExpression(nameof(value))] string message = "") + { + EnsureParameterIsNotNull(notExpected, "Assert.IsNotInstanceOfType"); + + if (value is null || notExpected != value.GetType()) + { + return; + } + + HandleFail("Assert.IsNotInstanceOfType", $"Wrong type:<{notExpected}>. Actual type:<{value.GetType()}>. {ReplaceNulls(message)}"); + } /// /// Tests whether the specified condition is true and throws an exception if the condition is false. @@ -62,20 +111,12 @@ public static void IsTrue(bool condition, [CallerArgumentExpression(nameof(condi } } - /// - /// 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 condition is true. The message is shown in test results. - /// Thrown if condition is . - public static void IsFalse(bool condition, [CallerArgumentExpression(nameof(condition))] string message = "") + public static void SkipTest(string message = null) { - if (condition) - { - HandleFail("Assert.IsFalse", message); - } + throw new SkipTestException(message); } - #endregion + + #region Equal @@ -881,68 +922,6 @@ public static void StartsWith( #region types, objects - /// - /// Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of the object. - /// - /// The expected type of value. - /// The object the test expects to be of the specified type. - /// The message to include in the exception when value is not an instance of expectedType. The message is shown in test results. - /// Thrown if is or is not in the inheritance hierarchy of . - public static void IsInstanceOfType( - object value, - Type expectedType, - string message = "") - { - if (expectedType == null || value == null) - { - HandleFail("Assert.IsInstanceOfType", message); - } - - if (expectedType != value.GetType()) - { - string message2 = string.Format(IsInstanceOfFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - expectedType, - value.GetType() - }); - - HandleFail("Assert.IsInstanceOfType", message2); - } - } - - /// - /// Tests whether the specified object is not an instance of the wrong type and throws an exception if the specified type is in the inheritance hierarchy of the object. - /// - /// The object the test expects not to be of the specified type. - /// The type that value should not be. - /// The message to include in the exception when value is an instance of wrongType. The message is shown in test results./param> - /// Thrown if is not and is in the inheritance hierarchy of . - public static void IsNotInstanceOfType( - object value, - Type wrongType, - string message = "") - { - if ((object)wrongType == null) - { - HandleFail("Assert.IsNotInstanceOfType", message); - } - - if (value != null) - { - if (wrongType != value.GetType()) - { - string message2 = string.Format(IsNotInstanceOfFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - wrongType, - value.GetType() - }); - - HandleFail("Assert.IsNotInstanceOfType", message2); - } - } - } /// /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. @@ -1062,23 +1041,24 @@ public static void ThrowsException( } #endregion - internal static void HandleFail(string assertionName, string message) + [DoesNotReturn] + internal static void HandleFail(string assertion, string message) { - string text = string.Empty; + var text = string.Empty; if (!string.IsNullOrEmpty(message)) { text = ReplaceNulls(message); } - throw new AssertFailedException(string.Format(AssertionFailed, new object[2] { assertionName, text })); + throw new AssertFailedException(string.Format(AssertionFailed, assertion, text)); } internal static string ReplaceNulls(object input) { if (input == null) { - return Common_NullInMessages; + return NullAsString; } string text = input.ToString(); From 32bb32262e2615dcb9529eb7e6127892e81e7f33 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:21:49 -0700 Subject: [PATCH 05/23] Move AreEqual methods to separate file --- source/TestFramework/Assert.AreEqual.cs | 241 ++++++++++++ source/TestFramework/Assert.cs | 358 ------------------ .../nanoFramework.TestFramework.nfproj | 7 +- 3 files changed, 246 insertions(+), 360 deletions(-) create mode 100644 source/TestFramework/Assert.AreEqual.cs diff --git a/source/TestFramework/Assert.AreEqual.cs b/source/TestFramework/Assert.AreEqual.cs new file mode 100644 index 0000000..8a47a6c --- /dev/null +++ b/source/TestFramework/Assert.AreEqual.cs @@ -0,0 +1,241 @@ +using System; +using TestFrameworkShared; + +namespace nanoFramework.TestFramework +{ + // ReSharper disable StringCompareIsCultureSpecific.1 + public sealed partial class Assert + { + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(bool expected, bool actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(byte expected, byte actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(char expected, char actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(DateTime expected, DateTime actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(double expected, double actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(float expected, float actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(int expected, int actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(long expected, long actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified objects are equal and throws an exception if the two objects are unequal. + /// + /// The first objects to compare. This is the objects the tests expects. + /// The second objects to compare. This is the objects produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(object expected, object actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(sbyte expected, sbyte actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(short expected, short actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(string expected, string actual, string message = "") + { + if (string.Compare(expected, actual) == 0) + { + return; + } + + HandleAreEqualFail(expected, actual, message); + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(uint expected, uint actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(ulong expected, ulong actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + /// + /// Tests whether the specified values are equal and throws an exception if the two values are unequal. + /// + /// The first value to compare. This is the value the tests expects. + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not equal to . The message is shown in test results. + /// Thrown if is not equal to . + public static void AreEqual(ushort expected, ushort actual, string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, message); + } + } + + private static void HandleAreEqualFail(object expected, object actual, string message) + { + HandleFail("Assert.AreEqual", $"Expected:<{ReplaceNulls(expected)}>. Actual:<{ReplaceNulls(actual)}>. {(message is null ? string.Empty : ReplaceNulls(message))}"); + } + } +} diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 4b5df7a..adf781b 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -116,349 +116,6 @@ public static void SkipTest(string message = null) throw new SkipTestException(message); } - - - #region Equal - - /// - /// Tests whether the specified objects are equal and throws an exception if the two objects are unequal. - /// - /// The first objects to compare. This is the objects the tests expects. - /// The second objects to compare. This is the objects produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - object expected, - object actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - bool expected, - bool actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - int expected, - int actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - Array expected, - Array actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - uint expected, - uint actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - short expected, - short actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - ushort expected, - ushort actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - long expected, - long actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - ulong expected, - ulong actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - byte expected, - byte actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - char expected, - char actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - sbyte expected, - sbyte actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - double expected, - double actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - float expected, - float actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - string expected, - string actual, - string message = "") - { - if (string.Compare(expected, actual) == 0) - { - return; - } - - HandleAreEqualFail( - expected, - actual, - message); - } - - /// - /// Tests whether the specified values are equal and throws an exception if the two values are unequal. - /// - /// The first value to compare. This is the value the tests expects. - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not equal to . The message is shown in test results. - /// Thrown if is not equal to . - public static void AreEqual( - DateTime expected, - DateTime actual, - string message = "") - { - if (!object.Equals(expected, actual)) - { - HandleAreEqualFail( - expected, - actual, - message); - } - } - #endregion - #region NotEqual /// @@ -1094,21 +751,6 @@ internal static string ReplaceNullChars(string input) return replacedString.ToString(); } - private static void HandleAreEqualFail( - object expected, - object actual, - string message) - { - string message2 = string.Format(AreEqualFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(actual) - }); - - HandleFail("Assert.AreEqual", message2); - } - private static void HandleAreNotEqualFail( object expected, object actual, diff --git a/source/TestFramework/nanoFramework.TestFramework.nfproj b/source/TestFramework/nanoFramework.TestFramework.nfproj index 510d2d5..794b826 100644 --- a/source/TestFramework/nanoFramework.TestFramework.nfproj +++ b/source/TestFramework/nanoFramework.TestFramework.nfproj @@ -31,13 +31,16 @@ - + + Assert.cs + + Assert.cs + - From 86f304d308c8bea6edebf599d3dda4b74957e637 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:25:15 -0700 Subject: [PATCH 06/23] Moved AreNotEqual to separate file --- source/TestFramework/Assert.AreNotEqual.cs | 10 + source/TestFramework/Assert.cs | 355 ------------------ .../nanoFramework.TestFramework.nfproj | 3 + 3 files changed, 13 insertions(+), 355 deletions(-) create mode 100644 source/TestFramework/Assert.AreNotEqual.cs diff --git a/source/TestFramework/Assert.AreNotEqual.cs b/source/TestFramework/Assert.AreNotEqual.cs new file mode 100644 index 0000000..aacfaff --- /dev/null +++ b/source/TestFramework/Assert.AreNotEqual.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace nanoFramework.TestFramework +{ + public class Assert + { + } +} diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index adf781b..3ab0fec 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -116,346 +116,6 @@ public static void SkipTest(string message = null) throw new SkipTestException(message); } - #region NotEqual - - /// - /// Tests whether the specified object are unequal and throws an exception if the two object are equal. - /// - /// The first object to compare. This is the object the tests expects. - /// The second object to compare. This is the object produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal to . - public static void AreNotEqual( - object notExpected, - object actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - bool notExpected, - bool actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - int notExpected, - int actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - Array notExpected, - Array actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - uint notExpected, - uint actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - short notExpected, - short actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - ushort notExpected, - ushort actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - long notExpected, - long actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - ulong notExpected, - ulong actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - byte notExpected, - byte actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - char notExpected, - char actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - sbyte notExpected, - sbyte actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - double notExpected, - double actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - float notExpected, - float actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - string notExpected, - string actual, - string message = "") - { - if (string.Compare(notExpected, actual) == 0) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - - /// - /// Tests whether the specified values are unequal and throws an exception if the two values are equal. - /// - /// The first value to compare. This is the value the test expects not to match - /// The second value to compare. This is the value produced by the code under test. - /// The message to include in the exception when is equal to . The message is shown in test results. - /// Thrown if is equal . - public static void AreNotEqual( - DateTime notExpected, - DateTime actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - #endregion - #region string /// @@ -750,20 +410,5 @@ internal static string ReplaceNullChars(string input) return replacedString.ToString(); } - - private static void HandleAreNotEqualFail( - object expected, - object actual, - string message) - { - string message2 = string.Format(AreNotEqualFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(actual) - }); - - HandleFail("Assert.AreNotEqual", message2); - } } } diff --git a/source/TestFramework/nanoFramework.TestFramework.nfproj b/source/TestFramework/nanoFramework.TestFramework.nfproj index 794b826..daf95d1 100644 --- a/source/TestFramework/nanoFramework.TestFramework.nfproj +++ b/source/TestFramework/nanoFramework.TestFramework.nfproj @@ -33,6 +33,9 @@ + Assert.cs + + Assert.cs From bb9dbd1a0508fb2c82166b995c102b06523b096a Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:27:34 -0700 Subject: [PATCH 07/23] Added DoesNotReturn --- source/TestFramework/Assert.AreEqual.cs | 2 + source/TestFramework/Assert.AreNotEqual.cs | 237 ++++++++++++++++++++- source/TestFramework/Assert.cs | 7 +- 3 files changed, 239 insertions(+), 7 deletions(-) diff --git a/source/TestFramework/Assert.AreEqual.cs b/source/TestFramework/Assert.AreEqual.cs index 8a47a6c..7e92156 100644 --- a/source/TestFramework/Assert.AreEqual.cs +++ b/source/TestFramework/Assert.AreEqual.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using TestFrameworkShared; namespace nanoFramework.TestFramework @@ -233,6 +234,7 @@ public static void AreEqual(ushort expected, ushort actual, string message = "") } } + [DoesNotReturn] private static void HandleAreEqualFail(object expected, object actual, string message) { HandleFail("Assert.AreEqual", $"Expected:<{ReplaceNulls(expected)}>. Actual:<{ReplaceNulls(actual)}>. {(message is null ? string.Empty : ReplaceNulls(message))}"); diff --git a/source/TestFramework/Assert.AreNotEqual.cs b/source/TestFramework/Assert.AreNotEqual.cs index aacfaff..6dbae38 100644 --- a/source/TestFramework/Assert.AreNotEqual.cs +++ b/source/TestFramework/Assert.AreNotEqual.cs @@ -1,10 +1,241 @@ using System; -using System.Collections.Generic; -using System.Text; +using System.Diagnostics.CodeAnalysis; +using TestFrameworkShared; namespace nanoFramework.TestFramework { - public class Assert + // ReSharper disable StringCompareIsCultureSpecific.1 + public sealed partial class Assert { + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(bool notExpected, bool actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(byte notExpected, byte actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(char notExpected, char actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(DateTime notExpected, DateTime actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(double notExpected, double actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(float notExpected, float actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(long notExpected, long actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(int notExpected, int actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified object are unequal and throws an exception if the two object are equal. + /// + /// The first object to compare. This is the object the tests expects. + /// The second object to compare. This is the object produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal to . + public static void AreNotEqual(object notExpected, object actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(sbyte notExpected, sbyte actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(short notExpected, short actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(string notExpected, string actual, string message = "") + { + if (string.Compare(notExpected, actual) == 0) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(uint notExpected, uint actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(ulong notExpected, ulong actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + /// + /// Tests whether the specified values are unequal and throws an exception if the two values are equal. + /// + /// The first value to compare. This is the value the test expects not to match + /// The second value to compare. This is the value produced by the code under test. + /// The message to include in the exception when is equal to . The message is shown in test results. + /// Thrown if is equal . + public static void AreNotEqual(ushort notExpected, ushort actual, string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + [DoesNotReturn] + private static void HandleAreNotEqualFail(object expected, object actual, string message) + { + HandleFail("Assert.AreNotEqual", $"Expected any value except:<{ReplaceNulls(expected)}>. Actual:<{ReplaceNulls(actual)}>. {(message is null ? string.Empty : ReplaceNulls(message))}"); + } } } diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 3ab0fec..14547ab 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -17,9 +17,9 @@ namespace nanoFramework.TestFramework public sealed partial class Assert { private const string AssertionFailed = "{0} failed. {1}"; + private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; - private const string Common_ObjectString = "(object)"; private const string WrongExceptionThrown = "Threw exception {2}, but exception {1} was expected. {0}\r\nException Message: {3}"; private const string NoExceptionThrown = "No exception thrown. {1} exception was expected. {0}"; private const string AreSameGivenValues = "Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}"; @@ -27,8 +27,6 @@ public sealed partial class Assert private const string StringDoesNotContainsFailMsg = "{2} should not contain {1}. {0}"; private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; - private const string AreEqualFailMsg = "Expected:<{1}>. Actual:<{2}>. {0}"; - private const string AreNotEqualFailMsg = "Expected any value except:<{1}>. Actual:<{2}>. {0}"; internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) { @@ -111,6 +109,7 @@ public static void IsTrue(bool condition, [CallerArgumentExpression(nameof(condi } } + [DoesNotReturn] public static void SkipTest(string message = null) { throw new SkipTestException(message); @@ -381,7 +380,7 @@ internal static string ReplaceNulls(object input) string text = input.ToString(); if (text == null) { - return Common_ObjectString; + return ObjectAsString; } return ReplaceNullChars(text); From 5c08122ed462273345f1e5269e6851b93b67ca98 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:30:41 -0700 Subject: [PATCH 08/23] Add CallerArgumentExpression to AreEqual/AreNotEqual --- source/TestFramework/Assert.AreEqual.cs | 31 +++++++++---------- source/TestFramework/Assert.AreNotEqual.cs | 35 +++++++++++----------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/source/TestFramework/Assert.AreEqual.cs b/source/TestFramework/Assert.AreEqual.cs index 7e92156..0e87c6d 100644 --- a/source/TestFramework/Assert.AreEqual.cs +++ b/source/TestFramework/Assert.AreEqual.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using TestFrameworkShared; namespace nanoFramework.TestFramework @@ -14,7 +15,7 @@ public sealed partial class Assert /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(bool expected, bool actual, string message = "") + public static void AreEqual(bool expected, bool actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -29,7 +30,7 @@ public static void AreEqual(bool expected, bool actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(byte expected, byte actual, string message = "") + public static void AreEqual(byte expected, byte actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -44,7 +45,7 @@ public static void AreEqual(byte expected, byte actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(char expected, char actual, string message = "") + public static void AreEqual(char expected, char actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -59,7 +60,7 @@ public static void AreEqual(char expected, char actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(DateTime expected, DateTime actual, string message = "") + public static void AreEqual(DateTime expected, DateTime actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -74,7 +75,7 @@ public static void AreEqual(DateTime expected, DateTime actual, string message = /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(double expected, double actual, string message = "") + public static void AreEqual(double expected, double actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -89,7 +90,7 @@ public static void AreEqual(double expected, double actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(float expected, float actual, string message = "") + public static void AreEqual(float expected, float actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -104,7 +105,7 @@ public static void AreEqual(float expected, float actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(int expected, int actual, string message = "") + public static void AreEqual(int expected, int actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -119,7 +120,7 @@ public static void AreEqual(int expected, int actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(long expected, long actual, string message = "") + public static void AreEqual(long expected, long actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -134,7 +135,7 @@ public static void AreEqual(long expected, long actual, string message = "") /// The second objects to compare. This is the objects produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(object expected, object actual, string message = "") + public static void AreEqual(object expected, object actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -149,7 +150,7 @@ public static void AreEqual(object expected, object actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(sbyte expected, sbyte actual, string message = "") + public static void AreEqual(sbyte expected, sbyte actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -164,7 +165,7 @@ public static void AreEqual(sbyte expected, sbyte actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(short expected, short actual, string message = "") + public static void AreEqual(short expected, short actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -179,7 +180,7 @@ public static void AreEqual(short expected, short actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(string expected, string actual, string message = "") + public static void AreEqual(string expected, string actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (string.Compare(expected, actual) == 0) { @@ -196,7 +197,7 @@ public static void AreEqual(string expected, string actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(uint expected, uint actual, string message = "") + public static void AreEqual(uint expected, uint actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -211,7 +212,7 @@ public static void AreEqual(uint expected, uint actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(ulong expected, ulong actual, string message = "") + public static void AreEqual(ulong expected, ulong actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { @@ -226,7 +227,7 @@ public static void AreEqual(ulong expected, ulong actual, string message = "") /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(ushort expected, ushort actual, string message = "") + public static void AreEqual(ushort expected, ushort actual, [CallerArgumentExpression(nameof(expected))] string message = "") { if (!Equals(expected, actual)) { diff --git a/source/TestFramework/Assert.AreNotEqual.cs b/source/TestFramework/Assert.AreNotEqual.cs index 6dbae38..574d4d0 100644 --- a/source/TestFramework/Assert.AreNotEqual.cs +++ b/source/TestFramework/Assert.AreNotEqual.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using TestFrameworkShared; namespace nanoFramework.TestFramework @@ -14,7 +15,7 @@ public sealed partial class Assert /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(bool notExpected, bool actual, string message = "") + public static void AreNotEqual(bool notExpected, bool actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -29,7 +30,7 @@ public static void AreNotEqual(bool notExpected, bool actual, string message = " /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(byte notExpected, byte actual, string message = "") + public static void AreNotEqual(byte notExpected, byte actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -44,7 +45,7 @@ public static void AreNotEqual(byte notExpected, byte actual, string message = " /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(char notExpected, char actual, string message = "") + public static void AreNotEqual(char notExpected, char actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -59,7 +60,7 @@ public static void AreNotEqual(char notExpected, char actual, string message = " /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(DateTime notExpected, DateTime actual, string message = "") + public static void AreNotEqual(DateTime notExpected, DateTime actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -74,7 +75,7 @@ public static void AreNotEqual(DateTime notExpected, DateTime actual, string mes /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(double notExpected, double actual, string message = "") + public static void AreNotEqual(double notExpected, double actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -89,7 +90,7 @@ public static void AreNotEqual(double notExpected, double actual, string message /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(float notExpected, float actual, string message = "") + public static void AreNotEqual(float notExpected, float actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -104,7 +105,7 @@ public static void AreNotEqual(float notExpected, float actual, string message = /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(long notExpected, long actual, string message = "") + public static void AreNotEqual(long notExpected, long actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -119,7 +120,7 @@ public static void AreNotEqual(long notExpected, long actual, string message = " /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(int notExpected, int actual, string message = "") + public static void AreNotEqual(int notExpected, int actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -134,7 +135,7 @@ public static void AreNotEqual(int notExpected, int actual, string message = "") /// The second object to compare. This is the object produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal to . - public static void AreNotEqual(object notExpected, object actual, string message = "") + public static void AreNotEqual(object notExpected, object actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -149,7 +150,7 @@ public static void AreNotEqual(object notExpected, object actual, string message /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(sbyte notExpected, sbyte actual, string message = "") + public static void AreNotEqual(sbyte notExpected, sbyte actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -164,7 +165,7 @@ public static void AreNotEqual(sbyte notExpected, sbyte actual, string message = /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(short notExpected, short actual, string message = "") + public static void AreNotEqual(short notExpected, short actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -179,7 +180,7 @@ public static void AreNotEqual(short notExpected, short actual, string message = /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(string notExpected, string actual, string message = "") + public static void AreNotEqual(string notExpected, string actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (string.Compare(notExpected, actual) == 0) { @@ -194,7 +195,7 @@ public static void AreNotEqual(string notExpected, string actual, string message /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(uint notExpected, uint actual, string message = "") + public static void AreNotEqual(uint notExpected, uint actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -209,7 +210,7 @@ public static void AreNotEqual(uint notExpected, uint actual, string message = " /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(ulong notExpected, ulong actual, string message = "") + public static void AreNotEqual(ulong notExpected, ulong actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -224,7 +225,7 @@ public static void AreNotEqual(ulong notExpected, ulong actual, string message = /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(ushort notExpected, ushort actual, string message = "") + public static void AreNotEqual(ushort notExpected, ushort actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") { if (Equals(notExpected, actual)) { @@ -233,9 +234,9 @@ public static void AreNotEqual(ushort notExpected, ushort actual, string message } [DoesNotReturn] - private static void HandleAreNotEqualFail(object expected, object actual, string message) + private static void HandleAreNotEqualFail(object notExpected, object actual, string message) { - HandleFail("Assert.AreNotEqual", $"Expected any value except:<{ReplaceNulls(expected)}>. Actual:<{ReplaceNulls(actual)}>. {(message is null ? string.Empty : ReplaceNulls(message))}"); + HandleFail("Assert.AreNotEqual", $"Expected any value except:<{ReplaceNulls(notExpected)}>. Actual:<{ReplaceNulls(actual)}>. {(message is null ? string.Empty : ReplaceNulls(message))}"); } } } From 34df58c8f344e3e1ee52689da8917e688e86d2fa Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:33:45 -0700 Subject: [PATCH 09/23] Change AreEqual/AreNotEqual argument expression to 'actual' --- source/TestFramework/Assert.AreEqual.cs | 30 +++++++++++----------- source/TestFramework/Assert.AreNotEqual.cs | 30 +++++++++++----------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/source/TestFramework/Assert.AreEqual.cs b/source/TestFramework/Assert.AreEqual.cs index 0e87c6d..7c1a6ae 100644 --- a/source/TestFramework/Assert.AreEqual.cs +++ b/source/TestFramework/Assert.AreEqual.cs @@ -15,7 +15,7 @@ public sealed partial class Assert /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(bool expected, bool actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(bool expected, bool actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -30,7 +30,7 @@ public static void AreEqual(bool expected, bool actual, [CallerArgumentExpressio /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(byte expected, byte actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(byte expected, byte actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -45,7 +45,7 @@ public static void AreEqual(byte expected, byte actual, [CallerArgumentExpressio /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(char expected, char actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(char expected, char actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -60,7 +60,7 @@ public static void AreEqual(char expected, char actual, [CallerArgumentExpressio /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(DateTime expected, DateTime actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(DateTime expected, DateTime actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -75,7 +75,7 @@ public static void AreEqual(DateTime expected, DateTime actual, [CallerArgumentE /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(double expected, double actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(double expected, double actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -90,7 +90,7 @@ public static void AreEqual(double expected, double actual, [CallerArgumentExpre /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(float expected, float actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(float expected, float actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -105,7 +105,7 @@ public static void AreEqual(float expected, float actual, [CallerArgumentExpress /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(int expected, int actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(int expected, int actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -120,7 +120,7 @@ public static void AreEqual(int expected, int actual, [CallerArgumentExpression( /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(long expected, long actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(long expected, long actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -135,7 +135,7 @@ public static void AreEqual(long expected, long actual, [CallerArgumentExpressio /// The second objects to compare. This is the objects produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(object expected, object actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(object expected, object actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -150,7 +150,7 @@ public static void AreEqual(object expected, object actual, [CallerArgumentExpre /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(sbyte expected, sbyte actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(sbyte expected, sbyte actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -165,7 +165,7 @@ public static void AreEqual(sbyte expected, sbyte actual, [CallerArgumentExpress /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(short expected, short actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(short expected, short actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -180,7 +180,7 @@ public static void AreEqual(short expected, short actual, [CallerArgumentExpress /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(string expected, string actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(string expected, string actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (string.Compare(expected, actual) == 0) { @@ -197,7 +197,7 @@ public static void AreEqual(string expected, string actual, [CallerArgumentExpre /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(uint expected, uint actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(uint expected, uint actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -212,7 +212,7 @@ public static void AreEqual(uint expected, uint actual, [CallerArgumentExpressio /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(ulong expected, ulong actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(ulong expected, ulong actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { @@ -227,7 +227,7 @@ public static void AreEqual(ulong expected, ulong actual, [CallerArgumentExpress /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is not equal to . The message is shown in test results. /// Thrown if is not equal to . - public static void AreEqual(ushort expected, ushort actual, [CallerArgumentExpression(nameof(expected))] string message = "") + public static void AreEqual(ushort expected, ushort actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (!Equals(expected, actual)) { diff --git a/source/TestFramework/Assert.AreNotEqual.cs b/source/TestFramework/Assert.AreNotEqual.cs index 574d4d0..2dba29b 100644 --- a/source/TestFramework/Assert.AreNotEqual.cs +++ b/source/TestFramework/Assert.AreNotEqual.cs @@ -15,7 +15,7 @@ public sealed partial class Assert /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(bool notExpected, bool actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(bool notExpected, bool actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -30,7 +30,7 @@ public static void AreNotEqual(bool notExpected, bool actual, [CallerArgumentExp /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(byte notExpected, byte actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(byte notExpected, byte actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -45,7 +45,7 @@ public static void AreNotEqual(byte notExpected, byte actual, [CallerArgumentExp /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(char notExpected, char actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(char notExpected, char actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -60,7 +60,7 @@ public static void AreNotEqual(char notExpected, char actual, [CallerArgumentExp /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(DateTime notExpected, DateTime actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(DateTime notExpected, DateTime actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -75,7 +75,7 @@ public static void AreNotEqual(DateTime notExpected, DateTime actual, [CallerArg /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(double notExpected, double actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(double notExpected, double actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -90,7 +90,7 @@ public static void AreNotEqual(double notExpected, double actual, [CallerArgumen /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(float notExpected, float actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(float notExpected, float actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -105,7 +105,7 @@ public static void AreNotEqual(float notExpected, float actual, [CallerArgumentE /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(long notExpected, long actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(long notExpected, long actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -120,7 +120,7 @@ public static void AreNotEqual(long notExpected, long actual, [CallerArgumentExp /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(int notExpected, int actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(int notExpected, int actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -135,7 +135,7 @@ public static void AreNotEqual(int notExpected, int actual, [CallerArgumentExpre /// The second object to compare. This is the object produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal to . - public static void AreNotEqual(object notExpected, object actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(object notExpected, object actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -150,7 +150,7 @@ public static void AreNotEqual(object notExpected, object actual, [CallerArgumen /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(sbyte notExpected, sbyte actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(sbyte notExpected, sbyte actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -165,7 +165,7 @@ public static void AreNotEqual(sbyte notExpected, sbyte actual, [CallerArgumentE /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(short notExpected, short actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(short notExpected, short actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -180,7 +180,7 @@ public static void AreNotEqual(short notExpected, short actual, [CallerArgumentE /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(string notExpected, string actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(string notExpected, string actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (string.Compare(notExpected, actual) == 0) { @@ -195,7 +195,7 @@ public static void AreNotEqual(string notExpected, string actual, [CallerArgumen /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(uint notExpected, uint actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(uint notExpected, uint actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -210,7 +210,7 @@ public static void AreNotEqual(uint notExpected, uint actual, [CallerArgumentExp /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(ulong notExpected, ulong actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(ulong notExpected, ulong actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { @@ -225,7 +225,7 @@ public static void AreNotEqual(ulong notExpected, ulong actual, [CallerArgumentE /// The second value to compare. This is the value produced by the code under test. /// The message to include in the exception when is equal to . The message is shown in test results. /// Thrown if is equal . - public static void AreNotEqual(ushort notExpected, ushort actual, [CallerArgumentExpression(nameof(notExpected))] string message = "") + public static void AreNotEqual(ushort notExpected, ushort actual, [CallerArgumentExpression(nameof(actual))] string message = "") { if (Equals(notExpected, actual)) { From 02673cc5c3ba329713dbcc4f3ac57154805186ca Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:36:49 -0700 Subject: [PATCH 10/23] Improve AreSame --- source/TestFramework/Assert.cs | 47 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 14547ab..2207be7 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -28,6 +28,29 @@ public sealed partial class Assert private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; + /// + /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. + /// + /// The first object to compare. This is the value the test expects. + /// The second object to compare. This is the value produced by the code under test. + /// The message to include in the exception when is not the same as . The message is shown in test results. + /// Thrown if does not refer to the same object as . + public static void AreSame(object expected, object actual, [CallerArgumentExpression(nameof(actual))] string message = "") + { + if (ReferenceEquals(expected, actual)) + { + return; + } + + if (expected is ValueType || actual is ValueType) + { + HandleFail("Assert.AreSame", $"Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {ReplaceNulls(message)}"); + + } + + HandleFail("Assert.AreSame", ReplaceNulls(message)); + } + internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) { if (value is not null) @@ -239,30 +262,6 @@ public static void StartsWith( #region types, objects - /// - /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. - /// - /// The first object to compare. This is the value the test expects. - /// The second object to compare. This is the value produced by the code under test. - /// The message to include in the exception when is not the same as . The message is shown in test results. - /// Thrown if does not refer to the same object as . - public static void AreSame( - object expected, - object actual, - string message = "") - { - if (expected != actual) - { - string message2 = message; - - if (expected is ValueType && actual is ValueType) - { - message2 = string.Format(AreSameGivenValues, new object[1] { (message == null) ? string.Empty : ReplaceNulls(message) }); - } - - HandleFail("Assert.AreSame", message2); - } - } /// /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. From 6c0dfd6dfb072ec7ff46f369d683603ee450d112 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:39:18 -0700 Subject: [PATCH 11/23] Improve AreNotSame --- source/TestFramework/Assert.cs | 35 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 2207be7..cc39cce 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -22,12 +22,28 @@ public sealed partial class Assert private const string WrongExceptionThrown = "Threw exception {2}, but exception {1} was expected. {0}\r\nException Message: {3}"; private const string NoExceptionThrown = "No exception thrown. {1} exception was expected. {0}"; - private const string AreSameGivenValues = "Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {0}"; private const string StringContainsFailMsg = "{2} does not contains {1}. {0}"; private const string StringDoesNotContainsFailMsg = "{2} should not contain {1}. {0}"; private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; + /// + /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. + /// + /// The first object to compare. This is the value the test expects not to match actual. + /// The second object to compare. This is the value produced by the code under test. + /// The message to include in the exception when is the same as . The message is shown in test results. + /// Thrown if refers to the same object as . + public static void AreNotSame(object notExpected, object actual, [CallerArgumentExpression(nameof(actual))] string message = "") + { + if (!ReferenceEquals(notExpected, actual)) + { + return; + } + + HandleFail("Assert.AreNotSame", ReplaceNulls(message)); + } + /// /// Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object. /// @@ -263,23 +279,6 @@ public static void StartsWith( - /// - /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. - /// - /// The first object to compare. This is the value the test expects not to match actual. - /// The second object to compare. This is the value produced by the code under test. - /// The message to include in the exception when is the same as . The message is shown in test results. - /// Thrown if refers to the same object as . - public static void AreNotSame( - object notExpected, - object actual, - string message = "") - { - if (notExpected == actual) - { - HandleFail("Assert.AreNotSame", message); - } - } /// /// Tests whether the specified object is null and throws an exception if it is not. From e477e97e75362b06e25e3075825e58681be8055f Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:40:31 -0700 Subject: [PATCH 12/23] Improve IsNull/IsNotNull --- source/TestFramework/Assert.cs | 58 ++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index cc39cce..e429b0e 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -134,6 +134,37 @@ public static void IsNotInstanceOfType(object value, Type notExpected, [CallerAr HandleFail("Assert.IsNotInstanceOfType", $"Wrong type:<{notExpected}>. Actual type:<{value.GetType()}>. {ReplaceNulls(message)}"); } + /// + /// Tests whether the specified object is non-null and throws an exception if it is null. + /// + /// The object the test expects not to be null. + /// The message to include in the exception when value is null. The message is shown in test results. + /// Thrown if value is null. + public static void IsNotNull([NotNull] object value, [CallerArgumentExpression(nameof(value))] string message = "") + { + if (value is not null) + { + } + + HandleFail("Assert.IsNotNull", message); + } + + /// + /// Tests whether the specified object is null and throws an exception if it is not. + /// + /// The object the test expects to be null. + /// The message to include in the exception when value is not null. The message is shown in test results. + /// Thrown if value is not null. + public static void IsNull(object value, [CallerArgumentExpression(nameof(value))] string message = "") + { + if (value is null) + { + return; + } + + HandleFail("Assert.IsNull", message); + } + /// /// Tests whether the specified condition is true and throws an exception if the condition is false. /// @@ -280,33 +311,6 @@ public static void StartsWith( - /// - /// Tests whether the specified object is null and throws an exception if it is not. - /// - /// The object the test expects to be null. - /// The message to include in the exception when value is not null. The message is shown in test results. - /// Thrown if value is not null. - public static void IsNull(object value, [CallerArgumentExpression(nameof(value))] string message = "") - { - if (value is not null) - { - HandleFail("Assert.IsNull", message); - } - } - - /// - /// Tests whether the specified object is non-null and throws an exception if it is null. - /// - /// The object the test expects not to be null. - /// The message to include in the exception when value is null. The message is shown in test results. - /// Thrown if value is null. - public static void IsNotNull([NotNull] object value, [CallerArgumentExpression(nameof(value))] string message = "") - { - if (value is null) - { - HandleFail("Assert.IsNotNull", message); - } - } /// /// Tests whether the code specified by delegate action throws exact given exception From 514d1ba9e02061938573b8bcfde5344d7123b6ee Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:44:40 -0700 Subject: [PATCH 13/23] Improve ReplaceNulls --- source/TestFramework/Assert.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index e429b0e..313e12c 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -379,11 +379,18 @@ internal static string ReplaceNulls(object input) return NullAsString; } - string text = input.ToString(); - if (text == null) + string text = null; + + try { - return ObjectAsString; + text = input.ToString(); } + catch (NotImplementedException) + { + // Move along + } + + text ??= ObjectAsString; return ReplaceNullChars(text); } From 246f5553c18c2db5e33d02b1b6752139b79e4934 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:52:10 -0700 Subject: [PATCH 14/23] Improve ThrowsException --- source/TestFramework/Assert.cs | 94 ++++++++++++++-------------------- 1 file changed, 39 insertions(+), 55 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 313e12c..b3b4f98 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -20,8 +20,6 @@ public sealed partial class Assert private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; - private const string WrongExceptionThrown = "Threw exception {2}, but exception {1} was expected. {0}\r\nException Message: {3}"; - private const string NoExceptionThrown = "No exception thrown. {1} exception was expected. {0}"; private const string StringContainsFailMsg = "{2} does not contains {1}. {0}"; private const string StringDoesNotContainsFailMsg = "{2} should not contain {1}. {0}"; private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; @@ -185,6 +183,45 @@ public static void SkipTest(string message = null) throw new SkipTestException(message); } + /// + /// Tests whether the code specified by delegate action throws exact given exception + /// of type (and not of derived type) and throws if code + /// does not throw exception or throws exception of type other than . + /// + /// Type of exception expected to be thrown. + /// Delegate to code to be tested and which is expected to throw exception. + /// The message to include in the exception when action does not throw exception of type . + /// Thrown if action does not throw exception of type . + /// Thrown if is . + public static void ThrowsException(Type exception, Action action, [CallerArgumentExpression(nameof(action))] string message = "") + { + EnsureParameterIsNotNull(action, "Assert.ThrowsException"); + EnsureParameterIsNotNull(exception, "Assert.ThrowsException"); + + string empty = string.Empty; + + if (action == null) + { + throw new ArgumentNullException(); + } + + try + { + action.Invoke(); + } + catch (Exception ex) + { + if (exception == ex.GetType()) + { + return; + } + + HandleFail("Assert.ThrowsException", $"Threw exception {ex.GetType().Name}, but exception {exception.Name} was expected. {ReplaceNulls(message)}\r\nException Message: {ex.Message}"); + } + + HandleFail("Assert.ThrowsException", $"No exception thrown. {exception.Name} exception was expected. {ReplaceNulls(message)}"); + } + #region string /// @@ -306,59 +343,6 @@ public static void StartsWith( #endregion - #region types, objects - - - - - - /// - /// Tests whether the code specified by delegate action throws exact given exception - /// of type (and not of derived type) and throws if code - /// does not throw exception or throws exception of type other than . - /// - /// Type of exception expected to be thrown. - /// Delegate to code to be tested and which is expected to throw exception. - /// The message to include in the exception when action does not throw exception of type . - /// Thrown if action does not throw exception of type . - /// Thrown if is . - public static void ThrowsException( - Type exceptionType, - Action action, - string message = "") - { - string empty = string.Empty; - - if (action == null) - { - throw new ArgumentNullException(); - } - - try - { - action.Invoke(); - } - catch (Exception ex) - { - if (ex.GetType() == exceptionType) - { - return; - } - - empty = string.Format(WrongExceptionThrown, ReplaceNulls(message), exceptionType.Name, ex.GetType().Name, ex.Message); - HandleFail("Assert.ThrowsException", empty); - } - - empty = string.Format(NoExceptionThrown, new object[2] - { - ReplaceNulls(message), - exceptionType.Name - }); - - HandleFail("Assert.ThrowsException", empty); - } - #endregion - [DoesNotReturn] internal static void HandleFail(string assertion, string message) { From 59490890ecfaa28f05834c6165f66e38f9dacb29 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:54:20 -0700 Subject: [PATCH 15/23] Improve HandleFail --- source/TestFramework/Assert.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index b3b4f98..0aafd95 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -16,7 +16,6 @@ namespace nanoFramework.TestFramework /// public sealed partial class Assert { - private const string AssertionFailed = "{0} failed. {1}"; private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; @@ -346,14 +345,14 @@ public static void StartsWith( [DoesNotReturn] internal static void HandleFail(string assertion, string message) { - var text = string.Empty; + var safeMessage = string.Empty; if (!string.IsNullOrEmpty(message)) { - text = ReplaceNulls(message); + safeMessage = ReplaceNulls(message); } - - throw new AssertFailedException(string.Format(AssertionFailed, assertion, text)); + + throw new AssertFailedException($"{assertion} failed. {safeMessage}"); } internal static string ReplaceNulls(object input) From a6878955c02550f00968077e1fb52ed83edd7ba8 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 12:56:22 -0700 Subject: [PATCH 16/23] Forgot to remove this --- source/TestFramework/Assert.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 0aafd95..1e792cb 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -197,13 +197,6 @@ public static void ThrowsException(Type exception, Action action, [CallerArgumen EnsureParameterIsNotNull(action, "Assert.ThrowsException"); EnsureParameterIsNotNull(exception, "Assert.ThrowsException"); - string empty = string.Empty; - - if (action == null) - { - throw new ArgumentNullException(); - } - try { action.Invoke(); From 6ad04ccae45cb861f7f288799d3e25d1160dbad9 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 13:01:32 -0700 Subject: [PATCH 17/23] Improve Contains --- source/TestFramework/Assert.cs | 48 ++++++++++++++-------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 1e792cb..0da33cb 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -19,7 +19,6 @@ public sealed partial class Assert private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; - private const string StringContainsFailMsg = "{2} does not contains {1}. {0}"; private const string StringDoesNotContainsFailMsg = "{2} should not contain {1}. {0}"; private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; @@ -64,6 +63,26 @@ public static void AreSame(object expected, object actual, [CallerArgumentExpres HandleFail("Assert.AreSame", ReplaceNulls(message)); } + /// + /// Tests whether a string contains another string. + /// + /// The string that is expected to be found on the string. + /// The string to check for the string. + /// The message to include in the exception when the string is not contained in the string. The message is shown in test results. + /// Thrown if the string contains the string. + public static void Contains(string expected, string value, [CallerArgumentExpression(nameof(value))] string message = "") + { + EnsureParameterIsNotNull(expected, "Assert.Contains"); + EnsureParameterIsNotNull(value, "Assert.Contains"); + + if (value.Contains(expected)) + { + return; + } + + HandleFail("Assert.Contains", $"'{value}' does not contain '{expected}'. {ReplaceNulls(message)}"); + } + internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) { if (value is not null) @@ -216,33 +235,6 @@ public static void ThrowsException(Type exception, Action action, [CallerArgumen #region string - /// - /// Tests whether a string contains another string. - /// - /// The string that is expected to be found on the string. - /// The string to check for the string. - /// The message to include in the exception when the string is not contained in the string. The message is shown in test results. - /// Thrown if the string contains the string. - public static void Contains( - string expected, - string other, - string message = "") - { - EnsureParameterIsNotNull(expected, "Assert.Contains"); - EnsureParameterIsNotNull(other, "Assert.Contains"); - - if (other.IndexOf(expected) < 0) - { - string message2 = string.Format(StringContainsFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.Contains", message2); - } - } /// /// Tests whether a string doesn't contain another string. From 9aed8f62561b9d25b659170bee02a2efa5e1b654 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 13:05:00 -0700 Subject: [PATCH 18/23] Improve DoesNotContains --- source/TestFramework/Assert.cs | 48 ++++++++++++++-------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 0da33cb..6f18ae8 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -19,7 +19,6 @@ public sealed partial class Assert private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; - private const string StringDoesNotContainsFailMsg = "{2} should not contain {1}. {0}"; private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; @@ -83,6 +82,26 @@ public static void Contains(string expected, string value, [CallerArgumentExpres HandleFail("Assert.Contains", $"'{value}' does not contain '{expected}'. {ReplaceNulls(message)}"); } + /// + /// Tests whether a string doesn't contain another string. + /// + /// The string that is not expected to be found on the string. + /// The string to check for the string. + /// The message to include in the exception when the string is contained in the string. The message is shown in test results. + /// Thrown if the string does not contain the string. + public static void DoesNotContains(string notExpected, string value, [CallerArgumentExpression(nameof(value))] string message = "") + { + EnsureParameterIsNotNull(notExpected, "Assert.DoesNotContains"); + EnsureParameterIsNotNull(value, "Assert.DoesNotContains"); + + if (!value.Contains(notExpected)) + { + return; + } + + HandleFail("Assert.DoesNotContains", $"'{value}' should not contain '{notExpected}'. {ReplaceNulls(message)}"); + } + internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) { if (value is not null) @@ -236,33 +255,6 @@ public static void ThrowsException(Type exception, Action action, [CallerArgumen #region string - /// - /// Tests whether a string doesn't contain another string. - /// - /// The string that is not expected to be found on the string. - /// The string to check for the string. - /// The message to include in the exception when the string is contained in the string. The message is shown in test results. - /// Thrown if the string does not contain the string. - public static void DoesNotContains( - string expected, - string other, - string message = "") - { - EnsureParameterIsNotNull(expected, "Assert.Contains"); - EnsureParameterIsNotNull(other, "Assert.Contains"); - - if (other.IndexOf(expected) >= 0) - { - string message2 = string.Format(StringDoesNotContainsFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.DoesNotContains", message2); - } - } /// /// Tests whether a string ends with another string. From 0e9d937c711c380da49c98555c6f1df2f64842d0 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 13:19:31 -0700 Subject: [PATCH 19/23] Improved EndWith --- source/TestFramework/Assert.cs | 51 +++++++++++++--------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 6f18ae8..c5744de 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -19,7 +19,6 @@ public sealed partial class Assert private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; - private const string StringDoesNotEndWithFailMsg = "{2} does not end with {1}. {0}"; private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; /// @@ -102,6 +101,26 @@ public static void DoesNotContains(string notExpected, string value, [CallerArgu HandleFail("Assert.DoesNotContains", $"'{value}' should not contain '{notExpected}'. {ReplaceNulls(message)}"); } + /// + /// Tests whether a string ends with another string. + /// + /// The string that is expected to be found at the end of the string. + /// The string to check for the string. + /// The message to include in the exception when the string is not found at the end of the string. The message is shown in test results. + /// Thrown if the string does not end with the string. + public static void EndsWith(string expected, string value, [CallerArgumentExpression(nameof(value))] string message = "") + { + EnsureParameterIsNotNull(expected, "Assert.EndsWith"); + EnsureParameterIsNotNull(value, "Assert.EndsWith"); + + if (value.EndsWith(expected)) + { + return; + } + + HandleFail("Assert.EndsWith", $"{value} does not end with {expected}. {ReplaceNulls(message)}"); + } + internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) { if (value is not null) @@ -256,36 +275,6 @@ public static void ThrowsException(Type exception, Action action, [CallerArgumen - /// - /// Tests whether a string ends with another string. - /// - /// The string that is expected to be found at the end of the string. - /// The string to check for the string. - /// The message to include in the exception when the string is not found at the end of the string. The message is shown in test results. - /// Thrown if the string does not end with the string. - public static void EndsWith( - string expected, - string other, - string message = "") - { - EnsureParameterIsNotNull(expected, "Assert.Contains"); - EnsureParameterIsNotNull(other, "Assert.Contains"); - - // We have to take the last index as the text can contains multiple times the same word - if (other.LastIndexOf(expected) == other.Length - expected.Length) - { - return; - } - - string message2 = string.Format(StringDoesNotEndWithFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.EndsWith", message2); - } /// /// Tests whether a string starts with another string. From 1c5fa320acb5fd5e313bec2bd6dcd59968124aef Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 13:22:19 -0700 Subject: [PATCH 20/23] Improve StartsWith --- source/TestFramework/Assert.cs | 61 ++++++++++++---------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index c5744de..13da2a5 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -19,8 +19,6 @@ public sealed partial class Assert private const string ObjectAsString = "(object)"; private const string NullAsString = "(null)"; - private const string StringDoesNotStartWithFailMsg = "{2} does not start with {1}. {0}"; - /// /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. /// @@ -118,7 +116,7 @@ public static void EndsWith(string expected, string value, [CallerArgumentExpres return; } - HandleFail("Assert.EndsWith", $"{value} does not end with {expected}. {ReplaceNulls(message)}"); + HandleFail("Assert.EndsWith", $"'{value}' does not end with '{expected}'. {ReplaceNulls(message)}"); } internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) @@ -239,6 +237,26 @@ public static void SkipTest(string message = null) throw new SkipTestException(message); } + /// + /// Tests whether a string starts with another string. + /// + /// The string that is expected to be found at the beginning of the string. + /// The string to check for the string. + /// The message to include in the exception when the string is not found at the beginning of the string. The message is shown in test results. + /// Thrown if the string does not start with the string. + public static void StartsWith(string expected, string value, [CallerArgumentExpression(nameof(value))] string message = "") + { + EnsureParameterIsNotNull(expected, "Assert.StartsWith"); + EnsureParameterIsNotNull(value, "Assert.StartsWith"); + + if (value.StartsWith(expected)) + { + return; + } + + HandleFail("Assert.StartsWith", $"'{value}' does not start with '{expected}'. {ReplaceNulls(message)}"); + } + /// /// Tests whether the code specified by delegate action throws exact given exception /// of type (and not of derived type) and throws if code @@ -271,43 +289,6 @@ public static void ThrowsException(Type exception, Action action, [CallerArgumen HandleFail("Assert.ThrowsException", $"No exception thrown. {exception.Name} exception was expected. {ReplaceNulls(message)}"); } - #region string - - - - - /// - /// Tests whether a string starts with another string. - /// - /// The string that is expected to be found at the beginning of the string. - /// The string to check for the string. - /// The message to include in the exception when the string is not found at the beginning of the string. The message is shown in test results. - /// Thrown if the string does not start with the string. - public static void StartsWith( - string expected, - string other, - string message = "") - { - EnsureParameterIsNotNull(expected, "Assert.Contains"); - EnsureParameterIsNotNull(other, "Assert.Contains"); - - if (other.IndexOf(expected) == 0) - { - return; - } - - string message2 = string.Format(StringDoesNotStartWithFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.StartsWith", message2); - } - - #endregion - [DoesNotReturn] internal static void HandleFail(string assertion, string message) { From 5180315d0c9063ed37046ec034f549347f3bd88b Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 13:23:23 -0700 Subject: [PATCH 21/23] Improve ReplaceNullChars --- source/TestFramework/Assert.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 13da2a5..3c4e423 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -332,9 +332,9 @@ internal static string ReplaceNullChars(string input) return input; } - string replacedString = ""; + var replacedString = string.Empty; - for (int i = 0; i < input.Length; i++) + for (var i = 0; i < input.Length; i++) { if (input[i] == '\0') { @@ -346,7 +346,7 @@ internal static string ReplaceNullChars(string input) } } - return replacedString.ToString(); + return replacedString; } } } From 28b5a5bfe3c5e2ad28f1eb52cd4a79a50c18f892 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 13:44:50 -0700 Subject: [PATCH 22/23] Missed the return in IsNotNull --- source/TestFramework/Assert.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/source/TestFramework/Assert.cs b/source/TestFramework/Assert.cs index 3c4e423..25fada5 100644 --- a/source/TestFramework/Assert.cs +++ b/source/TestFramework/Assert.cs @@ -196,6 +196,7 @@ public static void IsNotNull([NotNull] object value, [CallerArgumentExpression(n { if (value is not null) { + return; } HandleFail("Assert.IsNotNull", message); From 436965c57b0d5c11fabc23ff517bec349b576ef8 Mon Sep 17 00:00:00 2001 From: Cory Charlton Date: Fri, 17 May 2024 14:47:06 -0700 Subject: [PATCH 23/23] Updated unit tests --- .../Mock/MockObject.cs | 6 + .../NFUnitTestByReference.nfproj | 1 + poc/TestOfTestFrameworkByReference/Test.cs | 324 +++++++++++------- 3 files changed, 208 insertions(+), 123 deletions(-) create mode 100644 poc/TestOfTestFrameworkByReference/Mock/MockObject.cs diff --git a/poc/TestOfTestFrameworkByReference/Mock/MockObject.cs b/poc/TestOfTestFrameworkByReference/Mock/MockObject.cs new file mode 100644 index 0000000..82ff1e1 --- /dev/null +++ b/poc/TestOfTestFrameworkByReference/Mock/MockObject.cs @@ -0,0 +1,6 @@ +namespace NFUnitTest.Mock +{ + internal class MockObject + { + } +} diff --git a/poc/TestOfTestFrameworkByReference/NFUnitTestByReference.nfproj b/poc/TestOfTestFrameworkByReference/NFUnitTestByReference.nfproj index ca4f865..9e2ef89 100644 --- a/poc/TestOfTestFrameworkByReference/NFUnitTestByReference.nfproj +++ b/poc/TestOfTestFrameworkByReference/NFUnitTestByReference.nfproj @@ -28,6 +28,7 @@ + diff --git a/poc/TestOfTestFrameworkByReference/Test.cs b/poc/TestOfTestFrameworkByReference/Test.cs index 37c8240..8927799 100644 --- a/poc/TestOfTestFrameworkByReference/Test.cs +++ b/poc/TestOfTestFrameworkByReference/Test.cs @@ -5,189 +5,267 @@ // using System; -using System.Diagnostics; using System.Threading; +using nanoFramework.TestFramework; +using NFUnitTest.Mock; +using TestFrameworkShared; -namespace nanoFramework.TestFramework.Test +namespace NFUnitTest { [TestClass] public class TestOfTest { - [TestMethod] - public void TestRaisesException() + [Setup] + public void RunSetup() { - Debug.WriteLine("Test will raise exception"); - Assert.Throws(typeof(Exception), ThrowMe); - Assert.Throws(typeof(ArgumentOutOfRangeException), () => - { - Debug.WriteLine("To see another way of doing this"); - // This should throw an ArgumentException - Thread.Sleep(-2); - }); - try - { - Assert.Throws(typeof(Exception), () => { Debug.WriteLine("Nothing will be thrown"); }); - } - catch (Exception) - { - Debug.WriteLine("Exception raised, perfect"); - } + Console.WriteLine("Methods with [Setup] will run before tests."); } - private void ThrowMe() + [Cleanup] + public void Cleanup() { - throw new Exception("Test failed and it's a shame"); + Console.WriteLine("Methods with [Cleanup] will run after tests."); } [TestMethod] - public void TestCheckAllEqual() + public void TestAreEqual() { - Debug.WriteLine("Test will check that all the Equal are actually equal"); + Console.WriteLine("Test will check that all the AreEqual are actually equal and that AreNotEqual fails"); // Arrange - byte bytea = 42; byte byteb = 42; - char chara = (char)42; char charb = (char)42; - sbyte sbytea = 42; sbyte sbyteb = 42; - int inta = 42; int intb = 42; - uint uinta = 42; uint uintb = 42; - long longa = 42; long longb = 42; - ulong ulonga = 42; ulong ulongb = 42; - bool boola = true; bool boolb = true; - short shorta = 42; short shortb = 42; - ushort ushorta = 42; ushort ushortb = 42; - float floata = 42; float floatb = 42; - int[] intArraya = new int[5] { 1, 2, 3, 4, 5 }; - int[] intArrayb = new int[5] { 1, 2, 3, 4, 5 }; - object obja = new object(); object objb = obja; - string stra = "42"; string strb = "42"; - byte[] arrayempty = new byte[0]; + const bool boolA = true; const bool boolB = true; + const byte byteA = 42; const byte byteB = 42; + const char charA = (char)42; const char charB = (char)42; + var dateTimeA = new DateTime(2024, 4, 20); + var dateTimeB = new DateTime(2024, 4, 20); + const float floatA = 42; const float floatB = 42; + const int intA = 42; const int intB = 42; + var intArrayA = new[] { 1, 2, 3, 4, 5 }; + var intArrayB = new[] { 1, 2, 3, 4, 5 }; + const long longA = 42; const long longB = 42; + var objA = new object(); var objB = objA; + const sbyte sbyteA = 42; const sbyte sbyteB = 42; + const short shortA = 42; const short shortB = 42; + const string stringA = "42"; const string stringB = "42"; + const uint uintA = 42; const uint uintB = 42; + const ulong ulongA = 42; const ulong ulongB = 42; + const ushort ushortA = 42; const ushort ushortB = 42; // Assert - Assert.IsTrue(boola); - Assert.AreEqual(bytea, byteb); - Assert.AreEqual(chara, charb); - Assert.AreEqual(sbytea, sbyteb); - Assert.AreEqual(inta, intb); - Assert.AreEqual(uinta, uintb); - Assert.AreEqual(longa, longb); - Assert.AreEqual(ulonga, ulongb); - Assert.AreEqual(boola, boolb); - Assert.AreEqual(shorta, shortb); - Assert.AreEqual(ushorta, ushortb); - Assert.AreEqual(floata, floatb); - CollectionAssert.AreEqual(intArraya, intArrayb); - Assert.AreEqual(stra, strb); - Assert.AreSame(obja, objb); - CollectionAssert.Empty(arrayempty); + Assert.AreEqual(boolA, boolB); + Assert.AreEqual(byteA, byteB); + Assert.AreEqual(charA, charB); + Assert.AreEqual(dateTimeA, dateTimeB); + Assert.AreEqual(floatA, floatB); + Assert.AreEqual(intA, intB); + Assert.AreEqual(longA, longB); + Assert.AreEqual(objA, objB); + Assert.AreEqual(sbyteA, sbyteB); + Assert.AreEqual(shortA, shortB); + Assert.AreEqual(stringA, stringB); + Assert.AreEqual(uintA, uintB); + Assert.AreEqual(ulongA, ulongB); + Assert.AreEqual(ushortA, ushortB); + Assert.AreSame(objA, objB); + CollectionAssert.AreEqual(intArrayA, intArrayB); + + CatchAssertException(() => Assert.AreNotEqual(boolA, boolB)); + CatchAssertException(() => Assert.AreNotEqual(byteA, byteB)); + CatchAssertException(() => Assert.AreNotEqual(charA, charB)); + CatchAssertException(() => Assert.AreNotEqual(dateTimeA, dateTimeB)); + CatchAssertException(() => Assert.AreNotEqual(floatA, floatB)); + CatchAssertException(() => Assert.AreNotEqual(intA, intB)); + CatchAssertException(() => Assert.AreNotEqual(longA, longB)); + CatchAssertException(() => Assert.AreNotEqual(objA, objB)); + CatchAssertException(() => Assert.AreNotEqual(sbyteA, sbyteB)); + CatchAssertException(() => Assert.AreNotEqual(shortA, shortB)); + CatchAssertException(() => Assert.AreNotEqual(stringA, stringB)); + CatchAssertException(() => Assert.AreNotEqual(uintA, uintB)); + CatchAssertException(() => Assert.AreNotEqual(ulongA, ulongB)); + CatchAssertException(() => Assert.AreNotEqual(ushortA, ushortB)); + CatchAssertException(() => Assert.AreNotSame(objA, objB)); + CatchAssertException(() => CollectionAssert.AreNotEqual(intArrayA, intArrayB)); } [TestMethod] - public void TestCheckAllNotEqual() + public void TestAreNotEqual() { - Debug.WriteLine("Test will check that all the NotEqual are actually equal"); + Console.WriteLine("Test will check that all the AreNotEqual are actually not equal and AreEqual fails"); // Arrange - byte bytea = 42; byte byteb = 43; - char chara = (char)42; char charb = (char)43; - sbyte sbytea = 42; sbyte sbyteb = 43; - int inta = 42; int intb = 43; - uint uinta = 42; uint uintb = 43; - long longa = 42; long longb = 43; - ulong ulonga = 42; ulong ulongb = 43; - bool boola = true; bool boolb = false; - short shorta = 42; short shortb = 43; - ushort ushorta = 42; ushort ushortb = 43; - float floata = 42; float floatb = 43; - int[] intArraya = new int[5] { 1, 2, 3, 4, 5 }; - int[] intArrayb = new int[5] { 1, 2, 3, 4, 6 }; - int[] intArraybis = new int[4] { 1, 2, 3, 4 }; - int[] intArrayter = null; - object obja = new object(); object objb = new object(); - string stra = "42"; string strb = "43"; + const bool boolA = true; const bool boolB = false; + const byte byteA = 42; const byte byteB = 43; + const char charA = (char)42; const char charB = (char)43; + var dateTimeA = new DateTime(2024, 4, 20); + var dateTimeB = new DateTime(2024, 4, 21); + const float floatA = 42; const float floatB = 43; + const int intA = 42; const int intB = 43; + var intArrayA = new[] { 1, 2, 3, 4, 5 }; + var intArrayB = new[] { 5, 4, 3, 2, 1 }; + const long longA = 42; const long longB = 43; + var objA = new object(); var objB = new object(); + const sbyte sbyteA = 42; const sbyte sbyteB = 43; + const short shortA = 42; const short shortB = 43; + const string stringA = "42"; const string stringB = "43"; + const uint uintA = 42; const uint uintB = 43; + const ulong ulongA = 42; const ulong ulongB = 43; + const ushort ushortA = 42; const ushort ushortB = 43; // Assert - Assert.IsFalse(boolb); - Assert.AreNotEqual(bytea, byteb); - Assert.AreNotEqual(chara, charb); - Assert.AreNotEqual(sbytea, sbyteb); - Assert.AreNotEqual(inta, intb); - Assert.AreNotEqual(uinta, uintb); - Assert.AreNotEqual(longa, longb); - Assert.AreNotEqual(ulonga, ulongb); - Assert.AreNotEqual(boola, boolb); - Assert.AreNotEqual(shorta, shortb); - Assert.AreNotEqual(ushorta, ushortb); - Assert.AreNotEqual(floata, floatb); - Assert.AreNotEqual(intArraya, intArrayb); - Assert.AreNotEqual(intArraya, intArraybis); - Assert.AreNotEqual(intArraya, intArrayter); - Assert.AreNotEqual(stra, strb); - Assert.AreNotSame(obja, objb); - CollectionAssert.NotEmpty(intArraya); + Assert.AreNotEqual(boolA, boolB); + Assert.AreNotEqual(byteA, byteB); + Assert.AreNotEqual(charA, charB); + Assert.AreNotEqual(dateTimeA, dateTimeB); + Assert.AreNotEqual(floatA, floatB); + Assert.AreNotEqual(intA, intB); + Assert.AreNotEqual(longA, longB); + Assert.AreNotEqual(objA, objB); + Assert.AreNotEqual(sbyteA, sbyteB); + Assert.AreNotEqual(shortA, shortB); + Assert.AreNotEqual(stringA, stringB); + Assert.AreNotEqual(uintA, uintB); + Assert.AreNotEqual(ulongA, ulongB); + Assert.AreNotEqual(ushortA, ushortB); + Assert.AreNotSame(objA, objB); + CollectionAssert.AreNotEqual(intArrayA, intArrayB); + + CatchAssertException(() => Assert.AreEqual(boolA, boolB)); + CatchAssertException(() => Assert.AreEqual(byteA, byteB)); + CatchAssertException(() => Assert.AreEqual(charA, charB)); + CatchAssertException(() => Assert.AreEqual(dateTimeA, dateTimeB)); + CatchAssertException(() => Assert.AreEqual(floatA, floatB)); + CatchAssertException(() => Assert.AreEqual(intA, intB)); + CatchAssertException(() => Assert.AreEqual(longA, longB)); + CatchAssertException(() => Assert.AreEqual(objA, objB)); + CatchAssertException(() => Assert.AreEqual(sbyteA, sbyteB)); + CatchAssertException(() => Assert.AreEqual(shortA, shortB)); + CatchAssertException(() => Assert.AreEqual(stringA, stringB)); + CatchAssertException(() => Assert.AreEqual(uintA, uintB)); + CatchAssertException(() => Assert.AreEqual(ulongA, ulongB)); + CatchAssertException(() => Assert.AreEqual(ushortA, ushortB)); + CatchAssertException(() => Assert.AreSame(objA, objB)); + CatchAssertException(() => CollectionAssert.AreEqual(intArrayA, intArrayB)); + } + + [TestMethod] + public void TestInstanceOfType() + { + var mockObject = new MockObject(); + var notMockObject = new object(); + + Assert.IsInstanceOfType(mockObject, typeof(MockObject)); + Assert.IsNotInstanceOfType(notMockObject, typeof(MockObject)); + + CatchAssertException(() => Assert.IsInstanceOfType(notMockObject, typeof(MockObject))); + CatchAssertException(() => Assert.IsNotInstanceOfType(mockObject, typeof(MockObject))); } [TestMethod] - public void TestNullEmpty() + public void TestNullNotNull() { - Debug.WriteLine("Test null, not null, types"); + Console.WriteLine("Test null, not null"); // Arrange - object objnull = null; - object objnotnull = new object(); - Type typea = typeof(int); - Type typeb = typeof(int); - Type typec = typeof(long); + var nullObject = (object) null; + var notNullObject = new object(); // Assert - Assert.IsNull(objnull); - Assert.IsNotNull(objnotnull); - Assert.AreEqual(typea, typeb); - Assert.AreNotEqual(typea, typec); + Assert.IsNull(nullObject); + Assert.IsNotNull(notNullObject); + + CatchAssertException(() => Assert.IsNull(notNullObject)); + CatchAssertException(() => Assert.IsNotNull(nullObject)); } [TestMethod] public void TestStringComparison() { - Debug.WriteLine("Test string, Contains, EndsWith, StartWith"); + Console.WriteLine("Test string, Contains, EndsWith, StartWith"); // Arrange - string tocontains = "this text contains and end with contains"; - string startcontains = "contains start this text"; - string contains = "contains"; - string doesnotcontains = "this is totally something else"; - string empty = string.Empty; + const string contains = "contains"; + const string endsWithContains = "this text contains and end with contains"; + const string startsWithContains = "contains start this text"; + const string doesNotContain = "this is totally something else"; + var empty = string.Empty; // Assert - Assert.Contains(contains, tocontains); - Assert.DoesNotContains(contains, doesnotcontains); + Assert.Contains(contains, endsWithContains); + Assert.EndsWith(contains, endsWithContains); + Assert.StartsWith(contains, startsWithContains); + Assert.DoesNotContains(contains, doesNotContain); Assert.DoesNotContains(contains, empty); - Assert.StartsWith(contains, startcontains); - Assert.EndsWith(contains, tocontains); + + CatchAssertException(() => Assert.Contains(contains, doesNotContain)); + CatchAssertException(() => Assert.EndsWith(contains, doesNotContain)); + CatchAssertException(() => Assert.StartsWith(contains, doesNotContain)); + CatchAssertException(() => Assert.DoesNotContains(contains, startsWithContains)); } - [Setup] - public void RunSetup() + [TestMethod] + public void TestThrowsException() { - Debug.WriteLine("Setup"); + Console.WriteLine("Test will raise exception"); + + Assert.ThrowsException(typeof(Exception), ThrowsException); + Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => + { + Console.WriteLine("To see another way of doing this"); + // This should throw an ArgumentException + Thread.Sleep(-2); + }); + + try + { + Assert.ThrowsException(typeof(Exception), () => { Console.WriteLine("Nothing will be thrown"); }); + } + catch (AssertFailedException) + { + Console.WriteLine("AssertFailedException raised because no exception was thrown, perfect"); + } + + try + { + Assert.ThrowsException(typeof(ArgumentNullException), ThrowsException); + } + catch (AssertFailedException) + { + Console.WriteLine("AssertFailedException raised because wrong exception was thrown, perfect"); + } + + } + + [TestMethod] + public void TestTrueFalse() + { + Assert.IsTrue(true); + Assert.IsFalse(false); + + CatchAssertException(() => Assert.IsTrue(false)); + CatchAssertException(() => Assert.IsFalse(true)); + } + + private static void CatchAssertException(Action action) + { + Assert.ThrowsException(typeof(AssertFailedException), action); } public void Nothing() { - Debug.WriteLine("Nothing and should not be called"); + Console.WriteLine("Nothing and should not be called"); } - [Cleanup] - public void Cleanup() + private static void ThrowsException() { - Debug.WriteLine("Cleanup"); + throw new Exception("Test failed and it's a shame"); } } - public class SomthingElse + public class SomethingElse { public void NothingReally() { - Debug.WriteLine("Test failed: This would never get through"); + Console.WriteLine("Only classes marked with [TestClass] will run tests."); } } }