Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added sth.OrFail() extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 2bfb424 commit 9524ef5
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Architecture/Synergy.Convention.Testing/Rules/Deficit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public struct Deficit
public MemberInfo Member { get; }
public string Description { get; }

private Type DeclaringType => Member.DeclaringType.OrFail(nameof(Member.DeclaringType))!;
private Type DeclaringType => Member.DeclaringType.OrFail();
public Assembly Assembly => DeclaringType.Assembly;
public string MemberName => FullNameOfMember();

public Deficit(MemberInfo member, string description)
{
this.Member = member.OrFail(nameof(member));
this.Description = description.OrFail(nameof(description));
this.Member = member.OrFail();
this.Description = description.OrFail();
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
name: string [NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
name: string [NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T? [CanBeNull, AssertionCondition, NoEnumeration],
name: string [NotNull, NotNull]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.OrFail<T>(
value: T?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public List<Contractor> GetContractorsAged(DateTime minDate, DateTime? maxDate)

public List<Contractor> FilterContractors(ContractorFilterParameters paramaters)
{
paramaters.OrFail(nameof(paramaters));
paramaters.OrFail();
paramaters.EstablishedBetween.FailIfNull(Violation.Of("'{0}' is null and it shouldn't be", nameof(paramaters.EstablishedBetween)));
paramaters.EstablishedBetween.Max.OrFail(nameof(paramaters.EstablishedBetween.Max));

Expand Down
16 changes: 14 additions & 2 deletions Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,32 @@ public void OrFail(object thisMustBeNull)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
// ReSharper disable once ExpressionIsAlwaysNull
() => thisMustBeNull.OrFail(nameof(thisMustBeNull)));

// ASSERT
Assert.That(exception.Message, Is.EqualTo("'thisMustBeNull' is null; and it shouldn't be;"));
}

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNulls))]
public void OrFailNetCore(object thisMustBeNull)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => thisMustBeNull.OrFail());

// ASSERT
Assert.That(exception.Message, Is.EqualTo("'thisMustBeNull' is null; and it shouldn't be;"));
}

[Test]
[TestCaseSource(nameof(FailNullabilityTest.GetNotNulls))]
[SuppressMessage("ReSharper", "UnusedVariable")]
public void OrFailSuccess(object thisCannotBeNull)
{
// ACT
Type type = thisCannotBeNull.OrFail(nameof(thisCannotBeNull)).GetType();
thisCannotBeNull.OrFail(nameof(thisCannotBeNull));
thisCannotBeNull.OrFail();
}

#endregion
Expand Down
7 changes: 6 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailNullability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumerat
public static T OrFail<T>(
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration]
this T value,
[NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name)
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null
#else
string name
#endif
)
{
Fail.RequiresArgumentName(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public CompareResponseWithPattern(string patternFilePath, Ignore? ignore = null,
public override Result Assert(HttpOperation operation)
{
// TODO: Add non-nullable annotations to OrFail() - and other contract methods
var current = operation.Response.Content.ReadJson().OrFail("response")!;
var current = operation.Response.Content.ReadJson().OrFail("response");
if (_savedPattern == null)
{
SaveNewPattern(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class VerifyRequestMethod : Assertion

public VerifyRequestMethod(params HttpMethod[] expectedMethod)
{
_expectedMethod = expectedMethod.OrFail(nameof(expectedMethod));
_expectedMethod = expectedMethod.OrFail();
ExpectedResult = $"HTTP request method is {_expectedMethod}";
}

Expand Down
6 changes: 3 additions & 3 deletions Web/Synergy.Web.Api.Testing/HttpOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class HttpOperation
internal void Init(TestServer testServer, HttpRequestMessage request, HttpResponseMessage response, Stopwatch timer)
{
Duration = timer.Elapsed;
TestServer = testServer.OrFail(nameof(testServer));
Request = request.OrFail(nameof(request));
Response = response.OrFail(nameof(response));
TestServer = testServer.OrFail();
Request = request.OrFail();
Response = response.OrFail();
}

internal void Assert(IEnumerable<IAssertion> assertions)
Expand Down

0 comments on commit 9524ef5

Please sign in to comment.