Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfNotEqual(obj1, obj2) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 5e3028b commit d61e2d1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfNotEqual<TExpected, TActual>(
expected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
message: Violation
) : void [AssertionMethod]
expected: TExpected [Nullable, CanBeNull],
actual: TActual [Nullable, CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfNotEqual<TExpected, TActual>(
expected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod]
- Fail.IfNotNull<T>(
value: T [Nullable, CanBeNull, NoEnumeration],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfNotEqual<TExpected, TActual>(
expected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
message: Violation
) : void [AssertionMethod]
expected: TExpected [Nullable, CanBeNull],
actual: TActual [Nullable, CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfNotEqual<TExpected, TActual>(
expected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod]
- Fail.IfNotNull<T>(
value: T [Nullable, CanBeNull, NoEnumeration],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfNotEqual<TExpected, TActual>(
expected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
message: Violation
) : void [AssertionMethod]
expected: TExpected [Nullable, CanBeNull],
actual: TActual [Nullable, CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfNotEqual<TExpected, TActual>(
expected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod]
- Fail.IfNotNull<T>(
value: T [Nullable, CanBeNull, NoEnumeration],
Expand Down
14 changes: 14 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,26 @@ public void IfNotEqualWithName(Pair obj)
Assert.That(exception.Message, Is.EqualTo("'obj' (" + obj.GetValue1() + ") is NOT equal to " + obj.GetValue2() + " and it should be."));
}

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

// 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 IfNotEqualWithNameSuccess(Pair obj)
{
// ACT
Fail.IfNotEqual(obj.Value1, obj.Value2, nameof(obj));
Fail.IfNotEqual(obj.Value1, obj.Value2);
}

#endregion
Expand Down
7 changes: 6 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailEquality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ string argumentName
public static void IfNotEqual<TExpected, TActual>(
[CanBeNull] TExpected expected,
[CanBeNull] TActual actual,
[NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("actual")]
string? name = null
#else
string name
#endif
)
{
Fail.RequiresArgumentName(name);
Expand Down

0 comments on commit d61e2d1

Please sign in to comment.