diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md index 148d382..f9335f9 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md @@ -9,7 +9,7 @@ ## Fail (abstract class) - Fail.AsOrFail( value: object [CanBeNull, NoEnumeration], - name: string [CanBeNull, Optional] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, CanBeNull, AssertionMethod, ContractAnnotation] - Fail.Because( message: string [NotNull, NotNull] @@ -48,7 +48,7 @@ ) : T? [Extension, NotNull] - Fail.CastOrFail( value: object [CanBeNull, NoEnumeration], - name: string [CanBeNull, Optional] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.FailIfEmpty( value: DateTime, diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md index 328ed47..ea78d2c 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md @@ -9,7 +9,7 @@ ## Fail (abstract class) - Fail.AsOrFail( value: object [CanBeNull, NoEnumeration], - name: string [CanBeNull, Optional] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, CanBeNull, AssertionMethod, ContractAnnotation] - Fail.Because( message: string [NotNull, NotNull] @@ -48,7 +48,7 @@ ) : T? [Extension, NotNull] - Fail.CastOrFail( value: object [CanBeNull, NoEnumeration], - name: string [CanBeNull, Optional] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.FailIfEmpty( value: DateTime, diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md index 328ed47..ea78d2c 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md @@ -9,7 +9,7 @@ ## Fail (abstract class) - Fail.AsOrFail( value: object [CanBeNull, NoEnumeration], - name: string [CanBeNull, Optional] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, CanBeNull, AssertionMethod, ContractAnnotation] - Fail.Because( message: string [NotNull, NotNull] @@ -48,7 +48,7 @@ ) : T? [Extension, NotNull] - Fail.CastOrFail( value: object [CanBeNull, NoEnumeration], - name: string [CanBeNull, Optional] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.FailIfEmpty( value: DateTime, diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs index 2dd0b2a..a7e85ab 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs @@ -62,7 +62,7 @@ public void CastOrFail() ); // ASSERT - Assert.That(exception.Message, Is.EqualTo("Expected object of type 'System.String' but was '1'")); + Assert.That(exception.Message, Is.EqualTo("Expected somethingNotCastable of type 'System.String' but was '1'")); } [Test] @@ -78,7 +78,7 @@ public void CastOrFailWithNull() ); // ASSERT - Assert.That(exception.Message, Is.EqualTo("Expected object of type 'System.String' but was 'null'")); + Assert.That(exception.Message, Is.EqualTo("Expected somethingNotCastable of type 'System.String' but was 'null'")); } [Test] diff --git a/Contracts/Synergy.Contracts/Failures/FailCast.cs b/Contracts/Synergy.Contracts/Failures/FailCast.cs index 8d8861a..5e0a4c1 100644 --- a/Contracts/Synergy.Contracts/Failures/FailCast.cs +++ b/Contracts/Synergy.Contracts/Failures/FailCast.cs @@ -19,17 +19,17 @@ static partial class Fail [AssertionMethod] [ContractAnnotation("value: null => null; value: notnull => notnull")] public static T AsOrFail( - [CanBeNull] [NoEnumeration] this object value, + [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)); - return (T) value; + return (T)value; } /// @@ -40,10 +40,18 @@ string name /// Value to check if it can be cast to specified type. /// Name of the object to cast. /// The cast object. This method will NEVER return . - [NotNull] [return: System.Diagnostics.CodeAnalysis.NotNull] + [NotNull] + [return: System.Diagnostics.CodeAnalysis.NotNull] [AssertionMethod] [ContractAnnotation("value: null => halt; value: notnull => notnull")] - public static T CastOrFail([CanBeNull] [NoEnumeration] this object value, [CanBeNull] string name = null) + public static T CastOrFail( + [CanBeNull] [NoEnumeration] this object value, +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null +#else + string name +#endif + ) { Type castType = typeof(T); Fail.IfNull(value, Violation.WhenCannotCast(name ?? "object", value)); @@ -51,11 +59,11 @@ public static T CastOrFail([CanBeNull] [NoEnumeration] this object value, [Ca if (castType.IsEnum) { Fail.IfEnumNotDefined(value); - return (T) Enum.ToObject(castType, value); + return (T)Enum.ToObject(castType, value); } Fail.IfNotCastable(value, Violation.WhenCannotCast(name ?? "object", value)); - return (T) value; + return (T)value; } /// @@ -66,7 +74,12 @@ public static T CastOrFail([CanBeNull] [NoEnumeration] this object value, [Ca /// The expected type. /// Message that will be passed to when the check fails. [AssertionMethod] - public static void IfNotCastable([CanBeNull] [NoEnumeration] object value, [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] Type expectedType, Violation message) + public static void IfNotCastable( + [CanBeNull] [NoEnumeration] object value, + [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] + Type expectedType, + Violation message + ) { Fail.RequiresType(expectedType); @@ -85,7 +98,10 @@ public static void IfNotCastable([CanBeNull] [NoEnumeration] object value, [NotN /// Value to check if it can be cast to specified type. /// Message that will be passed to when the check fails. [AssertionMethod] - public static void IfNotCastable([CanBeNull] [NoEnumeration] object value, Violation message) + public static void IfNotCastable( + [CanBeNull] [NoEnumeration] object value, + Violation message + ) { Fail.IfNotCastable(value, typeof(T), message); } @@ -115,7 +131,8 @@ public static void IfNullOrNotCastable([CanBeNull] [NoEnumeration] object val [ContractAnnotation("value: null => halt")] public static void IfNullOrNotCastable( [CanBeNull] [NoEnumeration] object value, - Violation message) + Violation message + ) { Fail.IfNull(value, message); Fail.IfNotCastable(value, typeof(T), message); diff --git a/Core/Synergy.Core/Windsor/ComponentCollectionResolver.cs b/Core/Synergy.Core/Windsor/ComponentCollectionResolver.cs index 392f60e..1ad19ba 100644 --- a/Core/Synergy.Core/Windsor/ComponentCollectionResolver.cs +++ b/Core/Synergy.Core/Windsor/ComponentCollectionResolver.cs @@ -34,7 +34,7 @@ public override object Resolve( [NotNull] DependencyModel dependency) { var array = base.Resolve(context, contextHandlerResolver, model, dependency) - .CastOrFail(); + .CastOrFail("array"); return this.RemoveBaseComponents(array, dependency); }