Example #1
0
        public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        {
            Execute.Assertion
            .BecauseOf(context.Because, context.BecauseArgs)
            .ForCondition(context.Subject is T)
            .FailWith("Expected {context:object} to be of type {0}{because}, but found {1}", typeof(T), context.Subject)
            .Then
            .Given(() => comparer.Equals((T)context.Subject, (T)context.Expectation))
            .ForCondition(isEqual => isEqual)
            .FailWith("Expected {context:object} to be equal to {1} according to {0}{because}, but {2} was not.",
                      comparer.ToString(), context.Expectation, context.Subject);

            return(true);
        }
Example #2
0
        private static string GetComparerName <T>(IEqualityComparer <T> comparer)
        {
            string comparerStr = comparer.ToString();

            if (comparerStr == comparer.GetType().ToString())
            {
                comparerStr = comparer.GetType().Name;
            }

            comparerStr = AssertUtility.FormatArgument(comparerStr);

            if (comparer == EqualityComparer <T> .Default)
            {
                comparerStr += " (default)";
            }

            return(comparerStr);
        }
        public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
        {
            if (comparands.GetExpectedType(context.Options) != typeof(T))
            {
                return(EquivalencyResult.ContinueWithNext);
            }

            Execute.Assertion
            .BecauseOf(context.Reason.FormattedMessage, context.Reason.Arguments)
            .ForCondition(comparands.Subject is T)
            .FailWith("Expected {context:object} to be of type {0}{because}, but found {1}", typeof(T), comparands.Subject)
            .Then
            .Given(() => comparer.Equals((T)comparands.Subject, (T)comparands.Expectation))
            .ForCondition(isEqual => isEqual)
            .FailWith("Expected {context:object} to be equal to {1} according to {0}{because}, but {2} was not.",
                      comparer.ToString(), comparands.Expectation, comparands.Subject);

            return(EquivalencyResult.AssertionCompleted);
        }
        /// <summary>
        /// Determines whether two sequences are equal by comparing the elements by using
        /// the given equality comparer for their type. Also true if both sequences are null.
        /// </summary>
        /// <typeparam name="T">The type of sequence to compare.</typeparam>
        /// <param name="a">The first sequence.</param>
        /// <param name="b">The second seqeunce.</param>
        /// <returns>True if the two source sequences are both null or of equal length and their corresponding
        /// elements are equal according to the given equality comparer for their type;
        /// otherwise, false.</returns>
        public static bool SequenceEqualOrNull <T>(this IEnumerable <T> a, IEnumerable <T> b, IEqualityComparer <T> comparer)
        {                // I had to comment this out and change it to .TosTring() because we are using TFS 2013 for builds. The "nameof" is C#6.0 syntax,
                         // and TFS 2013 does not build with C#6.0 syntax
                         //if (comparer == null) throw new ArgumentNullException(nameof(comparer));
            if (comparer == null)
            {
                throw new ArgumentNullException(comparer.ToString());
            }

            if (a == null && b == null)
            {
                return(true);
            }
            if (a == null || b == null)
            {
                return(false);
            }
            return(Enumerable.SequenceEqual(a, b, comparer));
        }
Example #5
0
 public override string ToString()
 {
     return(_innerComparer.ToString());
 }
        private Parameter <double> GetEntitySimulationParameter(string paramName, Entity @object, BaseSimulation simulation, IEqualityComparer <string> comparer)
        {
            string key = $"{simulation.Name}_{@object.Name}_{paramName}_{(comparer != null ? comparer.ToString() : "null")}";

            if (!SimulationEntityParametersCache.TryGetValue(key, out var result))
            {
                result = simulation.EntityParameters[@object.Name].GetParameter <Parameter <double> >(paramName, comparer);
                SimulationEntityParametersCache[key] = result;
            }

            return(result);
        }
Example #7
0
        public void DumpFile(string fileName, bool writeHeader = false)
        {
            List <string> text = new List <string>();

            if (writeHeader)
            {
                text.Add(string.Join(", ", Columns));
            }
            text.AddRange(Data.ConvertAll(row => string.Join(", ", row.ConvertAll(IEqualityComparer => IEqualityComparer.ToString()))));
            File.WriteAllLines(fileName, text);
        }