Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfArgumentEqual(obj1, obj2) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 476439c commit 5e3028b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
argumentName: string [NotNull]
) : void [AssertionMethod]
- Fail.IfArgumentEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
argumentValue: TActual? [CanBeNull],
argumentName: string [NotNull, NotNull]
) : void [AssertionMethod]
unexpected: TExpected [Nullable, CanBeNull],
argumentValue: TActual [Nullable, CanBeNull],
argumentName: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfArgumentNull<T>(
argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration],
argumentName: string? [CallerArgumentExpression, Optional]
Expand Down Expand Up @@ -140,14 +140,14 @@
value: T
) : void [NullableContext]
- Fail.IfEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
message: Violation
) : void [AssertionMethod]
unexpected: TExpected [Nullable, CanBeNull],
actual: TActual [Nullable, CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod]
- Fail.IfFalse(
value: bool [AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
argumentName: string [NotNull]
) : void [AssertionMethod]
- Fail.IfArgumentEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
argumentValue: TActual? [CanBeNull],
argumentName: string [NotNull, NotNull]
) : void [AssertionMethod]
unexpected: TExpected [Nullable, CanBeNull],
argumentValue: TActual [Nullable, CanBeNull],
argumentName: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfArgumentNull<T>(
argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration],
argumentName: string? [CallerArgumentExpression, Optional]
Expand Down Expand Up @@ -140,14 +140,14 @@
value: T
) : void [NullableContext]
- Fail.IfEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
message: Violation
) : void [AssertionMethod]
unexpected: TExpected [Nullable, CanBeNull],
actual: TActual [Nullable, CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod]
- Fail.IfFalse(
value: bool [AssertionCondition],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
argumentName: string [NotNull]
) : void [AssertionMethod]
- Fail.IfArgumentEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
argumentValue: TActual? [CanBeNull],
argumentName: string [NotNull, NotNull]
) : void [AssertionMethod]
unexpected: TExpected [Nullable, CanBeNull],
argumentValue: TActual [Nullable, CanBeNull],
argumentName: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfArgumentNull<T>(
argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration],
argumentName: string? [CallerArgumentExpression, Optional]
Expand Down Expand Up @@ -140,14 +140,14 @@
value: T
) : void [NullableContext]
- Fail.IfEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
message: Violation
) : void [AssertionMethod]
unexpected: TExpected [Nullable, CanBeNull],
actual: TActual [Nullable, CanBeNull],
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfEqual<TExpected, TActual>(
unexpected: TExpected? [CanBeNull],
actual: TActual? [CanBeNull],
name: string [NotNull, NotNull]
message: Violation
) : void [AssertionMethod]
- Fail.IfFalse(
value: bool [AssertionCondition],
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 @@ -46,13 +46,27 @@ public void IfArgumentEqual(Pair obj)
// ASSERT
Assert.That(exception.Message, Is.EqualTo("Argument 'obj' is equal to " + obj.GetValue2() + " and it should NOT be."));
}

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

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

[Test]
[TestCaseSource(nameof(FailEqualityTest.GetNotEquals))]
public void IfArgumentEqualSuccess(Pair obj)
{
// ACT
Fail.IfArgumentEqual(obj.Value1, obj.Value2, nameof(obj));
Fail.IfArgumentEqual(obj.Value1, obj.Value2);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion Contracts/Synergy.Contracts/Failures/Fail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private static void RequiresMessage([NotNull] [System.Diagnostics.CodeAnalysis.N
/// Checks if argument name was provided.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
private static void RequiresArgumentName([NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string argumentName)
private static void RequiresArgumentName(string? argumentName)
{
if (string.IsNullOrWhiteSpace(argumentName))
throw new ArgumentNullException(nameof(argumentName));
Expand Down
7 changes: 0 additions & 7 deletions Contracts/Synergy.Contracts/Failures/FailEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,5 @@ string name

return value.CastOrFail<T>(name);
}

//[ExcludeFromCodeCoverage]
//private static void RequiresEnumValue([NotNull] Enum value)
//{
// if (value == null)
// throw new ArgumentNullException(nameof(value));
//}
}
}
14 changes: 12 additions & 2 deletions Contracts/Synergy.Contracts/Failures/FailEquality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ static partial class Fail
public static void IfEqual<TExpected, TActual>(
[CanBeNull] TExpected unexpected,
[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 Expand Up @@ -60,7 +65,12 @@ Violation message
public static void IfArgumentEqual<TExpected, TActual>(
[CanBeNull] TExpected unexpected,
[CanBeNull] TActual argumentValue,
[NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string argumentName
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("argumentValue")]
string? argumentName = null
#else
string argumentName
#endif
)
{
Fail.RequiresArgumentName(argumentName);
Expand Down

0 comments on commit 5e3028b

Please sign in to comment.