From da2fd390c661cc67112ee586f6e1e799c771934b Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 16:22:36 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added sth.AsOrFail() method --- .../Architecture/Debt/Todos.Technical.Debt.verified.md | 3 +-- .../Synergy.Contracts.Test/Failures/FailCastTest.cs | 2 +- Contracts/Synergy.Contracts/Failures/FailBoolean.cs | 2 -- Contracts/Synergy.Contracts/Failures/FailCast.cs | 10 ++++++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md index 1d3d1d2..adbbe31 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md @@ -1,6 +1,6 @@ # Technical Debt for Synergy.Contracts -Total: 15 +Total: 14 ## [BusinessDocumentation.cs](../../Requirements/BusinessDocumentation.cs) - TODO: Marcin Celej [from: Marcin Celej on: 08-04-2023]: check that and probably convert docs int tt @@ -14,7 +14,6 @@ Total: 15 - TODO:mace (from:mace @ 22-10-2016): variable.FailIfTrue(message) ## [FailCast.cs](../../../Synergy.Contracts/Failures/FailCast.cs) -- TODO:mace (from:mace @ 22-10-2016): Add [AssertionCondition] below - TODO:mace (from:mace @ 22-10-2016): public static void Fail.IfArgumentNotCastable([CanBeNull, AssertionCondition(conditionType: AssertionConditionType.IS_NOT_NULL)] string argumentValue) ## [FailCollection.cs](../../../Synergy.Contracts/Failures/FailCollection.cs) diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs index 870dda9..2dd0b2a 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs @@ -21,7 +21,7 @@ public void AsOrFail() ); // ASSERT - Assert.That(exception.Message, Is.EqualTo("Expected object of type 'System.String' but was 'System.Object'")); + Assert.That(exception.Message, Is.EqualTo("Expected someObjectButSurelyNotString of type 'System.String' but was 'System.Object'")); } [Test] diff --git a/Contracts/Synergy.Contracts/Failures/FailBoolean.cs b/Contracts/Synergy.Contracts/Failures/FailBoolean.cs index 06631e4..7d46c5a 100644 --- a/Contracts/Synergy.Contracts/Failures/FailBoolean.cs +++ b/Contracts/Synergy.Contracts/Failures/FailBoolean.cs @@ -9,7 +9,6 @@ static partial class Fail #region Fail.IfFalse - /// /// Throws exception when checked value is . /// @@ -30,7 +29,6 @@ public static void IfFalse( #region Fail.IfTrue - /// /// Throws exception when checked value is . /// diff --git a/Contracts/Synergy.Contracts/Failures/FailCast.cs b/Contracts/Synergy.Contracts/Failures/FailCast.cs index 90cd57a..8d8861a 100644 --- a/Contracts/Synergy.Contracts/Failures/FailCast.cs +++ b/Contracts/Synergy.Contracts/Failures/FailCast.cs @@ -3,7 +3,6 @@ namespace Synergy.Contracts { - // TODO:mace (from:mace @ 22-10-2016): Add [AssertionCondition] below static partial class Fail { // TODO:mace (from:mace @ 22-10-2016): public static void Fail.IfArgumentNotCastable([CanBeNull, AssertionCondition(conditionType: AssertionConditionType.IS_NOT_NULL)] string argumentValue) @@ -19,7 +18,14 @@ static partial class Fail [CanBeNull] [AssertionMethod] [ContractAnnotation("value: null => null; value: notnull => notnull")] - public static T AsOrFail([CanBeNull] [NoEnumeration] this object value, [CanBeNull] string name = null) + public static T AsOrFail( + [CanBeNull] [NoEnumeration] this object value, +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null +#else + string name +#endif + ) { Fail.IfNotCastable(value, Violation.WhenCannotCast(name ?? "object", value));