public bool ShouldBuildFunctionComparingCollectionsElementWiseIgnoringOrder(Account a, Account b)
        {
            var equals = new EqualityBuilder <Account>()
                         .AppendPublicFields()
                         .AppendPublicProperties()
                         .CompareCollectionsIgnoringOrder()
                         .Build();

            return(equals(a, b));
        }
        public bool ShouldOverrideComparisonFunctionForMember(Account a, Account b)
        {
            var equals = new EqualityBuilder <Account>()
                         .AppendPublicFields()
                         .AppendPublicProperties()
                         .Append(ac => ac.Phones, (ap, bp) => ap.FirstOrDefault() == bp.FirstOrDefault())
                         .Build();

            return(equals(a, b));
        }
        public bool ShouldUseProvidedComparisonFunctionForHierarchy(Account a, Account b)
        {
            var equals = new EqualityBuilder <Account>()
                         .AppendPublicFields()
                         .AppendPublicProperties()
                         .Use <int>((ai, bi) => Math.Abs(ai - bi) < 10)
                         .Use <string[]>((ar, br) => ar.Length == br.Length)
                         .Build();

            return(equals(a, b));
        }
Ejemplo n.º 4
0
        PersistenceTestResult IConfiguresComparison <T> .WithEqualityComparer(IEqualityComparer <T> comparer)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }

            var rule = new EqualityBuilder <T>()
                       .For("Default", x => x, c => c.UsingComparer(comparer))
                       .Build();

            return(WithEqualityRule(rule));
        }
Ejemplo n.º 5
0
        public void Build_ReturnsAnEqualityComparerWhichTestsUsingAComparerType_AndDetectsInequalObjectsAccordingly(SampleClass one, SampleClass two)
        {
            var comparer = new EqualityBuilder <SampleClass>()
                           .ForProperty(x => x.IntProp, c => c.UsingComparer(typeof(CloseEnoughComparer)))
                           .Build();

            one.IntProp = 2;
            two.IntProp = 30;

            var result = comparer.Equals(one, two);

            Assert.That(result, Is.False);
        }
Ejemplo n.º 6
0
        public void Build_ReturnsAnEqualityComparerWhichTestsUsingAComparer_AndDetectsInequalObjectsAccordingly(SampleClass one, SampleClass two)
        {
            var comparer = new EqualityBuilder <SampleClass>()
                           .ForProperty(x => x.StringProp, c => c.UsingComparer(StringComparer.InvariantCulture))
                           .Build();

            one.StringProp = "foo";
            two.StringProp = "FOO";

            var result = comparer.Equals(one, two);

            Assert.That(result, Is.False);
        }
Ejemplo n.º 7
0
        public void Build_CanAutoTestAllPropertiesAndReturnFalseWhenOneIsNotEqual(SampleClass one, SampleClass two)
        {
            var comparer = new EqualityBuilder <SampleClass>()
                           .ForAllOtherProperties()
                           .Build();

            one.IntProp    = 2;
            one.StringProp = "foo";
            two.IntProp    = 3;
            two.StringProp = "foo";

            var result = comparer.Equals(one, two);

            Assert.That(result, Is.False);
        }
Ejemplo n.º 8
0
        public void Build_IgnoresAPropertyViaExplicitMention(SampleClass one, SampleClass two)
        {
            var comparer = new EqualityBuilder <SampleClass>()
                           .ForProperty(x => x.IntProp, c => c.Ignore())
                           .ForAllOtherProperties()
                           .Build();

            one.IntProp    = 2;
            one.StringProp = "foo";
            two.IntProp    = 10;
            two.StringProp = "foo";

            var result = comparer.Equals(one, two);

            Assert.That(result, Is.True);
        }
Ejemplo n.º 9
0
        public void Build_ReturnsAnEqualityComparerWhichPassesIfAllRulesPass(SampleClass one, SampleClass two)
        {
            var comparer = new EqualityBuilder <SampleClass>()
                           .ForProperty(x => x.IntProp, c => c.UsingComparer(typeof(CloseEnoughComparer)))
                           .ForProperty(x => x.StringProp)
                           .Build();

            one.IntProp    = 2;
            one.StringProp = "foo";
            two.IntProp    = 3;
            two.StringProp = "foo";

            var result = comparer.Equals(one, two);

            Assert.That(result, Is.True);
        }
Ejemplo n.º 10
0
        PersistenceTestResult IConfiguresComparison <T> .WithEqualityRule(Func <EqualityBuilder <T>, EqualityBuilder <T> > equalityBuilderAction, IResolvesServices resolver)
        {
            if (equalityBuilderAction == null)
            {
                throw new ArgumentNullException(nameof(equalityBuilderAction));
            }

            var builder = new EqualityBuilder <T>(resolver);

            builder = equalityBuilderAction(builder);
            if (builder == null)
            {
                throw new ArgumentException("The equality builder action must not return null", nameof(equalityBuilderAction));
            }

            var rule = builder.Build();

            return(WithEqualityRule(rule));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets an instance of <see cref="ComparerFactoryBuilder{T}"/> for the equality builder.
        /// </summary>
        /// <returns>The comparer factory builder.</returns>
        /// <param name="builder">Equality builder.</param>
        /// <typeparam name="TEquality">The type for which the equality builder was created.</typeparam>
        /// <typeparam name="TCompared">The value type to be compared.</typeparam>
        public static ComparerFactoryBuilder <TCompared> GetComparerFactoryBuilder <TEquality, TCompared>(this EqualityBuilder <TEquality> builder)
        {
            var resolver = builder.GetServiceResolver();

            return(new ComparerFactoryBuilder <TCompared>(resolver));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets the equality builder as a <see cref="IResolvesServices"/>.
 /// </summary>
 /// <returns>The service resolver.</returns>
 /// <param name="builder">Equality builder.</param>
 /// <typeparam name="T">The type for which the equality builder was created.</typeparam>
 public static IResolvesServices GetServiceResolver <T>(this EqualityBuilder <T> builder)
 {
     return(((IProvidesResolver)builder).GetResolver());
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Gets the equality builder as a <see cref="IProvidesRuleBuilders{T}"/>.
 /// </summary>
 /// <returns>The rule builder provider.</returns>
 /// <param name="builder">Equality builder.</param>
 /// <typeparam name="T">The type for which the equality builder was created.</typeparam>
 public static IProvidesRuleBuilders <T> AsRuleBuilderProvider <T>(this EqualityBuilder <T> builder) => builder;