Ejemplo n.º 1
0
 private EquivalencyAssertionOptions <VariableShelfEntity> ExcludeProperties(
     EquivalencyAssertionOptions <VariableShelfEntity> options)
 {
     options.Excluding(t => t.Id);
     options.Excluding(t => t.Uid);
     return(options);
 }
Ejemplo n.º 2
0
 private EquivalencyAssertionOptions <IEnumerable> ApplyExcludesForDays(EquivalencyAssertionOptions <IEnumerable> options)
 {
     return(options
            .ExcludingIds()
            .Excluding(nameof(ScheduledStep.Task))
            .Excluding(nameof(ScheduledTask.Day)));
 }
        /// <summary>
        /// Asserts that a collection of objects is equivalent to another collection of objects.
        /// </summary>
        /// <remarks>
        /// The two collections are equivalent when they both contain the same strings in any order.
        /// </remarks>
        /// <param name="config">
        /// A reference to the <see cref="EquivalencyAssertionOptions{String}"/> configuration object that can be used
        /// to influence the way the object graphs are compared. You can also provide an alternative instance of the
        /// <see cref="EquivalencyAssertionOptions{String}"/> class. The global defaults are determined by the
        /// <see cref="AssertionOptions"/> class.
        /// </param>
        /// <param name="because">
        /// An optional formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the
        /// assertion is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        /// Zero or more objects to format using the placeholders in <see cref="because" />.
        /// </param>
        public AndConstraint <StringCollectionAssertions> BeEquivalentTo(IEnumerable <string> expectation,
                                                                         Func <EquivalencyAssertionOptions <string>, EquivalencyAssertionOptions <string> > config, string because = "",
                                                                         params object[] becauseArgs)
        {
            Guard.ThrowIfArgumentIsNull(config, nameof(config));

            EquivalencyAssertionOptions <IEnumerable <string> > options = config(AssertionOptions.CloneDefaults <string>()).AsCollection();

            var context = new EquivalencyValidationContext
            {
                Subject          = Subject,
                Expectation      = expectation,
                RootIsCollection = true,
                CompileTimeType  = typeof(IEnumerable <string>),
                Because          = because,
                BecauseArgs      = becauseArgs,
                Tracer           = options.TraceWriter
            };

            var equivalencyValidator = new EquivalencyValidator(options);

            equivalencyValidator.AssertEquality(context);

            return(new AndConstraint <StringCollectionAssertions>(this));
        }
Ejemplo n.º 4
0
 public static EquivalencyAssertionOptions <T> GetAssertionOptions <T>(EquivalencyAssertionOptions <T> options)
 {
     return(options.Using <Interval>(c => c.Subject.Equals(c.Expectation).Should().BeTrue())
            .WhenTypeIs <Interval>()
            .Using <OffsetDateTime>(c => c.Subject.Should().Be(c.Expectation))
            .WhenTypeIs <OffsetDateTime>());
 }
 EquivalencyAssertionOptions <T> AssertionOptions <T>(EquivalencyAssertionOptions <T> options)
 {
     return(options.WithStrictOrdering()
            .Using <Expression>(x => AssertEquivalentExpressions(x.Expectation, x.Subject))
            .WhenTypeIs <Expression>()
            .IncludingAllRuntimeProperties());
 }
 private EquivalencyAssertionOptions <AppTask> ApplyExcludesForTask(EquivalencyAssertionOptions <AppTask> options)
 {
     return(options
            .ExcludingIds()
            .Excluding(t => t.LastModified)
            .Excluding(nameof(Step.Task)));
 }
Ejemplo n.º 7
0
 static EquivalencyAssertionOptions <Person> ExcludingPersonId(EquivalencyAssertionOptions <Person> options)
 {
     return(options.Excluding(
                memberInfo => memberInfo.SelectedMemberInfo.Name == nameof(Person.Id) &&
                memberInfo.SelectedMemberInfo.DeclaringType == typeof(Person)
                ));
 }
        public void When_concurrently_getting_equality_strategy_it_should_not_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            Type type = typeof(IEnumerable);
            IEquivalencyAssertionOptions equivalencyAssertionOptions = new EquivalencyAssertionOptions();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () => Parallel.For(
                0,
                10_000,
                new ParallelOptions
            {
                MaxDegreeOfParallelism = 8
            },
                e => equivalencyAssertionOptions.GetEqualityStrategy(type)
                );

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.Should().NotThrow();
        }
 public static EquivalencyAssertionOptions <TDeclaring> ExcludeMember <TDeclaring>(
     this EquivalencyAssertionOptions <TDeclaring> options,
     string fieldName)
 {
     return(options.Excluding(info => info.SelectedMemberInfo.Name.Equals(fieldName) &&
                              info.SelectedMemberInfo.DeclaringType == typeof(TDeclaring)));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Asserts that an object can be serialized and deserialized using the binary serializer and that it stills retains
        /// the values of all members.
        /// </summary>
        /// <param name="assertions"></param>
        /// <param name="options">
        /// A reference to the <see cref="EquivalencyAssertionOptions{TExpectation}"/> configuration object that can be used
        /// to influence the way the object graphs are compared. You can also provide an alternative instance of the
        /// <see cref="EquivalencyAssertionOptions{TExpectation}"/> class. The global defaults are determined by the
        /// <see cref="AssertionOptions"/> class.
        /// </param>
        /// <param name="because">
        /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        /// Zero or more objects to format using the placeholders in <paramref name="because" />.
        /// </param>
        public static AndConstraint <ObjectAssertions> BeBinarySerializable <T>(this ObjectAssertions assertions,
                                                                                Func <EquivalencyAssertionOptions <T>, EquivalencyAssertionOptions <T> > options, string because = "",
                                                                                params object[] becauseArgs)
        {
            Guard.ThrowIfArgumentIsNull(options, nameof(options));

            try
            {
                object deserializedObject = CreateCloneUsingBinarySerializer(assertions.Subject);

                EquivalencyAssertionOptions <T> defaultOptions = AssertionOptions.CloneDefaults <T>()
                                                                 .RespectingRuntimeTypes().IncludingFields().IncludingProperties();

                ((T)deserializedObject).Should().BeEquivalentTo((T)assertions.Subject, _ => options(defaultOptions));
            }
            catch (Exception exc)
            {
                Execute.Assertion
                .BecauseOf(because, becauseArgs)
                .FailWith("Expected {0} to be serializable{reason}, but serialization failed with:{1}{1}{2}.",
                          assertions.Subject,
                          Environment.NewLine,
                          exc.Message);
            }

            return(new AndConstraint <ObjectAssertions>(assertions));
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Assertion rule for <see cref="TMessage"/>.
 /// </summary>
 /// <param name="options">Assertion options.</param>
 /// <typeparam name="TMessage">Type of the message.</typeparam>
 /// <returns>Configured assertion.</returns>
 public static EquivalencyAssertionOptions <TMessage> ForMessage <TMessage>(
     this EquivalencyAssertionOptions <TMessage> options)
     where TMessage : IMessage
 => options
 .Excluding(m => m.Id)
 .Excluding(m => m.Created)
 .ExcludingMissingMembers()
 .ComparingByMembers <IMessage>();
Ejemplo n.º 12
0
 public static EquivalencyAssertionOptions <T> ExcludingMember <T>(this EquivalencyAssertionOptions <T> option,
                                                                   params string[] fields)
 {
     foreach (var field in fields)
     {
         option.Excluding(t => t.SelectedMemberInfo.Name == field);
     }
     return(option);
 }
Ejemplo n.º 13
0
 static EquivalencyAssertionOptions <TestResult> TestResultMatchingOptions(EquivalencyAssertionOptions <TestResult> opts)
 {
     return(opts
            .Including(tr => tr.Outcome)
            .Including(tr => tr.ErrorMessage)
            .Including(tr => tr.ErrorStackTrace)
            .Including(tr => tr.TestCase.FullyQualifiedName)
            .Including(tr => tr.TestCase.ExecutorUri)
            .Including(tr => tr.TestCase.Source));
 }
Ejemplo n.º 14
0
    protected EquivalencyAssertionOptions <Product> DefaultProductEquivalencyOptions(
        EquivalencyAssertionOptions <Product> options)
    {
        options.Excluding(x => x.Etag);
        options.Excluding(x => x.CreatedTimeUtc);
        options.Excluding(x => x.LastUpdatedTimeRaw);
        options.Excluding(x => x.LastUpdatedTimeUtc);

        return(options);
    }
Ejemplo n.º 15
0
        public static void ShouldAllBeEquivalentTo <T>(this IEnumerable <T> subject, IEnumerable expectation,
                                                       Func <EquivalencyAssertionOptions <T>, EquivalencyAssertionOptions <T> > config, string reason = "", params object[] reasonArgs)
        {
            var context = new EquivalencyValidationContext()
            {
                Subject         = subject,
                Expectation     = expectation,
                CompileTimeType = typeof(T),
                Reason          = reason,
                ReasonArgs      = reasonArgs
            };

            new EquivalencyValidator(config(EquivalencyAssertionOptions <T> .Default())).AssertEquality(context);
        }
Ejemplo n.º 16
0
        public static EquivalencyAssertionOptions <DeliveryRequest> RootRequestEquivalenceOptions(
            EquivalencyAssertionOptions <DeliveryRequest> options) =>
        options
        .ExcludingMissingMembers()
        .Excluding(ctx => ctx.Path == "RequestId")
        .Excluding(ctx => ctx.Path == "ImpressionId")
        .Excluding(ctx => ctx.Path == "Context.UserAgent")
        .Excluding(ctx => ctx.Path == "ExperienceCloud")
        .IgnoringCyclicReferences()
        .ComparingByMembers <DeliveryRequest>()
        .ComparingByMembers <Context>()
        .ComparingByMembers <Notification>()
        .ComparingByMembers <Telemetry>()
        .ComparingByMembers <TelemetryEntry>()
        .ComparingByMembers <TelemetryFeatures>()
        .Using <string>(ctx =>
        {
            if (ctx.Expectation == "expect.any(String)")
            {
                ctx.Subject.Should().NotBeNullOrEmpty();
                return;
            }

            ctx.Subject.Should().Be(ctx.Expectation);
        })
        .When(info => info.Type == typeof(string))
        .Using <long?>(ctx =>
        {
            if (ctx.Expectation == -999L)
            {
                ctx.Subject.Should().BePositive();
                return;
            }

            ctx.Subject.Should().Be(ctx.Expectation);
        })
        .When(info => info.Type == typeof(long?))
        .Using <int?>(ctx =>
        {
            if (ctx.Expectation == -999)
            {
                ctx.Subject.Should().BePositive();
                return;
            }

            ctx.Subject.Should().Be(ctx.Expectation);
        })
        .When(info => info.Type == typeof(int?))
        .WithTracing();
Ejemplo n.º 17
0
        /// <summary>
        /// Asserts that an object is equivalent to another object.
        /// </summary>
        /// <remarks>
        /// Objects are equivalent when both object graphs have equally named properties with the same value,
        /// irrespective of the type of those objects. Two properties are also equal if one type can be converted to another and the result is equal.
        /// The type of a collection property is ignored as long as the collection implements <see cref="IEnumerable"/> and all
        /// items in the collection are structurally equal.
        /// </remarks>
        /// <param name="config">
        /// A reference to the <see cref="EquivalencyAssertionOptions{TSubject}"/> configuration object that can be used
        /// to influence the way the object graphs are compared. You can also provide an alternative instance of the
        /// <see cref="EquivalencyAssertionOptions{TSubject}"/> class. The global defaults are determined by the
        /// <see cref="AssertionOptions"/> class.
        /// </param>
        /// <param name="because">
        /// An optional formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the
        /// assertion is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="reasonArgs">
        /// Zero or more objects to format using the placeholders in <see cref="reason" />.
        /// </param>
        public static void ShouldBeEquivalentTo <T>(this T subject, object expectation,
                                                    Func <EquivalencyAssertionOptions <T>, EquivalencyAssertionOptions <T> > config, string because = "",
                                                    params object[] reasonArgs)
        {
            var context = new EquivalencyValidationContext
            {
                Subject         = subject,
                Expectation     = expectation,
                CompileTimeType = typeof(T),
                Reason          = because,
                ReasonArgs      = reasonArgs
            };

#pragma warning disable 618
            new EquivalencyValidator(config(EquivalencyAssertionOptions <T> .Default())).AssertEquality(context);
#pragma warning restore 618
        }
        public void UpdateLog_test()
        {
            using (var database = CreateDatabase())
            {
                var datastore = new CuttingUnitDatastore(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID, new SamplerInfoDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID));

                var tree_guid = datastore.CreateMeasureTree("u1", "st1", "sg1");

                var log = new Log()
                {
                    TreeID = tree_guid, LogNumber = 1
                };
                datastore.InsertLog(log);

                var randomizer = new Randomizer(8675309);

                log.BarkThickness    = randomizer.Double();
                log.BoardFootRemoved = randomizer.Double();
                log.CubicFootRemoved = randomizer.Double();
                log.DIBClass         = randomizer.Double();
                log.ExportGrade      = randomizer.String();
                log.Grade            = randomizer.String();
                log.GrossBoardFoot   = randomizer.Double();
                log.GrossCubicFoot   = randomizer.Double();
                log.LargeEndDiameter = randomizer.Double();
                log.Length           = randomizer.Int();
                log.LogNumber        = randomizer.Int();
                //log.ModifiedBy = randomizer.String(10);
                log.NetBoardFoot       = randomizer.Double();
                log.NetCubicFoot       = randomizer.Double();
                log.PercentRecoverable = randomizer.Double();
                log.SeenDefect         = randomizer.Double();
                log.SmallEndDiameter   = randomizer.Double();

                datastore.UpdateLog(log);

                var logAgain = datastore.GetLog(log.LogID);

                var eqivConfig = new EquivalencyAssertionOptions <Log>();
                eqivConfig.Excluding(x => x.CreatedBy);

                logAgain.Should().BeEquivalentTo(log, config: x => x.Excluding(l => l.CreatedBy));
            }
        }
        /// <summary>
        /// Asserts that an object is equivalent to another object.
        /// </summary>
        /// <remarks>
        /// Objects are equivalent when both object graphs have equally named properties with the same value,
        /// irrespective of the type of those objects. Two properties are also equal if one type can be converted to another and the result is equal.
        /// The type of a collection property is ignored as long as the collection implements <see cref="IEnumerable{T}"/> and all
        /// items in the collection are structurally equal.
        /// </remarks>
        /// <param name="config">
        /// A reference to the <see cref="EquivalencyAssertionOptions{TSubject}"/> configuration object that can be used
        /// to influence the way the object graphs are compared. You can also provide an alternative instance of the
        /// <see cref="EquivalencyAssertionOptions{TSubject}"/> class. The global defaults are determined by the
        /// <see cref="AssertionOptions"/> class.
        /// </param>
        /// <param name="because">
        /// An optional formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the
        /// assertion is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="becauseArgs">
        /// Zero or more objects to format using the placeholders in <see cref="because" />.
        /// </param>
        public void BeEquivalentTo <TExpectation>(TExpectation expectation,
                                                  Func <EquivalencyAssertionOptions <TExpectation>, EquivalencyAssertionOptions <TExpectation> > config, string because = "",
                                                  params object[] becauseArgs)
        {
            EquivalencyAssertionOptions <TExpectation> options = config(AssertionOptions.CloneDefaults <TExpectation>());

            var context = new EquivalencyValidationContext
            {
                Subject         = Subject,
                Expectation     = expectation,
                CompileTimeType = typeof(TExpectation),
                Because         = because,
                BecauseArgs     = becauseArgs,
                Tracer          = options.TraceWriter
            };

            var equivalencyValidator = new EquivalencyValidator(options);

            equivalencyValidator.AssertEquality(context);
        }
Ejemplo n.º 20
0
        public void CanHandleIMaybeExpectation()
        {
            // Arrange
            IMaybe <Guid> expectation = new Some <Guid>(Guid.NewGuid());

            var context = new EquivalencyValidationContext
            {
                Expectation     = expectation,
                CompileTimeType = expectation.GetType()
            };

            var config = new EquivalencyAssertionOptions <None <int> >();

            var sut = new MaybeEquivalencyStep();

            // Act
            var actual = sut.CanHandle(context, config);

            // Assert
            actual.Should().BeTrue();
        }
Ejemplo n.º 21
0
        public static EquivalencyAssertionOptions <List <ExecutedExample> > ExpectSomeDuration(
            this EquivalencyAssertionOptions <List <ExecutedExample> > opts)
        {
            opts.Using <TimeSpan>(ctx =>
            {
                TimeSpan actual   = ctx.Subject;
                TimeSpan expected = ctx.Expectation;

                if (expected == TimeSpan.Zero)
                {
                    actual.Should().Be(TimeSpan.Zero);
                }
                else
                {
                    actual.Should().BeGreaterThan(TimeSpan.Zero);
                }
            })
            .When(subj => subj.SelectedMemberPath.EndsWith(".Duration", StringComparison.Ordinal));

            return(opts);
        }
Ejemplo n.º 22
0
        public static EquivalencyAssertionOptions <List <ExecutedExample> > ExpectPartialStackTrace(
            this EquivalencyAssertionOptions <List <ExecutedExample> > opts)
        {
            opts.Using <string>(ctx =>
            {
                string actual   = ctx.Subject;
                string expected = ctx.Expectation;

                if (expected == null)
                {
                    actual.Should().BeNull();
                }
                else
                {
                    actual.Should().NotBeNull().And.Contain(expected);
                }
            })
            .When(subj => subj.SelectedMemberPath.EndsWith(".ExceptionStackTrace", StringComparison.Ordinal));

            return(opts);
        }
Ejemplo n.º 23
0
        public void CannotHandleObjectExpectation()
        {
            // Arrange
            object expectation = new object();

            var context = new EquivalencyValidationContext
            {
                Expectation     = expectation,
                CompileTimeType = expectation.GetType()
            };

            var config = new EquivalencyAssertionOptions <None <int> >();

            var sut = new MaybeEquivalencyStep();

            // Act
            var actual = sut.CanHandle(context, config);

            // Assert
            actual.Should().BeFalse();
        }
Ejemplo n.º 24
0
 private static EquivalencyAssertionOptions<KeyBinding> SetUpKeyBindingEquivalency(EquivalencyAssertionOptions<KeyBinding> options)
 {
     return
         options
             .Including(b => b.ActionTypeId)
             .Including(b => b.ActionId);
 }