Skip to content

Commit

Permalink
I added [NoEnumeration] in some places
Browse files Browse the repository at this point in the history
MarcinCelej committed Nov 8, 2017
1 parent 89575d3 commit abc0918
Showing 2 changed files with 28 additions and 13 deletions.
13 changes: 6 additions & 7 deletions Synergy.Contracts/Failures/FailCastable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using Synergy.Contracts.Extensions;

namespace Synergy.Contracts
{
@@ -23,7 +22,7 @@ public static partial class Fail
[ContractAnnotation("value: null => null; value: notnull => notnull")]
[CanBeNull]
[AssertionMethod]
public static T AsOrFail<T>([CanBeNull] this object value, [CanBeNull] string name = null)
public static T AsOrFail<T>([CanBeNull] [NoEnumeration] this object value, [CanBeNull] string name = null)
{
Fail.IfNotCastable<T>(value, Fail.notCastableMessageWithName, name ?? "object", typeof(T), value);

@@ -43,7 +42,7 @@ public static T AsOrFail<T>([CanBeNull] this object value, [CanBeNull] string na
[ContractAnnotation("value: null => halt; value: notnull => notnull")]
[NotNull]
[AssertionMethod]
public static T CastOrFail<T>([CanBeNull] this object value, [CanBeNull] string name = null)
public static T CastOrFail<T>([CanBeNull] [NoEnumeration] this object value, [CanBeNull] string name = null)
{
Fail.IfNull(value, Fail.notCastableMessageWithName, name ?? "object", typeof(T), "null");
Fail.IfNotCastable<T>(value, Fail.notCastableMessageWithName, name ?? "object", typeof(T), value);
@@ -60,7 +59,7 @@ public static T CastOrFail<T>([CanBeNull] this object value, [CanBeNull] string
/// <param name="args">Arguments that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[StringFormatMethod("message")]
[AssertionMethod]
public static void IfNotCastable([CanBeNull] object value, [NotNull] Type expectedType, [NotNull] string message, [NotNull] params object[] args)
public static void IfNotCastable([CanBeNull] [NoEnumeration] object value, [NotNull] Type expectedType, [NotNull] string message, [NotNull] params object[] args)
{
Fail.RequiresType(expectedType);
Fail.RequiresMessage(message, args);
@@ -82,7 +81,7 @@ public static void IfNotCastable([CanBeNull] object value, [NotNull] Type expect
/// <param name="args">Arguments that will be passed to <see cref="DesignByContractViolationException"/> when the check fails.</param>
[StringFormatMethod("message")]
[AssertionMethod]
public static void IfNotCastable<T>([CanBeNull] object value, [NotNull] string message, [NotNull] params object[] args)
public static void IfNotCastable<T>([CanBeNull] [NoEnumeration] object value, [NotNull] string message, [NotNull] params object[] args)
{
Fail.IfNotCastable(value, typeof(T), message, args);
}
@@ -95,7 +94,7 @@ public static void IfNotCastable<T>([CanBeNull] object value, [NotNull] string m
/// <param name="value">Value to check if it can be casted to specified type.</param>
[ContractAnnotation("value: null => halt")]
[AssertionMethod]
public static void IfNullOrNotCastable<T>([CanBeNull] object value)
public static void IfNullOrNotCastable<T>([CanBeNull] [NoEnumeration] object value)
{
Fail.IfNull(value, Fail.notCastableMessage, typeof(T), "<null>");
Fail.IfNotCastable<T>(value, Fail.notCastableMessage, typeof(T), value);
@@ -113,7 +112,7 @@ public static void IfNullOrNotCastable<T>([CanBeNull] object value)
[StringFormatMethod("message")]
[AssertionMethod]
public static void IfNullOrNotCastable<T>(
[CanBeNull] object value,
[CanBeNull] [NoEnumeration] object value,
[NotNull] string message,
[NotNull] params object[] args)
{
28 changes: 22 additions & 6 deletions Synergy.Contracts/Failures/FailNullability.cs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ public static partial class Fail
[NotNull]
[AssertionMethod]
public static T FailIfNull<T>(
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T value,
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration] this T value,
[NotNull] string message,
[NotNull] params object[] args)
{
@@ -47,12 +47,12 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T valu
/// <typeparam name="T">Type of the value to check against nullability.</typeparam>
/// <param name="value">Value to check against nullability.</param>
/// <param name="name">Name of the checked argument / parameter to check the nullability of.</param>
/// <returns></returns>
/// <returns>Exactly the same value as provided to this method.</returns>
[ContractAnnotation("value: null => halt; value: notnull => notnull")]
[AssertionMethod]
[NotNull]
public static T OrFail<T>(
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T value,
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration] this T value,
[NotNull] string name)
{
Fail.RequiresArgumentName(name);
@@ -63,6 +63,21 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T valu
return value;
}

/// <summary>
/// Returns EXACTLY the same object as method argument.
/// It is usefull when you have [NotNull] variable that you want to check against nullability as this method is marked with [CanBeNull].
/// </summary>
/// <typeparam name="T">Type of the value.</typeparam>
/// <param name="value">Value to change its contract from [NotNull] to [CanBeNull].</param>
/// <returns>Exactly the same value as provided to this method.</returns>
[ContractAnnotation("value: null => null; value: notnull => notnull")]
[AssertionMethod]
[CanBeNull]
public static T CanBeNull<T>([CanBeNull] [NoEnumeration] this T value)
{
return value;
}

#region Fail.IfArgumentNull

/// <summary>
@@ -73,7 +88,7 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T valu
[ContractAnnotation("argumentValue: null => halt")]
[AssertionMethod]
public static void IfArgumentNull<T>(
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T argumentValue,
[CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration] T argumentValue,
[NotNull] string argumentName)
{
Fail.RequiresArgumentName(argumentName);
@@ -91,7 +106,7 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T argumentV
[SourceTemplate]
[UsedImplicitly]
// ReSharper disable once InconsistentNaming
public static void fian([CanBeNull] this object argumentValue)
public static void fian([CanBeNull] [NoEnumeration] this object argumentValue)
{
Fail.IfArgumentNull(argumentValue, nameof(argumentValue));
}
@@ -215,8 +230,9 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value,
[StringFormatMethod("message")]
[ContractAnnotation("value: notnull => halt")]
[AssertionMethod]
public static void IfNotNull<T>([CanBeNull] T value, [NotNull] string message, [NotNull] params object[] args)
public static void IfNotNull<T>([CanBeNull] [NoEnumeration] T value, [NotNull] string message, [NotNull] params object[] args)
{
// TODO:mace (from:mace on:17-11-2016) This method should be splitted to 5 generic methods to prevent unnecesesary memory allocation
Fail.RequiresMessage(message, args);

if (value != null)

0 comments on commit abc0918

Please sign in to comment.