Ejemplo n.º 1
0
        /// <summary>
        /// Apply the item constraint to each item in the collection,
        /// succeeding only if the expected number of items pass.
        /// </summary>
        /// <param name="actual">The value to be tested</param>
        /// <returns>A ConstraintResult</returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            var enumerable = ConstraintUtils.RequireActual <IEnumerable>(actual, nameof(actual));
            var itemList   = new Collection <object>();
            var matchCount = 0;

            foreach (var item in enumerable)
            {
                if (_itemConstraint != null)
                {
                    if (_itemConstraint.ApplyTo(item).IsSuccess)
                    {
                        matchCount++;
                    }
                }
                else
                {
                    matchCount++;
                }

                // We intentionally add one item too many because we use it to trigger
                // the ellipsis when we call "MsgUtils.FormatCollection" later on.
                if (itemList.Count <= MsgUtils.DefaultMaxItems)
                {
                    itemList.Add(item);
                }
            }

            return(new ExactCountConstraintResult(this, actual, matchCount == _expectedCount, matchCount, itemList));
        }
Ejemplo n.º 2
0
            public override ConstraintResult ApplyTo <TActual>(TActual actual)
            {
                var instCheck = Is.InstanceOf <HttpRequestMessage>().ApplyTo(actual);

                if (!instCheck.IsSuccess)
                {
                    return(instCheck);
                }

                var message = actual as HttpRequestMessage;

                var results = new List <ConstraintResult>();

                if (uri != null)
                {
                    results.Add(uri.ApplyTo <Uri>(message.RequestUri));
                }
                if (method != null)
                {
                    results.Add(method.ApplyTo <HttpMethod>(message.Method));
                }
                if (content != null)
                {
                    results.Add(content.ApplyTo <HttpContent>(message.Content));
                }

                return(new CombinedResult(this, actual, results));
            }
Ejemplo n.º 3
0
        /// <summary>
        /// Apply the item constraint to each item in the collection,
        /// succeeding only if the expected number of items pass.
        /// </summary>
        /// <param name="actual"></param>
        /// <returns></returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            var enumerable = ConstraintUtils.RequireActual <IEnumerable>(actual, nameof(actual));

            int count = 0;

            if (_itemConstraint == null)
            {
                foreach (object item in enumerable)
                {
                    count++;
                }
            }
            else
            {
                foreach (object item in enumerable)
                {
                    if (_itemConstraint.ApplyTo(item).IsSuccess)
                    {
                        count++;
                    }
                }
            }

            return(new ConstraintResult(this, actual, count == _expectedCount));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Apply the item constraint to each item in the collection,
        /// succeeding only if the expected number of items pass.
        /// </summary>
        /// <param name="actual"></param>
        /// <returns></returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            if (!(actual is IEnumerable))
            {
                throw new ArgumentException("The actual value must be an IEnumerable", "actual");
            }

            int count = 0;

            if (_itemConstraint == null)
            {
                foreach (object item in (IEnumerable)actual)
                {
                    count++;
                }
            }
            else
            {
                foreach (object item in (IEnumerable)actual)
                {
                    if (_itemConstraint.ApplyTo(item).IsSuccess)
                    {
                        count++;
                    }
                }
            }

            return(new ConstraintResult(this, actual, count == _expectedCount));
        }
Ejemplo n.º 5
0
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            writer.WriteLine($"writing {actual}");
            var result = constraint.ApplyTo(actual);

            writer.WriteLine("status ", result.Status);
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sets an expectation on a value produced off the provided parameter value.
        /// </summary>
        /// <typeparam name="T">The type of the parameter used to produce the test value.</typeparam>
        /// <typeparam name="TValue">The type of the value to test.</typeparam>
        /// <param name="parameter">The parameter.</param>
        /// <param name="valueSelector"></param>
        /// <param name="constraint">A NUnit constraint used to set the expectation.</param>
        /// <returns>The parameter value.</returns>
        public static T That <T, TValue>(this Parameter <T> parameter, Func <T, Task <TValue> > valueSelector, IConstraint constraint)
        {
            _ = parameter ?? throw new ArgumentNullException(nameof(parameter));

            return(Moq.Match.Create <T>(item =>
            {
                var value = valueSelector(item).GetAwaiter().GetResult();
                return constraint.ApplyTo(value).IsSuccess;
            }));
        }
        /// <inheritdoc />
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            if (actual is T item)
            {
                var value = _valueSelector(item);
                return(_constraint.ApplyTo(value));
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Apply a constraint to a referenced value, succeeding if the constraint
        /// is satisfied and throwing an assertion exception on failure.
        /// </summary>
        /// <param name="actual">The actual value to test</param>
        /// <param name="expression">A Constraint to be applied</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That <T>(ref T actual, IResolveConstraint expression, string message = null, params object[] args)
        {
            IConstraint constraint = expression.Resolve();

            IncrementAssertCount();
            var result = constraint.ApplyTo(ref actual);

            if (!result.IsSuccess)
            {
                ReportFailure(result, message, args);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Apply a constraint to a <see cref="TCLite.ActualValueDelegate"/>,
        /// succeeding if the constraint is satisfied and throwing an
        /// assertion exception on failure.
        /// </summary>
        /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
        /// <param name="expr">A Constraint expression to be applied</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That <T>(ActualValueDelegate <T> del, IResolveConstraint expr, string message = null, params object[] args)
        {
            IConstraint constraint = expr.Resolve();

            IncrementAssertCount();
            var result = constraint.ApplyTo(del);

            if (!result.IsSuccess)
            {
                ReportFailure(result, message, args);
            }
        }
Ejemplo n.º 10
0
        /// <inheritdoc cref="Assert.That{T}(T,IResolveConstraint,string,object[])"/>
        /// <remarks> Delegating to a different name to emphasize that behavior of other Ensure overloads may differ in behavior to Assert.That, and to reduce the number of completion options Intellisense pops up with.</remarks>
        public static void Ensure <T>(T actual, IConstraint constraint, string message = null, params object[] args)
        {
            constraint = constraint.Resolve();

            TestExecutionContext.CurrentContext.IncrementAssertCount();
            var result = constraint.ApplyTo(actual);

            if (!result.IsSuccess)
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                result.WriteMessageTo(writer);
                // If an exception was thrown its stack trace should be displayed, not the stack trace of this assertion exception.
                throw new AssertionExceptionWithTrimmedStackTrace(writer.ToString(), (result.ActualValue as Exception)?.StackTrace);
            }
        }
Ejemplo n.º 11
0
            public override ConstraintResult ApplyTo <TActual>(TActual actual)
            {
                var instCheck = Is.InstanceOf <HttpContent>().ApplyTo(actual);

                if (!instCheck.IsSuccess)
                {
                    return(instCheck);
                }

                var httpContent = actual as HttpContent;

                if (content != null)
                {
                    Task <string> task = httpContent.ReadAsStringAsync();
                    task.Wait();
                    return(new CombinedResult(this, actual, new List <ConstraintResult> {
                        content.ApplyTo(task.Result)
                    }));
                }
                return(new ConstraintResult(this, actual, true));
            }
Ejemplo n.º 12
0
            public override ConstraintResult ApplyTo <TActual>(TActual actual)
            {
                var instCheck = Is.InstanceOf <Uri>().ApplyTo(actual);

                if (!instCheck.IsSuccess)
                {
                    return(instCheck);
                }

                var uri = actual as Uri;

                var results = new List <ConstraintResult>();

                if (path != null)
                {
                    results.Add(path.ApplyTo <string>(uri.AbsolutePath));
                }
                if (query != null)
                {
                    results.Add(query.ApplyTo <string>(uri.Query));
                }

                return(new CombinedResult(this, actual, results));
            }
            public override ConstraintResult ApplyTo <TActual>(TActual actual)
            {
                var constraintResult = _inner.ApplyTo(actual);

                return(constraintResult);
            }
Ejemplo n.º 14
0
        /// <summary>
        /// Sets an expectation on the value of the parameter being used.
        /// </summary>
        /// <typeparam name="T">The type of the parameter.</typeparam>
        /// <param name="parameter">The parameter.</param>
        /// <param name="constraint">A NUnit constraint used to set the expectation.</param>
        /// <returns>The parameter value.</returns>
        public static T That <T>(this Parameter <T> parameter, IConstraint constraint)
        {
            _ = parameter ?? throw new ArgumentNullException(nameof(parameter));

            return(Moq.Match.Create <T>(item => constraint.ApplyTo(item).IsSuccess));
        }