Ejemplo n.º 1
0
        /// <summary>
        ///     Asserts that the specification is met.
        /// </summary>
        /// <param name="builder">The specification builder.</param>
        /// <param name="runner"></param>
        /// <param name="comparer">The event comparer.</param>
        /// <param name="logger">A logger.</param>
        public static void Assert(
            this IEventCentricTestSpecificationBuilder builder,
            IEventCentricTestSpecificationRunner runner,
            IFactComparer comparer,
            ILogger logger)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (runner == null)
            {
                throw new ArgumentNullException(nameof(runner));
            }

            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var specification = builder.Build();

            logger.LogTrace($"Given: \r\n{specification.Givens.ToLogStringVerbose()}");
            logger.LogTrace($"When: \r\n{specification.When.ToLogString()}");
            logger.LogTrace($"Then: \r\n{specification.Thens.ToLogStringVerbose()}");

            var result = runner.Run(specification);

            if (result.Failed)
            {
                if (result.ButException.HasValue)
                {
                    logger.LogTrace($"Actual exception: \r\n{result.ButException.Value.ToLogString(includeStackTrace: true)}");

                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0} event(s),", result.Specification.Thens.Length);
                        writer.WriteLine("  But was:  {0}", result.ButException.Value);

#if NUNIT
                        throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                        throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                    }
                }

                if (result.ButEvents.HasValue)
                {
                    logger.LogTrace($"Actual events: \r\n{result.ButEvents.Value.ToLogStringVerbose()}");

                    if (result.ButEvents.Value.Length != result.Specification.Thens.Length)
                    {
                        using (var writer = new StringWriter())
                        {
                            writer.WriteLine("  Expected: {0} event(s) ({1}),",
                                             result.Specification.Thens.Length,
                                             result.Specification.Thens.ToLogStringShort());

                            writer.WriteLine("  But was:  {0} event(s) ({1})",
                                             result.ButEvents.Value.Length,
                                             result.ButEvents.Value.ToLogStringShort());

#if NUNIT
                            throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                            throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                        }
                    }

                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0} event(s) ({1}),",
                                         result.Specification.Thens.Length,
                                         result.Specification.Thens.ToLogStringShort());

                        writer.WriteLine("  But found the following differences:");
                        foreach (var difference in result.Specification.Thens
                                 .Zip(result.ButEvents.Value, (expected, actual) => new Tuple <Fact, Fact>(expected, actual))
                                 .SelectMany(_ => comparer.Compare(_.Item1, _.Item2)))
                        {
                            writer.WriteLine("    {0}", difference.Message);
                        }

#if NUNIT
                        throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                        throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                    }
                }

                logger.LogTrace("Actual events: None");
            }

            logger.LogTrace(result.ButEvents.HasValue
                ? $"Actual events: \r\n{result.ButEvents.Value.ToLogStringVerbose()}"
                : "Actual events: None");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asserts that the specification is met.
        /// </summary>
        /// <param name="builder">The specification builder.</param>
        /// <param name="comparer">The event comparer.</param>
        public static async Task Assert(this IEventCentricTestSpecificationBuilder builder,
                                        IFactComparer comparer, object handler, IFactRecorder factRecorder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            if (factRecorder == null)
            {
                throw new ArgumentNullException(nameof(factRecorder));
            }
            var specification = builder.Build();
            var runner        = new EventCentricTestSpecificationRunner(comparer, handler, factRecorder);
            var result        = await runner.Run(specification);

            if (!result.Failed)
            {
                return;
            }
            if (result.ButException.HasValue)
            {
                await using var writer = new StringWriter();
                writer.WriteLine("  Expected: {0} event(s),", result.Specification.Thens.Length);
                writer.WriteLine("  But was:  {0}", result.ButException.Value);

                throw new Xunit.Sdk.XunitException(writer.ToString());
            }

            if (!result.ButEvents.HasValue)
            {
                return;
            }

            if (result.ButEvents.Value.Length != result.Specification.Thens.Length)
            {
                await using var writer = new StringWriter();
                writer.WriteLine("  Expected: {0} event(s) ({1}),",
                                 result.Specification.Thens.Length,
                                 string.Join(",", result.Specification.Thens.Select(_ => _.Event.GetType().Name).ToArray()));
                writer.WriteLine("  But was:  {0} event(s) ({1})",
                                 result.ButEvents.Value.Length,
                                 string.Join(",", result.ButEvents.Value.Select(_ => _.Event.GetType().Name).ToArray()));

                throw new Xunit.Sdk.XunitException(writer.ToString());
            }

            await using (var writer = new StringWriter()) {
                writer.WriteLine("  Expected: {0} event(s) ({1}),",
                                 result.Specification.Thens.Length,
                                 string.Join(",", result.Specification.Thens.Select(_ => _.Event.GetType().Name).ToArray()));
                writer.WriteLine("  But found the following differences:");
                foreach (var difference in
                         result.Specification.Thens.Zip(result.ButEvents.Value,
                                                        (expected, actual) => new Tuple <Fact, Fact>(expected, actual))
                         .SelectMany(_ => comparer.Compare(_.Item1, _.Item2)))
                {
                    writer.WriteLine("    {0}", difference.Message);
                }

                throw new Xunit.Sdk.XunitException(writer.ToString());
            }
        }
Ejemplo n.º 3
0
 public bool Equals(Fact x, Fact y) => !_comparer.Compare(x, y).Any();