Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added sth.CastOrFail<T>() method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent da2fd39 commit debad70
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Fail (abstract class)
- Fail.AsOrFail<T>(
value: object [CanBeNull, NoEnumeration],
name: string [CanBeNull, Optional]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, CanBeNull, AssertionMethod, ContractAnnotation]
- Fail.Because(
message: string [NotNull, NotNull]
Expand Down Expand Up @@ -48,7 +48,7 @@
) : T? [Extension, NotNull]
- Fail.CastOrFail<T>(
value: object [CanBeNull, NoEnumeration],
name: string [CanBeNull, Optional]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfEmpty(
value: DateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Fail (abstract class)
- Fail.AsOrFail<T>(
value: object [CanBeNull, NoEnumeration],
name: string [CanBeNull, Optional]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, CanBeNull, AssertionMethod, ContractAnnotation]
- Fail.Because(
message: string [NotNull, NotNull]
Expand Down Expand Up @@ -48,7 +48,7 @@
) : T? [Extension, NotNull]
- Fail.CastOrFail<T>(
value: object [CanBeNull, NoEnumeration],
name: string [CanBeNull, Optional]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfEmpty(
value: DateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Fail (abstract class)
- Fail.AsOrFail<T>(
value: object [CanBeNull, NoEnumeration],
name: string [CanBeNull, Optional]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, CanBeNull, AssertionMethod, ContractAnnotation]
- Fail.Because(
message: string [NotNull, NotNull]
Expand Down Expand Up @@ -48,7 +48,7 @@
) : T? [Extension, NotNull]
- Fail.CastOrFail<T>(
value: object [CanBeNull, NoEnumeration],
name: string [CanBeNull, Optional]
name: string? [Nullable, CallerArgumentExpression, Optional]
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfEmpty(
value: DateTime,
Expand Down
4 changes: 2 additions & 2 deletions Contracts/Synergy.Contracts.Test/Failures/FailCastTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
37 changes: 27 additions & 10 deletions Contracts/Synergy.Contracts/Failures/FailCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ static partial class Fail
[AssertionMethod]
[ContractAnnotation("value: null => null; value: notnull => notnull")]
public static T AsOrFail<T>(
[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<T>(value, Violation.WhenCannotCast<T>(name ?? "object", value));

return (T) value;
return (T)value;
}

/// <summary>
Expand All @@ -40,22 +40,30 @@ string name
/// <param name="value">Value to check if it can be cast to specified type.</param>
/// <param name="name">Name of the object to cast.</param>
/// <returns>The cast object. This method will NEVER return <see langword="null"/>.</returns>
[NotNull] [return: System.Diagnostics.CodeAnalysis.NotNull]
[NotNull]
[return: System.Diagnostics.CodeAnalysis.NotNull]
[AssertionMethod]
[ContractAnnotation("value: null => halt; value: notnull => notnull")]
public static T CastOrFail<T>([CanBeNull] [NoEnumeration] this object value, [CanBeNull] string name = null)
public static T CastOrFail<T>(
[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<T>(name ?? "object", value));

if (castType.IsEnum)
{
Fail.IfEnumNotDefined<T>(value);
return (T) Enum.ToObject(castType, value);
return (T)Enum.ToObject(castType, value);
}

Fail.IfNotCastable<T>(value, Violation.WhenCannotCast<T>(name ?? "object", value));
return (T) value;
return (T)value;
}

/// <summary>
Expand All @@ -66,7 +74,12 @@ public static T CastOrFail<T>([CanBeNull] [NoEnumeration] this object value, [Ca
/// <param name="expectedType">The expected type.</param>
/// <param name="message">Message that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[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);

Expand All @@ -85,7 +98,10 @@ public static void IfNotCastable([CanBeNull] [NoEnumeration] object value, [NotN
/// <param name="value">Value to check if it can be cast to specified type.</param>
/// <param name="message">Message that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[AssertionMethod]
public static void IfNotCastable<T>([CanBeNull] [NoEnumeration] object value, Violation message)
public static void IfNotCastable<T>(
[CanBeNull] [NoEnumeration] object value,
Violation message
)
{
Fail.IfNotCastable(value, typeof(T), message);
}
Expand Down Expand Up @@ -115,7 +131,8 @@ public static void IfNullOrNotCastable<T>([CanBeNull] [NoEnumeration] object val
[ContractAnnotation("value: null => halt")]
public static void IfNullOrNotCastable<T>(
[CanBeNull] [NoEnumeration] object value,
Violation message)
Violation message
)
{
Fail.IfNull(value, message);
Fail.IfNotCastable(value, typeof(T), message);
Expand Down
2 changes: 1 addition & 1 deletion Core/Synergy.Core/Windsor/ComponentCollectionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override object Resolve(
[NotNull] DependencyModel dependency)
{
var array = base.Resolve(context, contextHandlerResolver, model, dependency)
.CastOrFail<Array>();
.CastOrFail<Array>("array");

return this.RemoveBaseComponents(array, dependency);
}
Expand Down

0 comments on commit debad70

Please sign in to comment.