Exemple #1
0
        public bool IsSatisfiedBy(T candidate, out SpecificationResult result)
        {
            if (candidate == null)
            {
                result = new SpecificationResult(false,
                                                 CreateTraceMessage("", false),
                                                 new FailedSpecification(GetType(), GetParameters(), (object)null, "Collection is null"));
                return(false);
            }

            var overall = OverallForEmpty;
            var failedSpecifications = new List <FailedSpecification>();
            var traces = new List <string>();

            var idx   = 0;
            var total = 1;

            foreach (var el in candidate)
            {
                var specOverall = CollectionElementSpecification.IsSatisfiedBy(el, out var specResult);

                total += specResult.TotalSpecificationsCount;
                if (!string.IsNullOrEmpty(specResult.Trace))
                {
                    traces.Add($"[{idx}]({specResult.Trace})");
                }

                if (!CanContinue(specOverall, ref overall))
                {
                    break;
                }

                if (!specOverall)
                {
                    var failures = CreateFailedSpecifications(specResult.FailedSpecifications, idx);
                    failedSpecifications.AddRange(failures);
                }

                idx++;
            }

            if (!overall)
            {
                failedSpecifications.Insert(0, new FailedSpecification(
                                                GetType(),
                                                GetParameters(),
                                                candidate,
                                                CreateFailedMessage()));
            }
            else
            {
                failedSpecifications.Clear();
            }

            var trace = CreateTraceMessage(string.Join($" {TraceConnector} ", traces), overall);

            result = new SpecificationResult(total, overall, trace, failedSpecifications.ToArray());

            return(overall);
        }
Exemple #2
0
        public Expression <Func <T, bool> > GetExpression()
        {
            var collectionExpression = CollectionElementSpecification.GetExpression();
            var arg = Expression.Parameter(typeof(T), "candidate");

            var checkNullExpression = Expression.NotEqual(arg, Expression.Constant(null, typeof(T)));
            var invokeAnyExpression = Expression.Call(
                CollectionElementCallMethodInfo, arg, collectionExpression);

            var andExpression = Expression.AndAlso(
                checkNullExpression, invokeAnyExpression);

            return(Expression.Lambda <Func <T, bool> >(
                       !typeof(T).GetTypeInfo().IsValueType&& _hasCheckNullExpression
                    ? (Expression)andExpression
                    : invokeAnyExpression,
                       arg));
        }
Exemple #3
0
 public override bool IsSatisfiedBy(T candidate)
 {
     return(candidate != null && candidate.All(c => CollectionElementSpecification.IsSatisfiedBy(c)));
 }