Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added guid.FailIfEmpty(message) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 4b5c0c4 commit 7e6acc0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Technical Debt for Synergy.Contracts

Total: 14
Total: 13

## [BusinessDocumentation.cs](../../Requirements/BusinessDocumentation.cs)
- TODO: Marcin Celej [from: Marcin Celej on: 08-04-2023]: check that and probably convert docs int tt
Expand All @@ -26,8 +26,5 @@ Total: 14
- TODO:mace (from:mace @ 22-10-2016): IfArgumentNotEqual
- TODO:mace (from:mace @ 22-10-2016): a.FailIfNotEqual(b)

## [FailGuid.cs](../../../Synergy.Contracts/Failures/FailGuid.cs)
- TODO:mace (from:mace @ 22-10-2016): guid.FailIfEmpty

## [FailNullability.cs](../../../Synergy.Contracts/Failures/FailNullability.cs)
- TODO: Marcin Celej [from: Marcin Celej on: 08-04-2023]: Use [CallerExpression] here - check https://andrewlock.net/exploring-dotnet-6-part-11-callerargumentexpression-and-throw-helpers/
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
value: DateTime,
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfEmpty(
value: Guid,
message: Violation
) : void [Extension, AssertionMethod]
- Fail.FailIfEnumOutOfRange<T>(
value: Enum,
name: string? [Nullable, CallerArgumentExpression, Optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
value: DateTime,
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfEmpty(
value: Guid,
message: Violation
) : void [Extension, AssertionMethod]
- Fail.FailIfEnumOutOfRange<T>(
value: Enum,
name: string? [Nullable, CallerArgumentExpression, Optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
value: DateTime,
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfEmpty(
value: Guid,
message: Violation
) : void [Extension, AssertionMethod]
- Fail.FailIfEnumOutOfRange<T>(
value: Enum,
name: string? [Nullable, CallerArgumentExpression, Optional]
Expand Down
26 changes: 26 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailGuidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@ public void IfEmptyWithMessageSuccess()

#endregion

#region variable.FailIfEmpty()

[Test]
public void FailIfEmptyWithMessage()
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => Guid.Empty.FailIfEmpty(Violation.Of("guid is empty and it shouldn't be"))
);

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

[Test]
public void FailIfEmptyWithMessageSuccess()
{
// ARRANGE
Guid notEmptyGuid = Guid.NewGuid();

// ACT
notEmptyGuid.FailIfEmpty(Violation.Of("guid is empty and it shouldn't be"));
}

#endregion

[ItemNotNull, NotNull]
private static IEnumerable GetEmptyGuid()
{
Expand Down
15 changes: 13 additions & 2 deletions Contracts/Synergy.Contracts/Failures/FailGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace Synergy.Contracts
{
static partial class Fail
{
// TODO:mace (from:mace @ 22-10-2016): guid.FailIfEmpty

/// <summary>
/// Throws exception when checked <see cref="Guid" /> is empty ({00000000-0000-0000-0000-000000000000}).
/// <para>REMARKS: When you create a <see cref="Guid" /> using default constructor it is empty.
Expand All @@ -20,6 +18,19 @@ public static void IfEmpty(Guid value, Violation message)
Fail.IfEqual(Guid.Empty, value, message);
}

/// <summary>
/// Throws exception when checked <see cref="Guid" /> is empty ({00000000-0000-0000-0000-000000000000}).
/// <para>REMARKS: When you create a <see cref="Guid" /> using default constructor it is empty.
/// You can check the emptiness using this Fail.</para>
/// </summary>
/// <param name="value">The <see cref="Guid" /> checked for emptiness</param>
/// <param name="message">Message that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[AssertionMethod]
public static void FailIfEmpty(this Guid value, Violation message)
{
Fail.IfEqual(Guid.Empty, value, message);
}

/// <summary>
/// Throws exception when checked argument is an empty <see cref="Guid" /> ({00000000-0000-0000-0000-000000000000}).<br/>
/// <para>REMARKS: When you create a <see cref="Guid" /> using default constructor it is empty.
Expand Down

0 comments on commit 7e6acc0

Please sign in to comment.