Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added value1.FailIfNotEqual(value2) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent bf1e29c commit 5c6efcc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Technical Debt for Synergy.Contracts

Total: 9
Total: 8

## [README.Generate.cs](../../Docs/README.Generate.cs)
- TODO: Marcin Celej [from: Marcin Celej on: 03-02-2024]: Prepare full description of Contract checks
Expand All @@ -19,6 +19,3 @@ Total: 9
## [FailCollection.cs](../../../Synergy.Contracts/Failures/FailCollection.cs)
- TODO:mace (from:mace @ 22-10-2016) public static void IfCollectionDoesNotContain<T>([CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable<T> collection,)
- TODO: Marcin Celej [from: Marcin Celej on: 29-05-2023]: Fail.IfCollectionContainsDuplicates

## [FailEquality.cs](../../../Synergy.Contracts/Failures/FailEquality.cs)
- TODO:mace (from:mace @ 22-10-2016): a.FailIfNotEqual(b)
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
date: DateTime? [CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : DateTime? [NullableContext, Extension, CanBeNull, AssertionMethod]
- Fail.FailIfNotEqual<TExpected, TActual>(
actual: TActual [Nullable],
expected: TExpected [Nullable],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, Extension, AssertionMethod]
- Fail.FailIfNull<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
message: Violation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
date: DateTime? [CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : DateTime? [NullableContext, Extension, CanBeNull, AssertionMethod]
- Fail.FailIfNotEqual<TExpected, TActual>(
actual: TActual [Nullable],
expected: TExpected [Nullable],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, Extension, AssertionMethod]
- Fail.FailIfNull<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
message: Violation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
date: DateTime? [CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : DateTime? [NullableContext, Extension, CanBeNull, AssertionMethod]
- Fail.FailIfNotEqual<TExpected, TActual>(
actual: TActual [Nullable],
expected: TExpected [Nullable],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, Extension, AssertionMethod]
- Fail.FailIfNull<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
message: Violation
Expand Down
22 changes: 22 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ public void IfNotEqualWithNameSuccess(Pair obj)

#endregion

[Test]
[TestCaseSource(nameof(FailEqualityTest.GetNotEquals))]
public void FailIfNotEqualWithName(Pair obj)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => obj.Value1.FailIfNotEqual(obj.Value2)
);

// ASSERT
Assert.That(exception.Message, Is.EqualTo("'obj.Value1' (" + obj.GetValue1() + ") is NOT equal to " + obj.GetValue2() + " and it should be."));
}

[Test]
[TestCaseSource(nameof(FailEqualityTest.GetEquals))]
public void FailIfNotEqualWithNameSuccess(Pair obj)
{
// ACT
obj.Value1.FailIfNotEqual(obj.Value2, nameof(obj));
obj.Value1.FailIfNotEqual(obj.Value2);
}

private static IEnumerable<Pair> GetEquals()
{
yield return new Pair(1, 1);
Expand Down
78 changes: 53 additions & 25 deletions Contracts/Synergy.Contracts/Failures/FailEquality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@ namespace Synergy.Contracts
{
static partial class Fail
{
// TODO:mace (from:mace @ 22-10-2016): a.FailIfNotEqual(b)

/// <summary>
/// Throws exception when argument value is equal to the <paramref name="unexpected"/> value.
/// <para>REMARKS: If one of the values is <see langword="null" /> the other one CANNOT be <see langword="null" />.</para>
/// </summary>
/// <param name="unexpected">The unexpected value.</param>
/// <param name="argumentValue">The argument value to be checked.</param>
/// <param name="argumentName">Name of the argument passed to your method.</param>
[AssertionMethod]
public static void IfArgumentEqual<TExpected, TActual>(
[CanBeNull] TExpected unexpected,
[CanBeNull] TActual argumentValue,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("argumentValue")]
string? argumentName = null
#else
string argumentName
#endif
)
{
Fail.RequiresArgumentName(argumentName);
Fail.IfEqual(unexpected, argumentValue, Violation.WhenArgumentEqual(argumentName, unexpected));
}

#region Fail.IfEqual()

/// <summary>
Expand Down Expand Up @@ -77,30 +98,9 @@ string name
}

#endregion

/// <summary>
/// Throws exception when argument value is equal to the <paramref name="unexpected"/> value.
/// <para>REMARKS: If one of the values is <see langword="null" /> the other one CANNOT be <see langword="null" />.</para>
/// </summary>
/// <param name="unexpected">The unexpected value.</param>
/// <param name="argumentValue">The argument value to be checked.</param>
/// <param name="argumentName">Name of the argument passed to your method.</param>
[AssertionMethod]
public static void IfArgumentEqual<TExpected, TActual>(
[CanBeNull] TExpected unexpected,
[CanBeNull] TActual argumentValue,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("argumentValue")]
string? argumentName = null
#else
string argumentName
#endif
)
{
Fail.RequiresArgumentName(argumentName);
Fail.IfEqual(unexpected, argumentValue, Violation.WhenArgumentEqual(argumentName, unexpected));
}

#region Fail.IfNotEqual()

/// <summary>
/// Throws exception when two values are NOT equal.
/// <para>REMARKS: If one of the values is <see langword="null" /> the other one MUST also be <see langword="null" />.</para>
Expand Down Expand Up @@ -141,5 +141,33 @@ Violation message
if (object.Equals(expected, actual) == false)
throw Fail.Because(message);
}

#endregion

#region a.FailIfNotEqual(b)

/// <summary>
/// Throws exception when two values are equal.
/// <para>REMARKS: If one of the values is <see langword="null" /> the other one CANNOT be <see langword="null" />.</para>
/// </summary>
/// <param name="unexpected">The unexpected value.</param>
/// <param name="actual">The actual value to be checked.</param>
/// <param name="name">Name of the checked argument / parameter to check.</param>
[AssertionMethod]
public static void FailIfNotEqual<TExpected, TActual>(
this TActual actual,
TExpected expected,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("actual")]
string? name = null
#else
string name
#endif
)
{
Fail.IfNotEqual(expected, actual, name);
}

#endregion
}
}

0 comments on commit 5c6efcc

Please sign in to comment.