Example #1
0
 public static void VerifyLessThanOrEqualTo <T>(this IComparable <T> item, T value, string parameterName)
 {
     if (item.IsGreaterThan(value))
     {
         throw new ArgumentOutOfRangeException(parameterName);
     }
 }
        void GreaterThan(IComparable x, Object y, Boolean expected)
        {
            Boolean result = default;

            Test.IfNot.Action.ThrowsException(() => result = x.IsGreaterThan(y), out Exception ex);
            Test.If.Value.IsEqual(result, expected);
        }
Example #3
0
        public static void IsGreaterThan()
        {
            IComparable <string> instance = null;
            string value  = null;
            var    result = false;

            When("an extension is called", () =>
            {
                Act(() =>
                {
                    result = instance.IsGreaterThan(value);
                });

                And("an instance is null", () =>
                {
                    instance = null;

                    Should("return false", () =>
                    {
                        Assert.That(result, Is.False);
                    });
                });

                And("an instance is not null", () =>
                {
                    value = "121";

                    And("the instance is less than the value", () =>
                    {
                        instance = "111";

                        Should("return false", () =>
                        {
                            Assert.That(result, Is.False);
                        });
                    });

                    And("the instance is greater than the value", () =>
                    {
                        instance = "131";

                        Should("return true", () =>
                        {
                            Assert.That(result, Is.True);
                        });
                    });

                    And("the instance is equal to the value", () =>
                    {
                        instance = "121";

                        Should("return false", () =>
                        {
                            Assert.That(result, Is.False);
                        });
                    });
                });
            });
        }
Example #4
0
        public void When_IsGreaterThan_is_called_with_a_same_value()
        {
            // Arrange.
            IComparable <int> value = 10;

            // Act.
            bool result = value.IsGreaterThan(10);

            // Assert.
            result.Should().Be(false);
        }
Example #5
0
 public static bool IsBetween <T>(this IComparable <T> item, T minimum, T maximum, bool inclusive)
 {
     if (inclusive)
     {
         return(item.IsGreaterThanOrEqualTo(minimum) || item.IsLessThanOrEqualTo(maximum));
     }
     else
     {
         return(item.IsGreaterThan(minimum) || item.IsLessThan(maximum));
     }
 }
Example #6
0
 /// <summary>
 /// 如果大于比较值则返回默认值否则返回原值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="t"></param>
 /// <param name="compareValue">比较值</param>
 /// <param name="becomeValue">默认值</param>
 /// <returns></returns>
 public static T IfGreaterThanBecome <T>(this IComparable <T> t, T compareValue, T becomeValue)
 {
     if (t.IsGreaterThan(compareValue))
     {
         return(becomeValue);
     }
     else
     {
         return((T)t);
     }
 }
        /// <summary>
        /// Checks if a value is clamped in a given exclusive range.
        /// </summary>
        /// <param name="_this">The <see cref="IComparable"/> that is checked against the range.</param>
        /// <param name="min">The lower border of the range. Is considered lower than <paramref name="_this"/> if null.</param>
        /// <param name="max">The upper border of the range. Is considered higher than <paramref name="_this"/> if null.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <returns>True if <paramref name="_this"/> is clamped, false if not.</returns>
        /// <example>
        /// <code>
        /// if(someIndex.IsClampedExclusive(-1, someList.Count)) {
        ///     doSomething(someIndex, someList);
        /// }
        /// </code>
        /// </example>
        public static Boolean IsClampedExclusive(this IComparable _this, Object min, Object max)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));

            Boolean result = true;

            result &= min == null || _this.IsGreaterThan(min);
            result &= max == null || _this.IsLessThan(max);

            return(result);
        }
Example #8
0
        public static bool IsCompareToType(this IComparable value, IComparable otherValue, CompareToType compareToType)
        {
            switch (compareToType)
            {
            case CompareToType.Equals: return(value.IsEqualTo(otherValue));

            case CompareToType.GreaterThan: return(value.IsGreaterThan(otherValue));

            case CompareToType.GreaterThanOrEquals: return(value.IsGreaterThanOrEquals(otherValue));

            case CompareToType.LessThan: return(value.IsLessThan(otherValue));

            case CompareToType.LessThanOrEquals: return(value.IsLessThanOrEquals(otherValue));
            }
            return(false);
        }
Example #9
0
 /// <summary>
 ///     Returns whether the specified <see cref="IComparable"/> is between two ranges (exclusive)
 /// </summary>
 public static bool IsBetweenExclusive(this IComparable comparable, IComparable lower,
                                       IComparable upper)
 => comparable.IsGreaterThan(lower) && comparable.IsLessThan(lower);
Example #10
0
 /// <summary>
 ///     Returns whether the specified instance of <see cref="IComparable"/> is greater than or equal to another specified instance of <see cref="IComparable"/>
 /// </summary>
 public static bool IsGreaterThanOrEqualTo(this IComparable comparable, IComparable other) => comparable.IsGreaterThan(other) || comparable.IsEqualTo(other);