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."); } } } diff --git a/source/TestFramework/Assert.AreEqual.cs b/source/TestFramework/Assert.AreEqual.cs new file mode 100644 index 0000000..7c1a6ae --- /dev/null +++ b/source/TestFramework/Assert.AreEqual.cs @@ -0,0 +1,244 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(actual))] string message = "") + { + if (!Equals(expected, actual)) + { + HandleAreEqualFail(expected, actual, 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 new file mode 100644 index 0000000..2dba29b --- /dev/null +++ b/source/TestFramework/Assert.AreNotEqual.cs @@ -0,0 +1,242 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using TestFrameworkShared; + +namespace nanoFramework.TestFramework +{ + // 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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(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, [CallerArgumentExpression(nameof(actual))] string message = "") + { + if (Equals(notExpected, actual)) + { + HandleAreNotEqualFail(notExpected, actual, message); + } + } + + [DoesNotReturn] + private static void HandleAreNotEqualFail(object notExpected, object actual, string message) + { + HandleFail("Assert.AreNotEqual", $"Expected any value except:<{ReplaceNulls(notExpected)}>. Actual:<{ReplaceNulls(actual)}>. {(message is null ? string.Empty : ReplaceNulls(message))}"); + } + } +} 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..25fada5 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,1582 +14,193 @@ 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)"; - 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}"; - 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}."; - - #region SkipTest - - public static void SkipTest(string message = "") - { - if (string.IsNullOrEmpty(message)) - { - throw new SkipTestException(); - } - - throw new SkipTestException(message); - } - - #endregion - - #region true/false - - /// - /// 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 . - public static void IsTrue(bool condition, [CallerArgumentExpression(nameof(condition))] string message = "") - { - if (!condition) - { - HandleFail("Assert.IsTrue", 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); - - /// - /// 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 = "") - { - if (condition) - { - 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 - - /// - /// 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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. - /// - /// 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - public static void AreEqual( - DateTime expected, - DateTime 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 . - [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 - - /// - /// 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - 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 . - [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. - /// - /// 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 . - [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 . - 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 . - [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 . - 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 . - [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 . - public static void AreNotEqual( - char notExpected, - char actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } - } - + private const string ObjectAsString = "(object)"; + private const string NullAsString = "(null)"; + /// - /// 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. + /// Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object. /// - /// 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 = "") + /// 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 (object.Equals(notExpected, actual)) + if (!ReferenceEquals(notExpected, actual)) { - HandleAreNotEqualFail( - notExpected, - actual, - message); + return; } - } - - /// - /// 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 . - public static void AreNotEqual( - double notExpected, - double actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } + HandleFail("Assert.AreNotSame", ReplaceNulls(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. + /// 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 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 = "") + /// 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 (object.Equals(notExpected, actual)) + if (ReferenceEquals(expected, actual)) { - HandleAreNotEqualFail( - notExpected, - actual, - message); + return; } - } - /// - /// 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 . - public static void AreNotEqual( - string notExpected, - string actual, - string message = "") - { - if (string.Compare(notExpected, actual) == 0) + if (expected is ValueType || actual is ValueType) { - HandleAreNotEqualFail( - notExpected, - actual, - message); + HandleFail("Assert.AreSame", $"Do not pass value types to AreSame(). Values converted to Object will never be the same. Consider using AreEqual(). {ReplaceNulls(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 . - public static void AreNotEqual( - DateTime notExpected, - DateTime actual, - string message = "") - { - if (object.Equals(notExpected, actual)) - { - HandleAreNotEqualFail( - notExpected, - actual, - message); - } + HandleFail("Assert.AreSame", ReplaceNulls(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 - /// /// 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 = "") + /// 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 = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(expected, "Assert.Contains"); + EnsureParameterIsNotNull(value, "Assert.Contains"); - if (other.IndexOf(expected) < 0) + if (value.Contains(expected)) { - string message2 = string.Format(StringContainsFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.Contains", message2); + return; } + + 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 expected, - string other, - string message = "") + /// 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 = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(notExpected, "Assert.DoesNotContains"); + EnsureParameterIsNotNull(value, "Assert.DoesNotContains"); - if (other.IndexOf(expected) >= 0) + if (!value.Contains(notExpected)) { - string message2 = string.Format(StringDoesNotContainsFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.DoesNotContains", message2); + return; } + + 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 other, - string message = "") + /// 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 = "") { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); + EnsureParameterIsNotNull(expected, "Assert.EndsWith"); + EnsureParameterIsNotNull(value, "Assert.EndsWith"); - // 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) + if (value.EndsWith(expected)) { return; } - string message2 = string.Format(StringDoesNotEndWithFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.EndsWith", message2); + HandleFail("Assert.EndsWith", $"'{value}' does not end with '{expected}'. {ReplaceNulls(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 other, - string message = "") + internal static void EnsureParameterIsNotNull(object value, string assertion, [CallerArgumentExpression(nameof(value))] string parameter = null) { - Assert.CheckParameterNotNull(expected, "Assert.Contains", "expected", string.Empty); - Assert.CheckParameterNotNull(other, "Assert.Contains", "other", string.Empty); - - if (other.IndexOf(expected) == 0) + if (value is not null) { return; } - string message2 = string.Format(StringDoesNotStartWithFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(other) - }); - - HandleFail("Assert.StartsWith", message2); - } - - #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); - } + HandleFail(assertion, $"The parameter '{parameter}' is invalid. The value cannot be null."); } /// - /// Tests whether the specified collection is not empty. + /// Tests whether the specified condition is false and throws an exception if the condition is true. /// - /// 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 = "") + /// 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 = "") { - if (collection.Count == 0) + if (condition) { - HandleFail("Assert.NotEmpty", message); + HandleFail("Assert.IsFalse", message); } } - #endregion region - - #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 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 = "") + /// 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 = "") { - if (expectedType == null || value == null) + EnsureParameterIsNotNull(expected, "Assert.IsInstanceOfType"); + + if (value is not null && expected == value.GetType()) { - HandleFail("Assert.IsInstanceOfType", message); + return; } - if (expectedType != value.GetType()) - { - string message2 = string.Format(IsInstanceOfFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - expectedType, - value.GetType() - }); + // 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", message2); - } + HandleFail("Assert.IsInstanceOfType", $"Expected type:<{expected}>. Actual type:<{ReplaceNulls(actual)}>. {ReplaceNulls(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 . - 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. /// /// 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 = "") + /// 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 = "") { - if ((object)wrongType == null) - { - HandleFail("Assert.IsNotInstanceOfType", message); - } + EnsureParameterIsNotNull(notExpected, "Assert.IsNotInstanceOfType"); - if (value != null) + if (value is null || notExpected != value.GetType()) { - 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); - } + return; } - } - - /// - /// 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. - /// - /// 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); - } + HandleFail("Assert.IsNotInstanceOfType", $"Wrong type:<{notExpected}>. Actual type:<{value.GetType()}>. {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. - /// - /// 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. + /// Tests whether the specified object is non-null and throws an exception if it is null. /// - /// 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 = "") + /// 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 (notExpected == actual) + if (value is not null) { - HandleFail("Assert.AreNotSame", message); + return; } + + HandleFail("Assert.IsNotNull", 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. @@ -1600,65 +210,68 @@ public static void NotSame( /// Thrown if value is not null. public static void IsNull(object value, [CallerArgumentExpression(nameof(value))] string message = "") { - if (value is not null) + if (value is null) { - HandleFail("Assert.IsNull", message); + return; } - } - /// - /// 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); + HandleFail("Assert.IsNull", message); + } /// - /// Tests whether the specified object is non-null and throws an exception if it is null. + /// Tests whether the specified condition is true and throws an exception if the condition is false. /// - /// 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 = "") + /// 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 . + public static void IsTrue(bool condition, [CallerArgumentExpression(nameof(condition))] string message = "") { - if (value is null) + if (!condition) { - HandleFail("Assert.IsNotNull", message); + HandleFail("Assert.IsTrue", message); } } + [DoesNotReturn] + public static void SkipTest(string message = null) + { + throw new SkipTestException(message); + } + /// - /// Tests whether the specified object is non-null and throws an exception if it is null. + /// Tests whether a string starts with another string. /// - /// 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); + /// 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 - /// does not throw exception or throws exception of type other than . + /// 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. + /// 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 . + /// 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 = "") + public static void ThrowsException(Type exception, Action action, [CallerArgumentExpression(nameof(action))] string message = "") { - string empty = string.Empty; - - if (action == null) - { - throw new ArgumentNullException(); - } + EnsureParameterIsNotNull(action, "Assert.ThrowsException"); + EnsureParameterIsNotNull(exception, "Assert.ThrowsException"); try { @@ -1666,69 +279,50 @@ public static void ThrowsException( } catch (Exception ex) { - if (ex.GetType() == exceptionType) + if (exception == ex.GetType()) { return; } - - empty = string.Format(WrongExceptionThrown, ReplaceNulls(message), exceptionType.Name, ex.GetType().Name, ex.Message); - HandleFail("Assert.ThrowsException", empty); + + HandleFail("Assert.ThrowsException", $"Threw exception {ex.GetType().Name}, but exception {exception.Name} was expected. {ReplaceNulls(message)}\r\nException Message: {ex.Message}"); } - empty = string.Format(NoExceptionThrown, new object[2] - { - ReplaceNulls(message), - exceptionType.Name - }); - - HandleFail("Assert.ThrowsException", empty); + HandleFail("Assert.ThrowsException", $"No exception thrown. {exception.Name} exception was 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 - /// 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) + [DoesNotReturn] + internal static void HandleFail(string assertion, string message) { - string text = string.Empty; + var safeMessage = string.Empty; if (!string.IsNullOrEmpty(message)) { - text = ReplaceNulls(message); + safeMessage = ReplaceNulls(message); } - - throw new AssertFailedException(string.Format(AssertionFailed, new object[2] { assertionName, text })); + + throw new AssertFailedException($"{assertion} failed. {safeMessage}"); } internal static string ReplaceNulls(object input) { if (input == null) { - return Common_NullInMessages; + return NullAsString; } - string text = input.ToString(); - if (text == null) + string text = null; + + try + { + text = input.ToString(); + } + catch (NotImplementedException) { - return Common_ObjectString; + // Move along } + text ??= ObjectAsString; + return ReplaceNullChars(text); } @@ -1739,9 +333,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') { @@ -1753,55 +347,7 @@ 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, - string message) - { - string message2 = string.Format(AreNotEqualFailMsg, new object[3] - { - (message == null) ? string.Empty : ReplaceNulls(message), - ReplaceNulls(expected), - ReplaceNulls(actual) - }); - - 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 - })); - } + return replacedString; } } } 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) { diff --git a/source/TestFramework/nanoFramework.TestFramework.nfproj b/source/TestFramework/nanoFramework.TestFramework.nfproj index 732f2bb..daf95d1 100644 --- a/source/TestFramework/nanoFramework.TestFramework.nfproj +++ b/source/TestFramework/nanoFramework.TestFramework.nfproj @@ -31,10 +31,19 @@ + + + Assert.cs + + + Assert.cs + + + Assert.cs + - 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) { } } }