public bool Compare(ComparisonContext context, ValueComparison valueComparison)
        {
            bool areEqual;
            var options = context.Rules.GetOptions<FloatValueComparatorOptions>(valueComparison);

            if (valueComparison.PropertyInfo.PropertyType == typeof(float))
            {
                areEqual = this.CompareFloats(
                    (float)valueComparison.ExpectedValue,
                    (float)valueComparison.ActualValue,
                    options.FloatEpsilon);
            }
            else
            {
                areEqual = this.CompareDoubles(
                    (double)valueComparison.ExpectedValue,
                    (double)valueComparison.ActualValue,
                    options.DoubleEpsilon);
            }

            if (!areEqual)
            {
                context.AddDifference(valueComparison);
            }

            return areEqual;
        }
 /// <summary>
 /// Initializes new instance of the <see cref="ValueComparisonAttribute"/> class.
 /// </summary>
 /// <param name="otherProperty">The name of the other property.</param>
 /// <param name="comparison">The <see cref="ValueComparison"/> to perform between values.</param>
 public ValueComparisonAttribute(string propertyName, ValueComparison comparison)
     : base(propertyName)
 {
     this.comparison = comparison;
     this.failure = new ValidationResult(String.Empty);
     this.success = ValidationResult.Success;
 }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            if (comparison.PropertyType == null)
            {
                throw new ArgumentException("Could not determine type to reflect.", "comparison");
            }

            var areEqual = true;
            var properties = comparison.PropertyType
               .GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var property in properties)
            {
                var propertyPathItem = new PropertyPathItem(property, comparison.PropertyPathItem);

                var expectedPropValue = property.GetValue(comparison.ExpectedValue, null);
                var actualPropValue = property.GetValue(comparison.ActualValue, null);

                if (!context.CompareItem(expectedPropValue, actualPropValue, propertyPathItem))
                {
                    areEqual = false;
                }
            }

            return areEqual;
        }
        private bool CompareArrayItems(ComparisonContext context, ValueComparison comparison, int currentDimension, int[] currentIndices)
        {
            var actual = (Array)comparison.ActualValue;
            var expected = (Array)comparison.ExpectedValue;

            var length = expected.GetLength(currentDimension);

            var areEqual = true;
            for (var i = 0; i < length; i++)
            {
                currentIndices[currentDimension] = i;
                if (currentDimension == currentIndices.Length - 1)
                {
                    // Last dimension, get value and compare
                    var actualValue = actual.GetValue(currentIndices);
                    var expectedValue = expected.GetValue(currentIndices);

                    var indicesString = this.GetArrayIndicesString(currentIndices);
                    var propertyPath = new PropertyPathItem(indicesString, comparison.PropertyPathItem);
                    if (!context.CompareItem(expectedValue, actualValue, propertyPath))
                    {
                        areEqual = false;
                    }
                }
                else
                {
                    if (!this.CompareArrayItems(context, comparison, currentDimension + 1, currentIndices))
                    {
                        areEqual = false;
                    }
                }
            }

            return areEqual;
        }
        public bool Compare(ComparisonContext comparisonContext, ValueComparison valueComparison)
        {
            var actual = (IDictionary)valueComparison.ActualValue;
            var expected = (IDictionary)valueComparison.ExpectedValue;

            if (!this.AreKeysEqual(expected, actual, comparisonContext, valueComparison))
            {
                return false;
            }

            foreach (var key in expected.Keys)
            {
                var actualValue = actual[key];
                var expectedValue = expected[key];

                // TODO: Why is this immediately picked up by the recursive comparator
                // TODO: Create a valid use case test for the recursive comparator
                // TODO: Both values null in the dictionary
                // TODO: Comple objects in the dictionary

                var propertyPath = new PropertyPathItem(key.ToString(), valueComparison.PropertyPathItem);
                comparisonContext.CompareItem(expectedValue, actualValue, propertyPath);
            }

            return true;
        }
        public DateCompareAttribute(ValueComparison comparison)
        {
            if (!Enum.IsDefined(typeof(ValueComparison), comparison))
                throw new ArgumentException("Undefined value.", "comparison");

            this.comparison = comparison;
            this.compareProperty = "";
        }
        /// <summary>
        /// Initializes new instance of the <see cref="DateTimeCompareAttribute"/> class.
        /// </summary>
        /// <param name="otherProperty">The name of the other property.</param>
        /// <param name="comparison">The <see cref="ValueComparison"/> to perform between values.</param>
        public DateTimeCompareAttribute(string otherProperty, ValueComparison comparison)
            : base(otherProperty)
        {
            if (!Enum.IsDefined(typeof(ValueComparison), comparison))
                throw new ArgumentException("Undefined value.", "comparison");

            this.comparison = comparison;
            this.success = ValidationResult.Success;
            this.failure = new ValidationResult(String.Empty);
        }
        public void ThenShouldReturnFalseIfValuesAreNotRefEqual()
        {
            var actual = "AAA";
            var expected = "BBB";

            var comparisonA = new ValueComparison(propPath, expected.EnsureUniqueInstance(), actual);
            var comparisonB = new ValueComparison(propPath, expected.EnsureUniqueInstance(), actual);

            comparisonB.Equals(comparisonA).Should().BeFalse();
        }
        public void ThenShouldReturnTrueIfValuesAreRefEqual()
        {
            var actual = "AAA";
            var expected = "BBB";

            var comparisonA = new ValueComparison(propPath, expected, actual);
            var comparisonB = new ValueComparison(propPath, expected, actual);

            comparisonB.Equals(comparisonA).Should().BeTrue();
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areEqual = object.Equals(comparison.ExpectedValue, comparison.ActualValue);
            if (!areEqual)
            {
                context.AddDifference(comparison);
            }

            return areEqual;
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areEqual = ReferenceEquals(comparison.ExpectedValue, comparison.ActualValue);
            if (!areEqual)
            {
                comparison.Message = "The instances are different, reference equals failed.";
                context.AddDifference(comparison);
            }

            return areEqual;
        }
        private ValueComparison CreateKeyNamedComparison(ValueComparison comparison, string key, string messageFormat)
        {
            var itemPropPath = new PropertyPathItem(key, comparison.PropertyPathItem);

            var result = new ValueComparison(
                itemPropPath,
                comparison.ExpectedValue,
                comparison.ActualValue);

            result.Message = string.Format(messageFormat, key);
            return result;
        }
        public bool Compare(ComparisonContext comparisonContext, ValueComparison valueComparison)
        {
            var expected = (IComparable)valueComparison.ExpectedValue;
            var actual = (IComparable)valueComparison.ActualValue;

            var areEqual = expected.CompareTo(actual) == 0;
            if (!areEqual)
            {
                comparisonContext.AddDifference(valueComparison);
            }

            return areEqual;
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var maxDimentions = comparison.PropertyType.GetArrayRank();
            var currentIndices = new int[maxDimentions];

            if (!this.CompareArrayDimensionLengths(comparison, maxDimentions))
            {
                context.AddDifference(comparison);
                return false;
            }

            return this.CompareArrayItems(context, comparison, 0, currentIndices);
        }
Esempio n. 15
0
        public void ThenLogEventShouldHavePropertiesSet()
        {
            var context = new ValueComparison(null, null, null);

            var logger = new Logger();
            logger.Callback(x =>
            {
                x.Message.Should().Be("Hello, world!");
                x.Level.Should().Be(LogLevel.Error);
                x.Comparison.Should().NotBeNull();
            });

            logger.Log(LogLevel.Error, context, "Hello, {0}!", "world");
        }
        public bool Compare(ComparisonContext context, ValueComparison valueComparison)
        {
            var options = context.Rules.GetOptions<StringValueComparatorOptions>(valueComparison);
            var areEqual = string.Equals(
                (string)valueComparison.ExpectedValue,
                (string)valueComparison.ActualValue,
                options.ComparisonType);

            if (!areEqual)
            {
                context.AddDifference(valueComparison);
            }

            return areEqual;
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areSame = this.customCompare.Invoke(comparison, context);
            if (!areSame)
            {
                if (string.IsNullOrEmpty(comparison.Message))
                {
                    comparison.Message = "Custom comparator returned false";
                }

                context.AddDifference(comparison);
            }

            return areSame;
        }
        public bool Compare(ComparisonContext context, ValueComparison comparison)
        {
            var areEqual = true;
            var options = context.Rules.GetOptions<CollectionValueComparatorOptions>(comparison);

            var actual = (IEnumerable)comparison.ActualValue;
            var expected = (IEnumerable)comparison.ExpectedValue;

            if (options.OrderFunction != null)
            {
                actual = options.OrderFunction.Invoke(actual);
                expected = options.OrderFunction.Invoke(expected);
            }

            var actualEnumerator = actual.GetEnumerator();
            var expectedEnumerator = expected.GetEnumerator();

            var actualHasNext = actualEnumerator.MoveNext();
            var expectedHasNext = expectedEnumerator.MoveNext();

            var itemCounter = 0;
            while (actualHasNext && expectedHasNext)
            {
                var propertyPath = new PropertyPathItem(itemCounter.ToString(), comparison.PropertyPathItem);
                if (!context.CompareItem(expectedEnumerator.Current, actualEnumerator.Current, propertyPath))
                {
                    areEqual = false;
                }

                expectedHasNext = expectedEnumerator.MoveNext();
                actualHasNext = actualEnumerator.MoveNext();
                itemCounter++;
            }

            if (actualHasNext != expectedHasNext)
            {
                comparison.Message = string.Format(
                    "The collections have a different number of items, expected: {0}, actual: {1}.",
                    expected.Cast<object>().Count(),
                    actual.Cast<object>().Count());

                context.AddDifference(comparison);
                areEqual = false;
            }

            return areEqual;
        }
Esempio n. 19
0
        public bool IsMatch(ValueComparison valueComparison)
        {
            if (valueComparison.PropertyPathItem.IsRoot)
            {
                return this.AppliesToRoot;
            }

            if (this.PropertyType != null)
            {
                var propType = valueComparison.PropertyType;
                if (this.IncludeInheritedTypes 
                    && !propType.IsSubclassOf(this.PropertyType) 
                    && propType != this.PropertyType)
                {
                    return false;
                }

                if (!this.IncludeInheritedTypes && propType != this.PropertyType)
                {
                    return false;
                }
            }

            if (this.DeclarationType != null && valueComparison.PropertyInfo != null)
            {
                if (valueComparison.PropertyInfo.DeclaringType != this.DeclarationType)
                {
                    return false;
                }
            }
            
            if (this.Predicate != null && !this.Predicate.Invoke(valueComparison))
            {
                return false;
            }

            if (this.PropertyExpression != null)
            {
                var propertyNames = this.PropertyExpression.GetPropertyNames();
                if (!this.IsPropertyPathMatch(valueComparison.PropertyPathItem, propertyNames))
                {
                    return false;
                }
            }

            return true;
        }
        private bool CompareArrayDimensionLengths(ValueComparison comparison, int maxDimensions)
        {
            var actual = (Array)comparison.ActualValue;
            var expected = (Array)comparison.ExpectedValue;

            for (var i = 0; i < maxDimensions; i++)
            {
                var actualLength = actual.GetLength(i);
                var expectedLength = expected.GetLength(i);

                if (actualLength != expectedLength)
                {
                    comparison.Message = string.Format("The arrays have different lengths at the dimension {0}", i);
                    return false;
                }
            }

            return true;
        }
        private bool AreKeysEqual(IDictionary expected, IDictionary actual, ComparisonContext comparisonContext, ValueComparison valueComparison)
        {
            var notInExpected = actual.Keys
                .Cast<object>()
                .Where(x => !expected.Contains(x));

            var notInActual = expected.Keys
                .Cast<object>()
                .Where(x => !actual.Contains(x));

            var result = true;

            foreach (var key in notInActual)
            {
                var keyComparison = this.CreateKeyNamedComparison(
                    valueComparison,
                    key.ToString(),
                    "The dictionaries differ, the key '{0}' is missing in the actual value.");
                
                comparisonContext.AddDifference(keyComparison);
                result = false;
            }

            foreach (var key in notInExpected)
            {        
                var keyComparison = this.CreateKeyNamedComparison(
                    valueComparison, 
                    key.ToString(), 
                    "The dictionaries differ, there is an extra key '{0}' in the actual value.");

                comparisonContext.AddDifference(keyComparison);
                result = false;
            }

            return result;
        }
Esempio n. 22
0
 private static Func<float, bool> getComparison(
     ValueComparison comparison, float value)
 {
     switch(comparison)
     {
         case ValueComparison.GreaterThan:
             return x => x > value;
         case ValueComparison.LessThan:
             return x => x < value;
         case ValueComparison.NotEqualTo:
             return x => x != value;
         default:
         case ValueComparison.EqualTo:
             return x => x == value;
     }
 }
Esempio n. 23
0
 public abstract bool IsMatch(ValueComparison valueComparison);
Esempio n. 24
0
 /// ---------------------------------------------------------------------------
 /// <summary>Parses values for enum</summary>
 /// ---------------------------------------------------------------------------
 static public string ParseType(ValueComparison value)
 {
     return ParseType(value.GetType().ToString(), value.ToString());
 }
Esempio n. 25
0
        //---------------------------------------------------------------------------
        // Helper for CompareEndpoints() test cases
        //---------------------------------------------------------------------------
        internal void CompareEndpointsHelper(SampleText sampleText, TargetRangeType callingRangeType, TargetRangeType argumentRangeType, ValueComparison startStart, ValueComparison startEnd, ValueComparison endStart, ValueComparison endEnd, Type expectedException)
        {
            TextPatternRange callingRange = null;
            TextPatternRange argumentRange = null;

            // Pre-Condition Verify text is expected value <<sampleText>> 
            TS_SetText(sampleText, CheckType.IncorrectElementConfiguration);

            // Pre-Condition Create calling range = <<callingRangeType>>
            TS_CreateRange(out callingRange, callingRangeType, null, true, CheckType.Verification);

            // Pre-Condition Create argument range = <<argumentRangeType>>
            TS_CreateRange(out argumentRange, argumentRangeType, callingRange, true, CheckType.Verification);

            // Verify CompareEndPoints(start,start) <<startStart>> zero
            TS_CompareEndpoints(callingRange, TextPatternRangeEndpoint.Start, argumentRange, 
                                              TextPatternRangeEndpoint.Start, startStart, expectedException, CheckType.Verification);

            // Verify CompareEndPoints(start,  end) <<startEnd>> zero
            TS_CompareEndpoints(callingRange, TextPatternRangeEndpoint.Start, argumentRange,
                                              TextPatternRangeEndpoint.End,   startEnd, expectedException, CheckType.Verification);

            // Verify CompareEndPoints(  end,start) <<endStart>> zero
            TS_CompareEndpoints(callingRange, TextPatternRangeEndpoint.End,   argumentRange,
                                              TextPatternRangeEndpoint.Start, endStart, expectedException, CheckType.Verification);

            // Verify CompareEndPoints(  end,  end) <<endEnd>> zero
            TS_CompareEndpoints(callingRange, TextPatternRangeEndpoint.End,   argumentRange, 
                                              TextPatternRangeEndpoint.End,   endEnd, expectedException, CheckType.Verification);
        }
Esempio n. 26
0
        static internal void IsMatchingValue(int actual, ValueComparison valueComparison, int expected, string msg, CheckType checkType)
        {
            bool result = false;

            Comment("Expecting " + actual + " " + valueComparison.ToString() + " " +
                        expected);

            switch (valueComparison)
            {
                case ValueComparison.LessThan:
                    result = (actual < expected);
                    break;
                case ValueComparison.Equals:
                    result = (actual == expected);
                    break;
                case ValueComparison.GreaterThan:
                    result = (actual > expected);
                    break;
                default:
                    throw new ArgumentException("IsMatchingValue() has no support for " + ParseType(valueComparison));
            }

            if (result == true)
                Comment(msg + " results matched as expected, ValueComparison = " + Parse(valueComparison));
            else
            {
                Comment("Evaluated " + actual + " " + valueComparison + " " + expected + " as " + result);
                ThrowMe(checkType, msg + " results were not as expected ");
            }
        }
Esempio n. 27
0
        //---------------------------------------------------------------------------
        // TestStep for TextPatternRange.CompareEndpoints Method
        //---------------------------------------------------------------------------
        internal void TS_CompareEndpoints(TextPatternRange callingRange, TextPatternRangeEndpoint endPoint, TextPatternRange argumentRange, TextPatternRangeEndpoint targetEndPoint, ValueComparison valueComparison, Type expectedException, CheckType checkType)
        {
            int endPointOffset = 0;
            string msg = endPoint.ToString() + "/" + targetEndPoint.ToString();

            Range_CompareEndpoints(callingRange, endPoint,
                        argumentRange, targetEndPoint, ref endPointOffset, expectedException, checkType);

            IsMatchingValue(endPointOffset, valueComparison, 0, msg, checkType);
            m_TestStep++;
        }
 public override bool IsMatch(ValueComparison valueComparison)
 {
     return this.Selection.IsMatch(valueComparison);
 }
Esempio n. 29
0
 private string getValueComparisonString(ValueComparison comparison)
 {
     string value = "";
     switch (comparison)
     {
         case ValueComparison.IsEqual:
             value = "=";
             break;
         case ValueComparison.IsNotEqual:
             value = "!=";
             break;
         case ValueComparison.IsGreaterThan:
             value = ">";
             break;
         case ValueComparison.IsGreaterThanOrEqual:
             value = ">=";
             break;
         case ValueComparison.IsLessThan:
             value = "<";
             break;
         case ValueComparison.IsLessThanOrEqual:
             value = "<=";
             break;
     }
     return value;
 }
Esempio n. 30
0
 static public string Parse(ValueComparison value)
 {
     switch (value)
     {
         case ValueComparison.LessThan: return "Less than";
         case ValueComparison.Equals: return "Equals";
         case ValueComparison.GreaterThan: return "Greater Than";
         default:
             throw new ArgumentException("Parse() has no support for " + ParseType(value));
     }
 }