Rework Asserts (#179)
This commit is contained in:
Родитель
100ad51dea
Коммит
6495f582d3
|
@ -8,6 +8,13 @@ Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.TestFramework
|
|||
EndProject
|
||||
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "TestFrameworkShared", "source\TestFrameworkShared\TestFrameworkShared.shproj", "{55F048B5-6739-43C5-A93D-DB61DB8E912F}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{177B2F99-E031-410F-88EF-C0C46289B80C}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
NuGet.Config = NuGet.Config
|
||||
source\package.nuspec = source\package.nuspec
|
||||
version.json = version.json
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,134 @@
|
|||
////
|
||||
// Copyright (c) .NET Foundation and Contributors
|
||||
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
////
|
||||
|
||||
|
||||
using System.Collections;
|
||||
|
||||
namespace nanoFramework.TestFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// A collection of helper classes to test various conditions associated
|
||||
/// with collections within unit tests. If the condition being tested is not
|
||||
/// met, an exception is thrown.
|
||||
/// </summary>
|
||||
public sealed class CollectionAssert
|
||||
{
|
||||
private const string CollectionEqualReason = "{0}({1})";
|
||||
private const string NumberOfElementsDiff = "Different number of elements.";
|
||||
private const string ElementsAtIndexDontMatch = "Element at index {0} do not match.";
|
||||
private const string BothCollectionsSameReference = "Both collection references point to the same collection object. {0}";
|
||||
private const string BothCollectionsSameElements = "Both collection contain same elements.";
|
||||
|
||||
#region collection
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified collection is empty.
|
||||
/// </summary>
|
||||
/// <param name="collection">The collection the test expects to be empty.</param>
|
||||
/// <param name="message">The message to include in the exception when the collection is empty. The message is shown in test results.</param>
|
||||
/// <exception cref="AssertFailedException">Raises an exception if the collection is not empty.</exception>
|
||||
/// <exception cref=""></exception>
|
||||
public static void Empty(ICollection collection, string message = "")
|
||||
{
|
||||
Assert.CheckParameterNotNull(collection, "CollectionAssert.Empty", "collection", string.Empty);
|
||||
|
||||
if (collection.Count != 0)
|
||||
{
|
||||
Assert.HandleFail("CollectionAssert.Empty", message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified collection is not empty.
|
||||
/// </summary>
|
||||
/// <param name="collection">The collection the test expects not to be empty.</param>
|
||||
/// <param name="message">The message to include in the exception when the collection is not empty. The message is shown in test results.</param>
|
||||
/// <exception cref="AssertFailedException">Raises an exception if the collection is not empty.</exception>
|
||||
public static void NotEmpty(ICollection collection, string message = "")
|
||||
{
|
||||
Assert.CheckParameterNotNull(collection, "CollectionAssert.NotEmpty", "collection", string.Empty);
|
||||
|
||||
if (collection.Count == 0)
|
||||
{
|
||||
Assert.HandleFail("CollectionAssert.NotEmpty", message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified collections are equal and throws an exception if the two collections are not equal. Equality is defined as having the same elements in the same order and quantity. Different references to the same value are considered equal.
|
||||
/// </summary>
|
||||
/// <param name="expected">The first collection to compare. This is the collection the tests expects.</param>
|
||||
/// <param name="actual"> The second collection to compare. This is the collection produced by the code under test.</param>
|
||||
/// <param name="message">The message to include in the exception when <paramref name="actual"/> is not equal to <paramref name="expected"/>. The message is shown in test results.</param>
|
||||
/// <exception cref="AssertFailedException">Thrown if <paramref name="expected"/> is not equal to <paramref name="actual"/>.</exception>
|
||||
public static void AreEqual(ICollection expected, ICollection actual, string message = "")
|
||||
{
|
||||
string reason = string.Empty;
|
||||
|
||||
if (!AreCollectionsEqual(expected, actual, ref reason))
|
||||
{
|
||||
Assert.HandleFail(
|
||||
"CollectionAssert.AreEqual",
|
||||
string.Format(CollectionEqualReason, new object[2]
|
||||
{
|
||||
message,
|
||||
reason
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion region
|
||||
|
||||
private static bool AreCollectionsEqual(
|
||||
ICollection expected,
|
||||
ICollection actual,
|
||||
ref string reason)
|
||||
{
|
||||
if (expected != actual)
|
||||
{
|
||||
if (expected == null || actual == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (expected.Count != actual.Count)
|
||||
{
|
||||
reason = NumberOfElementsDiff;
|
||||
return false;
|
||||
}
|
||||
|
||||
IEnumerator enumerator = expected.GetEnumerator();
|
||||
IEnumerator enumerator2 = actual.GetEnumerator();
|
||||
|
||||
int num = 0;
|
||||
|
||||
while (enumerator.MoveNext() && enumerator2.MoveNext())
|
||||
{
|
||||
if (enumerator.Current != enumerator2.Current)
|
||||
{
|
||||
reason = string.Format(
|
||||
ElementsAtIndexDontMatch,
|
||||
new object[1] { num });
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
num++;
|
||||
}
|
||||
|
||||
reason = BothCollectionsSameElements;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
reason = string.Format(
|
||||
BothCollectionsSameReference,
|
||||
new object[1] { string.Empty });
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,783 +0,0 @@
|
|||
//
|
||||
// Copyright (c) .NET Foundation and Contributors
|
||||
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace nanoFramework.TestFramework
|
||||
{
|
||||
/// <summary>
|
||||
/// A static Assert class as a helper for tests
|
||||
/// </summary>
|
||||
public class Assert
|
||||
{
|
||||
#region SkipTest
|
||||
|
||||
public static void SkipTest(string message = "")
|
||||
{
|
||||
if (string.IsNullOrEmpty(message))
|
||||
{
|
||||
throw new SkipTestException();
|
||||
}
|
||||
|
||||
throw new SkipTestException(message);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region true/false
|
||||
|
||||
/// <summary>
|
||||
/// Check if a condition is true
|
||||
/// </summary>
|
||||
/// <param name="condition">The condition to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the condition is not true</exception>
|
||||
public static void True(bool condition, string message = "")
|
||||
{
|
||||
if (condition)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{condition} is not true. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a condition is false
|
||||
/// </summary>
|
||||
/// <param name="condition">The condition to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the condition is not false</exception>
|
||||
public static void False(bool condition, string message = "")
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{condition} is not false. {message}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Equal
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(bool a, bool b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(int a, int b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(Array a, Array b, string message = "")
|
||||
{
|
||||
if (a.SequenceEqual(b))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(uint a, uint b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(short a, short b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(ushort a, ushort b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(long a, long b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(ulong a, ulong b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(byte a, byte b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(char a, char b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(sbyte a, sbyte b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(double a, double b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(float a, float b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(string a, string b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void Equal(DateTime a, DateTime b, string message = "")
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not equal to {b}. {message}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region NotEqual
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(bool a, bool b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(int a, int b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(Array a, Array b, string message = "")
|
||||
{
|
||||
if (!a.SequenceEqual(b))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are not equal</exception>
|
||||
public static void NotEqual(uint a, uint b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(short a, short b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(ushort a, ushort b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(long a, long b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(ulong a, ulong b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(byte a, byte b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(char a, char b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(sbyte a, sbyte b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(double a, double b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(float a, float b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(string a, string b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if both elements are not equal
|
||||
/// </summary>
|
||||
/// <param name="a">First element</param>
|
||||
/// <param name="b">Second element</param>
|
||||
/// <exception cref="Exception">Raises an exception if both elements are equal</exception>
|
||||
public static void NotEqual(DateTime a, DateTime b, string message = "")
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be equal to {b}. {message}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region string
|
||||
|
||||
/// <summary>
|
||||
/// Check if a string is included in another string
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected string</param>
|
||||
/// <param name="actual">The actual string to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the expected string is not included in the actual</exception>
|
||||
public static void Contains(string expected, string actual, string message = "")
|
||||
{
|
||||
if (actual.IndexOf(expected) >= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{actual} does not contains {expected}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a string does not contains another string
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected string</param>
|
||||
/// <param name="actual">The actual string to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the expected string is included in the actual</exception>
|
||||
public static void DoesNotContains(string expected, string actual, string message = "")
|
||||
{
|
||||
if (actual == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (actual.IndexOf(expected) < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{actual} should not contain {expected}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check is a string ends with another string
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected string</param>
|
||||
/// <param name="actual">The actual string to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the expected string is not at the end of the actual string</exception>
|
||||
public static void EndsWith(string expected, string actual, string message = "")
|
||||
{
|
||||
// We have to take the last index as the text can contains multiple times the same word
|
||||
if (actual.LastIndexOf(expected) == actual.Length - expected.Length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{actual} does not ends with {expected}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check is a string starts with another string
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected string</param>
|
||||
/// <param name="actual">The actual string to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the expected string is not at the end of the actual string</exception>
|
||||
public static void StartsWith(string expected, string actual, string message = "")
|
||||
{
|
||||
if (actual.IndexOf(expected) == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{actual} does not starts with {expected}. {message}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region collection
|
||||
|
||||
/// <summary>
|
||||
/// Check if a collection is empty
|
||||
/// </summary>
|
||||
/// <param name="collection">The collection to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the collection is not empty</exception>
|
||||
public static void Empty(ICollection collection, string message = "")
|
||||
{
|
||||
if (collection.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{collection} is not empty. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a collection is empty
|
||||
/// </summary>
|
||||
/// <param name="collection">The collection to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the collection is not empty</exception>
|
||||
public static void NotEmpty(ICollection collection, string message = "")
|
||||
{
|
||||
if (collection.Count > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{collection} should not be empty. {message}");
|
||||
}
|
||||
|
||||
#endregion region
|
||||
|
||||
#region types, objects
|
||||
|
||||
/// <summary>
|
||||
/// Check if an object is equal to a type
|
||||
/// </summary>
|
||||
/// <param name="type">The type to check</param>
|
||||
/// <param name="obj">The object to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the types are not equal</exception>
|
||||
public static void IsType(Type type, object obj, string message = "")
|
||||
{
|
||||
if (type == obj.GetType())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{obj} is not type of {type}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an object is not equal to a type
|
||||
/// </summary>
|
||||
/// <param name="type">The type to check</param>
|
||||
/// <param name="obj">The object to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the types are equal</exception>
|
||||
public static void IsNotType(Type type, object obj, string message = "")
|
||||
{
|
||||
if (type != obj.GetType())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{obj} should not be type of {type}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an object is the same as another
|
||||
/// </summary>
|
||||
/// <param name="a">The first object</param>
|
||||
/// <param name="b">The second object</param>
|
||||
/// <exception cref="Exception">Raises an exception if the objects are not the same</exception>
|
||||
public static void Same(object a, object b, string message = "")
|
||||
{
|
||||
if (a.Equals(b))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} is not the same as {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an object is not the same as another
|
||||
/// </summary>
|
||||
/// <param name="a">The first object</param>
|
||||
/// <param name="b">The second object</param>
|
||||
/// <exception cref="Exception">Raises an exception if the objects are the same</exception>
|
||||
public static void NotSame(object a, object b, string message = "")
|
||||
{
|
||||
if (!a.Equals(b))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"{a} should not be the same as {b}. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an object is null
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the object is not null</exception>
|
||||
public static void Null(object obj, string message = "")
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"The object is not null. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an object is not null
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to check</param>
|
||||
/// <exception cref="Exception">Raises an exception if the object is null/exception>
|
||||
public static void NotNull(object obj, string message = "")
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"The object should not be null. {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an exception is raised and matching a type
|
||||
/// </summary>
|
||||
/// <param name="exceptionType">The exception to be raised</param>
|
||||
/// <param name="action">The method to execute</param>
|
||||
public static void Throws(Type exceptionType, Action action, string message = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ex.GetType() == exceptionType)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception($"An exception {ex.GetType()} has been thrown but is not type {exceptionType}. {message}");
|
||||
}
|
||||
|
||||
throw new Exception($"No exception has been thrown. {message}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -30,9 +30,10 @@
|
|||
</PropertyGroup>
|
||||
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
|
||||
<ItemGroup>
|
||||
<Compile Include="CollectionAssert.cs" />
|
||||
<Compile Include="OutputHelper.cs" />
|
||||
<Compile Include="TestExtensions.cs" />
|
||||
<Compile Include="TestFramework.cs" />
|
||||
<Compile Include="Assert.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
////
|
||||
// Copyright (c) .NET Foundation and Contributors
|
||||
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// See LICENSE file in the project root for full license information.
|
||||
////
|
||||
|
||||
using System;
|
||||
|
||||
namespace TestFrameworkShared
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="AssertFailedException"/> class. Used to indicate failure for a test case.
|
||||
/// </summary>
|
||||
public class AssertFailedException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssertFailedException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="msg">The message.</param>
|
||||
/// <param name="ex">The exception.</param>
|
||||
public AssertFailedException(string msg, Exception ex)
|
||||
: base(msg, ex)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssertFailedException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="msg">The message.</param>
|
||||
public AssertFailedException(string msg)
|
||||
: base(msg)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssertFailedException"/> class.
|
||||
/// </summary>
|
||||
public AssertFailedException()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
<Import_RootNamespace>TestFrameworkShared</Import_RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)AssertFailedException.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)CleanupAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DataRowAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helper.cs" />
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
|
||||
"version": "2.0",
|
||||
"version": "2.1",
|
||||
"assemblyVersion": {
|
||||
"precision": "build"
|
||||
},
|
||||
|
|
Загрузка…
Ссылка в новой задаче