Esempio n. 1
0
        /// <summary>
        /// Will return true if the object's scope is higher than scope of roleToCompare.
        /// The lower the scope is in the enumeration, the higher is its precedence in business logic.
        /// e.g., CMS is higher than State. However, the numeric value of the enumeration is higher for State.
        /// </summary>
        /// <param name="scopeToCompare"></param>
        /// <returns></returns>
        public bool Compare(Scope scopeToCompare, ComparisonCriteria criteria)
        {
            bool result = false;

            switch (criteria)
            {
            case ComparisonCriteria.IsHigher:
                result = (this.scope < scopeToCompare);
                break;

            case ComparisonCriteria.IsHigherThanOrEqualTo:
                result = (this.scope <= scopeToCompare);
                break;

            case ComparisonCriteria.IsLower:
                result = (this.scope > scopeToCompare);
                break;

            case ComparisonCriteria.IsLowerThanOrEqualTo:
                result = (this.scope >= scopeToCompare);
                break;

            case ComparisonCriteria.IsEqual:
                result = (this.scope == scopeToCompare);
                break;

            default:
                ShiptalkCommon.ShiptalkException.ThrowSecurityException("Unspecific ComparisonCriteria.", "An error occured while execution of request. Please contact support for assistance.");
                break;
            }
            return(result);
        }
Esempio n. 2
0
        public static bool Compare <T>(this T testValue, T otherValue, ComparisonCriteria criteria)
            where T : struct, IComparable <T>
        {
            switch (criteria)
            {
            case ComparisonCriteria.Equal:
                return(testValue.CompareTo(otherValue) == 0);

            case ComparisonCriteria.NotEqual:
                return(testValue.CompareTo(otherValue) != 0);

            case ComparisonCriteria.GreaterThan:
                return(testValue.CompareTo(otherValue) > 0);

            case ComparisonCriteria.GreaterThanOrEqual:
                return(testValue.CompareTo(otherValue) >= 0);

            case ComparisonCriteria.LessThan:
                return(testValue.CompareTo(otherValue) < 0);

            case ComparisonCriteria.LessThanOrEqual:
                return(testValue.CompareTo(otherValue) <= 0);

            default:
                throw new ArgumentException("Invalicomparisocriteria", "criteria");
            }
        }
Esempio n. 3
0
        public static bool Compare <T>(this T testValue, T otherValue, ComparisonCriteria criteria, bool nullOrder)
            where T : class, IComparable <T>
        {
            switch (criteria)
            {
            case ComparisonCriteria.Equal:
                return(testValue != null ? otherValue != null?testValue.CompareTo(otherValue) == 0 : false : otherValue != null ? false : true);

            case ComparisonCriteria.NotEqual:
                return(testValue != null ? otherValue != null?testValue.CompareTo(otherValue) != 0 : true : otherValue != null ? true : false);

            case ComparisonCriteria.GreaterThan:
                return(testValue != null ? otherValue != null?testValue.CompareTo(otherValue) > 0 : !nullOrder : otherValue != null ? nullOrder : false);

            case ComparisonCriteria.GreaterThanOrEqual:
                return(testValue != null ? otherValue != null?testValue.CompareTo(otherValue) >= 0 : !nullOrder : otherValue != null ? nullOrder : true);

            case ComparisonCriteria.LessThan:
                return(testValue != null ? otherValue != null?testValue.CompareTo(otherValue) < 0 : nullOrder : otherValue != null ? !nullOrder : false);

            case ComparisonCriteria.LessThanOrEqual:
                return(testValue != null ? otherValue != null?testValue.CompareTo(otherValue) <= 0 : nullOrder : otherValue != null ? !nullOrder : true);

            default:
                throw new ArgumentException("Invalid comparison criteria", "criteria");
            }
        }
Esempio n. 4
0
        public void TestComparisonPredicateReverse(string input, ComparisonCriteria criteria)
        {
            var model      = parser.Parse(input);
            var root       = model.Goals().Where(x => x.Identifier == "test").ShallBeSingle();
            var expression = (root.FormalSpec as Exists).Enclosed as ComparisonPredicate;

            Assert.NotNull(expression);

            expression.Criteria.ShallEqual(criteria);
        }
Esempio n. 5
0
        public ComparisonPredicate(IComparer <T> comparer, T value, ComparisonCriteria criteria)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException("comparer");
            }

            _value      = value;
            _criteria   = criteria;
            _comparison = comparer.Compare;
        }
Esempio n. 6
0
        public ComparisonPredicate(Comparison <T> comparison, T value, ComparisonCriteria criteria)
        {
            if (comparison == null)
            {
                throw new ArgumentNullException("comparison");
            }

            _value      = value;
            _criteria   = criteria;
            _comparison = comparison;
        }
        public void TestComparisonPredicateBool(string input, ComparisonCriteria criteria, bool value)
        {
            var model = parser.Parse (input);
            var root = model.Goals().Where (x => x.Identifier == "test").ShallBeSingle ();
            var expression = (root.FormalSpec as Exists).Enclosed as ComparisonPredicate;

            Assert.NotNull (expression);

            expression.Criteria.ShallEqual (criteria);
            (expression.Right as BoolConstant).Value.ShallEqual (value);
        }
        /// <summary>
        /// Creates the attribute
        /// </summary>
        /// <param name="otherProperty">The other property to compare to</param>
        /// <param name="criteria">The <see cref="ComparisonCriteria"/> to use when comparing</param>
        /// <exception cref="ArgumentNullException"><paramref name="otherProperty"/> is <see langword="null" />.</exception>
        public CompareValuesAttribute(string otherProperty, ComparisonCriteria criteria)
            : base("{0} must be {1} {2}")
        {
            if (otherProperty == null)
            {
                throw new ArgumentNullException(nameof(otherProperty));
            }

            OtherProperty = otherProperty;
            Criteria = criteria;
        }
Esempio n. 9
0
        public void TestComparisonPredicateNumeric(string input, ComparisonCriteria criteria, double value)
        {
            var model      = parser.Parse(input);
            var root       = model.Goals().Where(x => x.Identifier == "test").ShallBeSingle();
            var expression = (root.FormalSpec as Exists).Enclosed as ComparisonPredicate;

            Assert.NotNull(expression);

            expression.Criteria.ShallEqual(criteria);
            (expression.Right as NumericConstant).Value.ShallEqual(value);
        }
Esempio n. 10
0
        public void TestComparisonPredicateOtherPredicate(string input, ComparisonCriteria criteria)
        {
            var model      = parser.Parse(input);
            var root       = model.Goals().Where(x => x.Identifier == "test").ShallBeSingle();
            var expression = (root.FormalSpec as Exists).Enclosed as ComparisonPredicate;

            Assert.NotNull(expression);

            expression.Criteria.ShallEqual(criteria);
            (expression.Right as PredicateReference).PredicateIdentifier.ShallEqual("predicate");
            (expression.Right as PredicateReference).ActualArguments.ShallOnlyContain(new string[] { "arg1" });
        }
        Expression VisitRange(Comparison rangeComparison, Expression left, Expression right)
        {
            var existingCriteriaExpression = left as CriteriaExpression;

            if (existingCriteriaExpression == null)
            {
                var inverted = left is ConstantExpression;

                var cm = ConstantMemberPair.Create(left, right);

                if (cm == null)
                {
                    throw new NotSupportedException("A {0} must test a constant against a member");
                }

                var field = Mapping.GetFieldName(SourceType, cm.MemberExpression);
                var comparisonCriteria = new ComparisonCriteria(field, cm.MemberExpression.Member, rangeComparison, cm.ConstantExpression.Value);

                return(new CriteriaExpression(inverted ? comparisonCriteria.Negate() : comparisonCriteria));
            }
            else
            {
                var distanceCriteria = existingCriteriaExpression.Criteria as DistanceCriteria;

                if (distanceCriteria != null)
                {
                    var constantExpression = right as ConstantExpression;

                    if (constantExpression == null)
                    {
                        throw new NotSupportedException($"A {right} must test a constant against a member");
                    }

                    distanceCriteria.ReplaceComparison(rangeComparison, constantExpression.Value);
                }

                return(existingCriteriaExpression);
            }
        }
        public void GetValidationResult_OneNullValue_IsInvalid(ComparisonCriteria criteria)
        {
            ComparisonEntity entity = CreateComparisonEntity(min: 1);
            ValidationContext validationContext = new ValidationContext(entity) { MemberName = "Minimum" };
            CompareValuesAttribute attribute = new CompareValuesAttribute("Maximum", criteria);

            // Act
            ValidationResult result = attribute.GetValidationResult(null, validationContext);

            Assert.That(result, Is.Not.EqualTo(ValidationResult.Success));
        }
        public void CompareValues_MinimumComparedToMaximumWithInvalidValues_IsInvalid(
            ComparisonCriteria criteria, int minimum, int maximum)
        {
            ComparisonEntity entity = CreateComparisonEntity(minimum, maximum);
            ValidationContext validationContext = new ValidationContext(entity) { MemberName = "Minimum" };
            CompareValuesAttribute attribute = new CompareValuesAttribute("Maximum", criteria);

            // Act
            ValidationResult result = attribute.GetValidationResult(entity.Minimum, validationContext);

            Assert.That(result, Is.Not.EqualTo(ValidationResult.Success));
        }
        public void TestComparisonPredicateOtherPredicate(string input, ComparisonCriteria criteria)
        {
            var model = parser.Parse (input);
            var root = model.Goals().Where (x => x.Identifier == "test").ShallBeSingle ();
            var expression = (root.FormalSpec as Exists).Enclosed as ComparisonPredicate;

            Assert.NotNull (expression);

            expression.Criteria.ShallEqual (criteria);
            (expression.Right as PredicateReference).Predicate.Name.ShallEqual ("Predicate");
            (expression.Right as PredicateReference).ActualArguments.ShallOnlyContain (new string[] { "arg1" });
        }
Esempio n. 15
0
 /// <summary>
 /// Will return true if the object's scope is higher than scope of roleToCompare.
 /// The lower the scope is in the enumeration, the higher is its precedence in business logic.
 /// e.g., CMS is higher than State. However, the numeric value of the enumeration is higher for State.
 /// </summary>
 /// <param name="scopeToCompare"></param>
 /// <returns></returns>
 public bool Compare(Role roleToCompare, ComparisonCriteria criteria)
 {
     return(Compare(roleToCompare.scope, criteria));
 }
 private void Build(ComparisonCriteria criteria)
 {
     SearchRequest.SearchParameters.Filter = criteria.ToString();
 }
        public void TestComparisonVariable(string input, ComparisonCriteria criteria)
        {
            var model = parser.Parse (input);
            var root = model.Goals().Where (x => x.Identifier == "test").ShallBeSingle ();
            var expression = (root.FormalSpec as Exists).Enclosed as ComparisonPredicate;

            Assert.NotNull (expression);

            expression.Criteria.ShallEqual (criteria);
            (expression.Left as VariableReference).Name.ShallEqual ("arg1");
            (expression.Right as VariableReference).Name.ShallEqual ("arg2");
        }
        /// <summary>
        ///     Gets the description value for the supplied ComparisonCriteria value
        ///     or if it doesn't have a description, its string representation
        /// </summary>
        /// <param name="value">The <see cref="ComparisonCriteria"/> value</param>
        /// <returns>The values description</returns>
        private static string GetCriteriaDescription(ComparisonCriteria value)
        {
            DescriptionAttribute attribute =
                (DescriptionAttribute) value.GetType().GetField(value.ToString()).
                    GetCustomAttributes(typeof (DescriptionAttribute)).FirstOrDefault();

            if (attribute != null)
            {
                return attribute.Description;
            }

            return value.ToString();
        }