예제 #1
0
        /// <summary>
        ///     Checks that the enumerable contains only the given values and nothing else, in any order.
        ///     Note: this check succeeded with empty value.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="expectedValues">The expected values to be found.</param>
        /// <returns>
        ///     A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">The enumerable does not contain only the expected values provided.</exception>
        public static ICheckLink <ICheck <IEnumerable> > IsOnlyMadeOf(
            this ICheck <IEnumerable> check,
            params object[] expectedValues)
        {
            var properExpectedValues = ExtractEnumerableValueFromSingleEntry(expectedValues);

            ImplementIsOnlyMadeOf(ExtensibilityHelper.BeginCheckAs(check, enumerable => enumerable.Cast <object>()), properExpectedValues);

            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #2
0
 /// <summary>
 /// Checks that the actual value has an expected reference.
 /// </summary>
 /// <typeparam name="T">
 /// Type of the checked value.
 /// </typeparam>
 /// <typeparam name="TU">Type of expected reference</typeparam>
 /// <param name="check">The fluent check to be extended.</param>
 /// <param name="expected">The expected object.</param>
 /// <returns>
 /// A check link.
 /// </returns>
 /// <exception cref="FluentCheckException">The actual value is not the same reference than the expected value.</exception>
 public static ICheckLink <ICheck <T> > IsSameReferenceAs <T, TU>(this ICheck <T> check, TU expected)
 {
     ExtensibilityHelper.BeginCheck(check)
     .SetSutName("object")
     .ComparingTo(expected, "same instance as", "distinct from")
     .FailWhen(sut => !ReferenceEquals(sut, expected), "The {0} must be the same instance as the {1}.")
     .OnNegate("The {0} must be an instance distinct from the {1}.")
     .EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #3
0
 /// <summary>
 /// Checks that the actual value is more than a comparand.
 /// </summary>
 /// <param name="comparand">
 /// Comparand to compare the value to.
 /// </param>
 /// <returns>
 /// A check link.
 /// </returns>
 /// <exception cref="FluentCheckException">
 /// The value is not less than the comparand.
 /// </exception>
 public ICheckLink <ICheck <TN> > IsStrictlyGreaterThan(TN comparand)
 {
     ExtensibilityHelper.BeginCheck(this.check)
     .ComparingTo(comparand, "strictly greater than", "less than or equal to")
     .FailWhen(sut => sut.CompareTo(comparand) < 0, "The {0} is less than the {1}.")
     .FailWhen(sut => sut.CompareTo(comparand) == 0, "The {0} is equal to the {1}.")
     .OnNegate("The {0} is greater than the {1}.")
     .EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(this.check));
 }
예제 #4
0
        /// <summary>
        ///     Checks that the actual actualValue doesn't have all fields equal to the expected actualValue ones.
        /// </summary>
        /// <typeparam name="T">
        ///     Type of the checked actualValue.
        /// </typeparam>
        /// <param name="check">
        ///     The fluent check to be extended.
        /// </param>
        /// <param name="expected">
        ///     The expected actualValue.
        /// </param>
        /// <returns>
        ///     A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">
        ///     The actual actualValue has all fields equal to the expected actualValue ones.
        /// </exception>
        /// <remarks>
        ///     The comparison is done field by field.
        /// </remarks>
        public static ICheckLink <ICheck <T> > HasNotFieldsWithSameValues <T>(this ICheck <T> check, object expected)
        {
            var checker       = ExtensibilityHelper.ExtractChecker(check);
            var fieldsWrapper = ReflectionWrapper.BuildFromInstance(typeof(T), checker.Value,
                                                                    new Kernel.Criteria(BindingFlags.Instance));
            var checkWithConsidering = new CheckWithConsidering(fieldsWrapper, checker.Negated).All.Fields;

            ReflectionWrapperChecks.FieldEqualTest(checkWithConsidering, expected, false);
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #5
0
 /// <summary>
 ///     Checks that the checked <see cref="char" /> and the given one are the same letter, whatever the case.
 /// </summary>
 /// <param name="check">The chained fluent check.</param>
 /// <param name="otherChar">The other char that.</param>
 /// <exception cref="FluentCheckException">
 ///     The checked <see cref="char" /> is not the same letter as the expected one,
 ///     whatever the case.
 /// </exception>
 /// <returns>A check link.</returns>
 public static ICheckLink <ICheck <char> > IsSameLetterAs(this ICheck <char> check, char otherChar)
 {
     ExtensibilityHelper.BeginCheck(check).
     DefineExpectedValue(otherChar).
     FailWhen(sut => !IsALetter(sut), "The {0} is not the same letter as the {1} (whatever the case)." + Environment.NewLine + "The {0} is not even a letter!").
     FailWhen(sut => !IsSameCharCaseInsensitive(sut, otherChar), "The {0} is not the same letter as the {1} (whatever the case).").
     OnNegate("The {0} is the same letter as the {1} (whatever the case), whereas it must not.").
     EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #6
0
        public static ICheckLink <ICheck <BuildContext> > HasNoErrors(this ICheck <BuildContext> check)
        {
            var actual = ExtensibilityHelper.ExtractChecker(check).Value;

            ExtensibilityHelper.BeginCheck(check)
            .FailWhen(sut => sut.HasError, $"Current context has errors whereas it should not : {actual.Error?.Message}")
            .OnNegate("hum, I don't see any errors whereas it's supposed to have...")
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #7
0
 /// <summary>
 /// Checks if a given type has an attribute of a given type.
 /// </summary>
 /// <typeparam name="T">Expected attribute type.</typeparam>
 /// <param name="check">Check component.</param>
 /// <returns>A check to link checks.</returns>
 public static ICheckLink <ICheck <Type> > HasAttribute <T>(this ICheck <Type> check) where T : Attribute
 {
     ExtensibilityHelper.BeginCheck(check)
     .FailIfNull()
     .FailWhen(
         sut => !sut.TypeHasAttribute(typeof(T)),
         "The {0} does not have an attribute of the expected type.")
     .OnNegate("The {0} does have an attribute of type {1} where as it should not.")
     .EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #8
0
 /// <summary>
 ///     Checks that the string is not empty.
 /// </summary>
 /// <param name="check">The fluent check to be extended.</param>
 /// <returns>
 ///     A check link.
 /// </returns>
 /// <exception cref="FluentCheckException">The string is empty.</exception>
 public static ICheckLink <ICheck <string> > IsNotEmpty(this ICheck <string> check)
 {
     ExtensibilityHelper.BeginCheck(check)
     .FailWhen(sut => sut == null, "The {0} is null whereas it must have content.",
               MessageOption.NoCheckedBlock)
     .FailWhen(string.IsNullOrEmpty, "The {0} is empty, whereas it must not.", MessageOption.NoCheckedBlock)
     .OnNegateWhen(sut => sut == null, "The {0} is null instead of being empty.")
     .OnNegate("The {0} is not empty or null.")
     .EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #9
0
        /// <summary>
        /// Checks that the event is set within a given timeout.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="timeOut">The maximum amount of time before the event should be set (time unit being specified with the timeUnit parameter).</param>
        /// <param name="timeUnit">The time unit of the given timeOut.</param>
        /// <returns>
        /// A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">The event was not set before the given timeout.</exception>
        public static ICheckLink <ICheck <EventWaitHandle> > IsSetWithin(this ICheck <EventWaitHandle> check, double timeOut, TimeUnit timeUnit)
        {
            var duration = new Duration(timeOut, timeUnit);

            ExtensibilityHelper.BeginCheck(check)
            .FailWhen(sut => !sut.WaitOne((int)duration.ToMilliseconds()),
                      $"The {{checked}} has not been set before the given timeout ({duration}) whereas it must be.", MessageOption.NoCheckedBlock)
            .OnNegate($"The {{checked}} has been set before the given timeout ({duration}) whereas it must not.", MessageOption.NoCheckedBlock)
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #10
0
        /// <summary>
        /// Checks that the actual value is one of the legal values.
        /// </summary>
        /// <typeparam name="T">Type of the checked value.</typeparam>
        /// <param name="check">The fluent check context object.</param>
        /// <param name="values">List of possible values.</param>
        /// <returns>A check link</returns>
        public static ICheckLink <ICheck <T> > IsOneOf <T>(this ICheck <T> check, params T[] values)
        {
            var comparer = new EqualityHelper.EqualityComparer <T>();

            ExtensibilityHelper.BeginCheck(check)
            .ExpectingValues(values, values.Length, "one of", "none of")
            .FailsIf(sut => !values.Any(value => comparer.Equals(sut, value)), "The {0} is not one of the {1}.")
            .Negates("The {0} should not be one of the {1}.").
            EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #11
0
 /// <summary>
 /// Check that the code does not throw an exception.
 /// </summary>
 /// <param name="check">The fluent check to be extended.
 /// </param>
 /// <typeparam name="T">Inferred type of the code.</typeparam>
 /// <returns>
 /// A check link.
 /// </returns>
 /// <exception cref="FluentCheckException">The code raised an exception.</exception>
 public static ICheckLink <ICodeCheck <T> > DoesNotThrow <T>(this ICodeCheck <T> check)
     where T : RunTrace
 {
     ExtensibilityHelper.BeginCheck(check).
     SetSutName("code").
     CheckSutAttributes(sut => sut.RaisedException, "raised exception").
     FailWhen(sut => sut != null, "The checked code raised an exception, whereas it must not.").
     OnNegate("The checked code did not raise an exception, whereas it must.", MessageOption.NoCheckedBlock).
     EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #12
0
        /// <summary>
        /// Checks that the checked value is strictly less than the comparand.
        /// </summary>
        /// <param name="comparand">
        /// Comparand to compare the value to.
        /// </param>
        /// <returns>
        /// A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">
        /// The checked value is not strictly less than the comparand.
        /// </exception>
        public ICheckLink <ICheck <TN> > IsLessOrEqualThan(TN comparand)
        {
            ExtensibilityHelper.BeginCheck(check)
            .ComparingTo(comparand, "less than", "greater than")
            .FailWhen(sut => sut.CompareTo(comparand) > 0, "The {0} is greater than the {1}.")
            .OnNegateWhen(sut => sut.CompareTo(comparand) == 0, "The {0} is equal to the {1} whereas it must be greater.")
            .OnNegate("The {0} is less than the {1} whereas it must be greater.")
            .EndCheck();

            return(ExtensibilityHelper.BuildCheckLink(this.check));
        }
        /// <summary>
        /// Determines whether the actual number is close to an expected value within a given within.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="expected">The expected value.</param>
        /// <param name="within">The within.</param>
        /// <returns>A continuation check.</returns>
        public static ICheckLink <ICheck <float> > IsCloseTo(this ICheck <float> check, Double expected, Double within)
        {
            var range = new RangeBlock(expected, within);

            ExtensibilityHelper.BeginCheck(check).
            FailsIf((sut) => !range.IsInRange(sut), "The {0} is outside the expected value range.").
            Expecting(range).
            Negates("The {0} is within the expected range, whereas it must not.").
            EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #14
0
        /// <summary>
        /// Checks that the string matches a given regular expression.
        /// </summary>
        /// <param name="context">The fluent check to be extended.</param>
        /// <param name="regExp">The regular expression.</param>
        /// <returns>
        ///     A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">The string does not end with the expected prefix.</exception>
        public static ICheckLink <ICheck <string> > Matches(this ICheck <string> context, string regExp)
        {
            ExtensibilityHelper.BeginCheck(context)
            .DefineExpectedValue(regExp, "matches", "does not match")
            .FailIfNull()
            .FailWhen(sut => new Regex(regExp).IsMatch(sut) == false, "The {0} does not match the {1}.")
            .OnNegate("The {0} matches the {1}, whereas it must not.")
            .EndCheck();

            return(ExtensibilityHelper.BuildCheckLink(context));
        }
예제 #15
0
 /// <summary>
 /// Checks if a given type has an attribute of a given type.
 /// </summary>
 /// <typeparam name="T">Expected attribute type.</typeparam>
 /// <param name="check">Check component.</param>
 /// <returns>A check to link checks.</returns>
 public static ICheckLink <ICheck <Type> > HasAttribute <T>(this ICheck <Type> check) where T : Attribute
 {
     ExtensibilityHelper.BeginCheck(check)
     .FailIfNull()
     .FailWhen(
         sut => sut.GetTypeInfo().GetCustomAttributes(false).All(customAttribute => customAttribute.GetType() != typeof(T)),
         "The {0} does not have an attribute of the expected type.")
     .OnNegate("The {0} does have an attribute of type {1} where as it should not.")
     .EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #16
0
 /// <summary>
 /// Checks whether if the checked value is of the given type.
 /// </summary>
 /// <typeparam name="TU">The given type to check the checked value against.</typeparam>
 /// <returns>A chainable check.</returns>
 /// <exception cref="FluentCheckException">The specified value is not of the given type.</exception>
 public ICheckLink <IStructCheck <T> > IsInstanceOf <TU>() where TU : struct
 {
     ExtensibilityHelper.BeginCheck((FluentSut <T>) this)
     .FailWhen(sut => sut.GetTypeWithoutThrowingException() != typeof(TU),
               "The {0} is not an instance of the expected type.")
     .OnNegate(
         $"The {{0}} is an instance of [{typeof(TU).ToStringProperlyFormatted()}] whereas it must not.", MessageOption.WithType)
     .DefineExpectedType(typeof(TU))
     .EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(this));
 }
예제 #17
0
        /// <summary>
        /// Checks that the actual value is one of the legal values.
        /// </summary>
        /// <typeparam name="T">Type of the checked value.</typeparam>
        /// <param name="check">The fluent check context object.</param>
        /// <param name="values">List of possible values.</param>
        /// <returns>A check link</returns>
        public static ICheckLink <ICheck <T> > IsOneOf <T>(this ICheck <T> check, params T[] values)
        {
            var comparer = new EqualityHelper.EqualityComparer <T>();

            ExtensibilityHelper.BeginCheck(check)
            .DefinePossibleValues(values, values.Length)
            .FailWhen(sut => !values.Any(value => comparer.Equals(sut, value)), "The {0} is not one of the {1}.")
            .OnNegate("The {0} should not be one of the {1}.").
            EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #18
0
        /// <summary>
        ///     Checks that the string is null, empty or only spaces.
        /// </summary>
        /// <param name="check">The fluent check.</param>
        /// <returns>
        ///     A check link.
        /// </returns>
        public static ICheckLink <ICheck <string> > IsNullOrWhiteSpace(this ICheck <string> check)
        {
            ExtensibilityHelper.BeginCheck(check)
            .FailWhen(sut => !PolyFill.IsNullOrWhiteSpace(sut), "The {0} contains non whitespace characters.")
            .OnNegateWhen(sut => sut == null, "The {0} is null, whereas it should not.")
            .OnNegateWhen(sut => sut == string.Empty, "The {0} is empty, whereas it should not.")
            .OnNegate("The {0} contains only whitespace characters, whereas it should not.")
            .EndCheck();

            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #19
0
 /// <summary>
 ///     Verify that the code results in a failed NFluent check with a specified message.
 /// </summary>
 /// <param name="check"></param>
 /// <returns>A link check</returns>
 public static ICheckLink <ICodeCheck <RunTrace> > IsAFaillingCheck(this ICodeCheck <RunTrace> check)
 {
     ExtensibilityHelper.BeginCheck(check)
     .SetSutName("fluent check")
     .CheckSutAttributes((sut) => sut.RaisedException, "raised exception")
     .FailIfNull("The fluent check did not raise an exception, where as it must.")
     .FailWhen((sut) => !ExceptionHelper.IsFailedException(sut),
               "The exception raised is not of the expected type").
     DefineExpectedType(ExceptionHelper.BuildException(string.Empty).GetType()).
     EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #20
0
        public static ICheckLink <ICheck <ReflectionWrapper> > HasHashCode(this ICheck <ReflectionWrapper> wrapper, int val)
        {
            ExtensibilityHelper.BeginCheck(wrapper).
            CheckSutAttributes(x => x.GetHashCode(), "hashcode").
            FailWhen(x => x != val,
                     "The {0} does not have the expected value.").
            OnNegate("The {0} should not have the given hashcode.").
            DefineExpectedValue(val).
            EndCheck();

            return(ExtensibilityHelper.BuildCheckLink(wrapper));
        }
예제 #21
0
        /// <summary>
        /// Checks that the actual duration is greater (strictly) than a comparand.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="providedDuration">The duration to compare to.</param>
        /// <param name="unit">The unit in which the duration is expressed.</param>
        /// <returns>A check link.</returns>
        /// <exception cref="FluentCheckException">The actual value is not greater than the provided comparand.</exception>
        public static ICheckLink <ICheck <TimeSpan> > IsGreaterThan(this ICheck <TimeSpan> check, double providedDuration, TimeUnit unit)
        {
            var expected = new Duration(providedDuration, unit);

            ExtensibilityHelper.BeginCheck(check)
            .CheckSutAttributes(sut => new Duration(sut, unit), "")
            .FailWhen(sut => sut <= expected, "The {0} is not more than the limit.")
            .OnNegate("The {0} is more than the limit.")
            .ComparingTo(expected, "more than", "less than or equal to")
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #22
0
        /// <summary>
        /// Determines whether the actual date time is close to an expected value within a given within.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="expected">The expected value.</param>
        /// <param name="within">The within.</param>
        /// <returns>A continuation check.</returns>
        public static ICheckLink <ICheck <DateTime> > IsCloseTo(this ICheck <DateTime> check, DateTime expected, Duration within)
        {
            var range = new DateTimeRangeBlock(expected, within);

            ExtensibilityHelper.BeginCheck(check).
            FailWhen((sut) => !range.IsInRange(sut), "The {0} is outside the expected value range.").
            DefineExpectedValue(range).
            OnNegate("The {0} is within the expected value range, whereas it must not.").
            EndCheck();

            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #23
0
 /// <summary>
 /// Verify that the code results in a failed NFluent assumption.
 /// </summary>
 /// <param name="check"></param>
 /// <returns>A link check</returns>
 public static ICheckLink <ICheck <RunTrace> > IsAFailingAssumption(this ICheck <RunTrace> check)
 {
     ExtensibilityHelper.BeginCheck(check)
     .SetSutName("fluent assumption")
     .CheckSutAttributes((sut) => sut.RaisedException, "raised exception")
     .FailIfNull("The assumption succeeded whereas it should have failed.")
     .FailWhen(sut => ExceptionHelper.InconclusiveExceptionType() != sut.GetTypeWithoutThrowingException(),
               "The exception raised is not of the expected type").
     DefineExpectedType(ExceptionHelper.InconclusiveExceptionType()).
     EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
예제 #24
0
 /// <summary>
 ///     Verify that the code results in a failed NFluent check with a specified message.
 /// </summary>
 /// <param name="check"></param>
 /// <returns>A link check</returns>
 public static ICheckLink <ICodeCheck <RunTrace> > IsAFaillingCheck(this ICodeCheck <RunTrace> check)
 {
     ExtensibilityHelper.BeginCheck(check)
     .GetSutProperty((sut) => sut.RaisedException, "raised exception")
     .SutNameIs("fluent check")
     .FailsIfNull()
     .FailsIf((sut) => !ExceptionHelper.IsFailedException(sut),
              "The exception raised is not of the expected type").
     ExpectingType(ExceptionHelper.BuildException(string.Empty).GetType(), "an instance of:", "an instance of a different type").
     EndCheck();
     return(ExtensibilityHelper.BuildCheckLink(check));
 }
        /// <summary>
        /// Verify that the response has a content header with a given name and value.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="expectedHeader">The name of the response header.</param>
        /// <param name="expectedValue">The value of the response header, this could be a pattern that includes an astrix ('*').</param>
        /// <returns>A check link.</returns>
        public static ICheckLink <ICheck <HttpResponseMessage?> > HasContentHeader(this ICheck <HttpResponseMessage?> check, string expectedHeader, string expectedValue)
        {
            ExtensibilityHelper.BeginCheck(check)
            .SetSutName("response")
            .FailIfNull()
#pragma warning disable CS8602 // Dereference of a possibly null reference. Justification: Null check is performed in FailIfNull statement
            .CheckSutAttributes(sut => sut.Content.GetHeaders().Select(x => new Header(x.Key, x.Value)), "content's headers")
#pragma warning restore CS8602 // Dereference of a possibly null reference.
            .HasHeaderWithValue(expectedHeader, expectedValue);

            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #26
0
        /// <summary>
        /// Checks that the event is set within a given timeout.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="timeOut">The maximum amount of time before the event should be set (time unit being specified with the timeUnit parameter).</param>
        /// <param name="timeUnit">The time unit of the given timeOut.</param>
        /// <returns>
        /// A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">The event was not set before the given timeout.</exception>
        public static ICheckLink <ICheck <EventWaitHandle> > IsSetWithin(this ICheck <EventWaitHandle> check, double timeOut, TimeUnit timeUnit)
        {
            var duration = new Duration(timeOut, timeUnit);

            ExtensibilityHelper.BeginCheck(check)
            .Expecting(duration, expectedLabel: "The given timeout:", negatedLabel: "The minimal wait time:")
            .FailsIf(sut => !sut.WaitOne((int)duration.ToMilliseconds()),
                     "The checked event has not been set before the given timeout whereas it must.", MessageOption.NoCheckedBlock)
            .Negates("The checked event has been set before the given timeout whereas it must not.", MessageOption.NoCheckedBlock)
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #27
0
        /// <summary>
        ///     Checks that the enumerable has the proper number of elements.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="expectedSize">The expected size to be found.</param>
        /// <returns>
        ///     A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">The enumerable has not the expected number of elements.</exception>
        public static ICheckLink <ICheck <IEnumerable> > HasSize(this ICheck <IEnumerable> check, long expectedSize)
        {
            long actualSize = 0;

            ExtensibilityHelper.BeginCheck(check).
            FailIfNull().
            Analyze((sut, _) => actualSize = sut.Count()).
            FailWhen(_ => actualSize != expectedSize, $"The {{0}} has {BuildElementNumberLiteral(actualSize).DoubleCurlyBraces()} instead of {expectedSize}.").
            OnNegate($"The {{0}} has {BuildElementNumberLiteral(expectedSize).DoubleCurlyBraces()} which is unexpected.").
            EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #28
0
        /// <summary>
        /// Checks that the actual duration is equal to a target duration.
        /// </summary>
        /// <param name="check">The fluent check to be extended.</param>
        /// <param name="duration">The duration to be compared to.</param>
        /// <param name="unit">The <see cref="TimeUnit" /> in which duration is expressed.</param>
        /// <returns>A check link.</returns>
        /// <exception cref="FluentCheckException">The actual value is not equal to the target duration.</exception>
        public static ICheckLink <ICheck <TimeSpan> > IsEqualTo(this ICheck <TimeSpan> check, double duration, TimeUnit unit)
        {
            var expected = new Duration(duration, unit);

            ExtensibilityHelper.BeginCheck(check)
            .CheckSutAttributes(sut => new Duration(sut, unit), "")
            .FailWhen(sut => sut != expected, "The {0} is different from the {1}.")
            .OnNegate("The {0} is the same than {1}, whereas it must not.")
            .DefineExpectedValue(expected)
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #29
0
        /// <summary>
        /// Checks if an object is an instance of a specified type.
        /// </summary>
        /// <param name="check">checker logic object</param>
        /// <param name="type">expected type</param>
        /// <typeparam name="T">type of checked object</typeparam>
        /// <returns>A check link</returns>
        public static ICheckLink <ICheck <T> > IsInstanceOfType <T>(this ICheck <T> check, Type type)
        {
            ExtensibilityHelper.BeginCheck(check).
            FailWhen(sut => sut == null && !typeof(T).IsNullable(), $"The {{checked}} is not an instance of [{type.ToStringProperlyFormatted()}].").
            Analyze((sut, test) =>
            {
                if (sut is ReflectionWrapper reflectionSut)
                {
                    var expectedWrapper = ReflectionWrapper.BuildFromType(type, reflectionSut.Criteria);
                    expectedWrapper.MapFields(reflectionSut, 1, (expected, actual, depth) =>
                    {
                        if (actual != null && expected != null)
                        {
                            if (actual.ValueType != expected.ValueType)
                            {
                                if (!actual.ValueType.IsPrimitive() && !expected.ValueType.IsPrimitive())
                                {
                                    return(true);
                                }

                                test.CheckSutAttributes(_ => actual.Value, actual.MemberLabel)
                                .Fail("The {0} is of a different type than the {1}.")
                                .DefineExpectedType(expected.ValueType);
                            }
                        }
                        else if (actual == null)
                        {
                            test.CheckSutAttributes(_ => expectedWrapper.Value, expected.MemberLabel)
                            .DefineExpectedValue(expected)
                            .Fail("The {1} is absent from the {0}.", MessageOption.NoCheckedBlock);
                        }
                        else
                        {
                            test.CheckSutAttributes(_ => actual, actual.MemberLabel.DoubleCurlyBraces())
                            .Fail("The {0} is absent from the {1}.");
                        }

                        return(false);
                    });
                }
                else
                {
                    test.FailWhen(sut2 => sut2.GetTypeWithoutThrowingException() != type,
                                  $"The {{0}} is not an instance of [{type.ToStringProperlyFormatted()}].",
                                  MessageOption.WithType);
                }
            })
            .DefineExpectedType(type)
            .OnNegate($"The {{0}} is an instance of [{type.ToStringProperlyFormatted()}] whereas it must not.", MessageOption.WithType)
            .EndCheck();
            return(ExtensibilityHelper.BuildCheckLink(check));
        }
예제 #30
0
        /// <summary>
        /// Checks that the string matches a given expression with wildcard.
        /// </summary>
        /// <param name="context">The fluent check to be extended.</param>
        /// <param name="expression">The regular expression.</param>
        /// <returns>
        ///     A check link.
        /// </returns>
        /// <exception cref="FluentCheckException">The string does not end with the expected prefix.</exception>
        public static ICheckLink <ICheck <string> > MatchesWildcards(this ICheck <string> context, string expression)
        {
            var regExp = "^" + Regex.Escape(expression).Replace("\\*", ".*").Replace("\\?", ".") + "$";

            ExtensibilityHelper.BeginCheck(context)
            .DefineExpectedValue(expression, "matches", "does not match")
            .FailIfNull()
            .FailWhen(sut => new Regex(regExp).IsMatch(sut) == false, "The {0} does not match the {1}.")
            .OnNegate("The {0} matches the {1}, whereas it must not.")
            .EndCheck();

            return(ExtensibilityHelper.BuildCheckLink(context));
        }