Ejemplo n.º 1
0
        /// <summary>
        /// Runs the specified test specification.
        /// </summary>
        /// <param name="specification">The test specification to run.</param>
        /// <returns>
        /// The result of running the test specification.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="specification"/> is <c>null</c>.</exception>
        public ExceptionCentricAggregateQueryTestResult Run(ExceptionCentricAggregateQueryTestSpecification specification)
        {
            if (specification == null)
            {
                throw new ArgumentNullException(nameof(specification));
            }
            var sut = specification.SutFactory();

            sut.Initialize(specification.Givens);
            object queryResult = null;
            var    result      = Catch.Exception(() => queryResult = specification.When(sut));

            if (!result.HasValue)
            {
                if (sut.HasChanges())
                {
#if NET20
                    return(specification.Fail(new List <object>(sut.GetChanges()).ToArray()));
#else
                    return(specification.Fail(sut.GetChanges().ToArray()));
#endif
                }
                return(specification.Fail(queryResult));
            }
            var actualException = result.Value;
#if NET20
            using (var enumerator = _comparer.Compare(actualException, specification.Throws).GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    return(specification.Fail(actualException));
                }
            }
#else
            if (_comparer.Compare(actualException, specification.Throws).Any())
            {
                return(specification.Fail(actualException));
            }
#endif
            if (sut.HasChanges())
            {
#if NET20
                return(specification.Fail(new List <object>(sut.GetChanges())));
#else
                return(specification.Fail(sut.GetChanges().ToArray()));
#endif
            }
            return(specification.Pass());
        }
Ejemplo n.º 2
0
        public async Task <ExceptionCentricTestResult> Run(ExceptionCentricTestSpecification specification)
        {
            var methodInfo = _handler.GetType().GetMethods().Single(
                mi => {
                var parameters = mi.GetParameters();
                return(mi.IsPublic &&
                       mi.ReturnType == typeof(ValueTask) &&
                       parameters.Length == 2 &&
                       parameters[0].ParameterType == specification.When.GetType() &&
                       parameters[1].ParameterType == typeof(CancellationToken));
            });

            ValueTask Handler(object o, CancellationToken ct) => (ValueTask)methodInfo.Invoke(_handler, new[] { o, ct }) !;

            _factRecorder.Record(specification.Givens);

            Optional <Exception> optionalException = Optional <Exception> .Empty;

            try {
                await Handler(specification.When, CancellationToken.None);
            } catch (Exception ex) {
                optionalException = ex;
            }

            var then = _factRecorder.GetFacts().Skip(specification.Givens.Length).ToArray();

            return(new ExceptionCentricTestResult(specification,
                                                  optionalException.HasValue &&
                                                  !_comparer.Compare(specification.Throws, optionalException.Value).Any()
                    ? TestResultState.Passed
                    : TestResultState.Failed,
                                                  optionalException, then));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Runs the specified test specification.
        /// </summary>
        /// <param name="specification">The test specification to run.</param>
        /// <returns>
        /// The result of running the test specification.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="specification"/> is <c>null</c>.</exception>
        public ExceptionCentricAggregateQueryTestResult Run(ExceptionCentricAggregateQueryTestSpecification specification)
        {
            if (specification == null)
            {
                throw new ArgumentNullException("specification");
            }

            var sut = specification.SutFactory();

            sut.Initialize(specification.Givens);

            object queryResult = null;
            var    result      = Catch.Exception(() => queryResult = specification.When(sut));

            if (!result.HasValue)
            {
                return(sut.HasChanges()
                    ? specification.Fail(sut.GetChanges().ToArray())
                    : specification.Fail(queryResult));
            }

            var actualException = result.Value;

            if (_comparer.Compare(actualException, specification.Throws).Any())
            {
                return(specification.Fail(actualException));
            }

            return(sut.HasChanges()
                ? specification.Fail(sut.GetChanges().ToArray())
                : specification.Pass());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Runs the specified test specification.
        /// </summary>
        /// <param name="specification">The test specification to run.</param>
        /// <returns>
        /// The result of running the test specification.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="specification"/> is <c>null</c>.</exception>
        public ExceptionCentricAggregateConstructorTestResult Run(ExceptionCentricAggregateConstructorTestSpecification specification)
        {
            if (specification == null)
            {
                throw new ArgumentNullException("specification");
            }
            IAggregateRootEntity sut = null;
            var result = Catch.Exception(() => sut = specification.SutFactory());

            if (!result.HasValue)
            {
                if (sut.HasChanges())
                {
#if NET20
                    return(specification.Fail(new List <object>(sut.GetChanges()).ToArray()));
#else
                    return(specification.Fail(sut.GetChanges().ToArray()));
#endif
                }
                return(specification.Fail());
            }
            var actualException = result.Value;
#if NET20
            using (var enumerator = _comparer.Compare(actualException, specification.Throws).GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    return(specification.Fail(actualException));
                }
            }
#else
            if (_comparer.Compare(actualException, specification.Throws).Any())
            {
                return(specification.Fail(actualException));
            }
#endif
            return(specification.Pass());
        }
Ejemplo n.º 5
0
        private async Task <ExceptionCentricTestResult> RunAsync(ExceptionCentricTestSpecification spec)
        {
            var position = await _factWriter.PersistFacts(spec.Givens);

            var handleCommand = _handlerResolver.ResolveHandlerFor(spec.When);
            var result        = await Catch.Exception(async() => await handleCommand(spec.When));

            var actualEvents = await _factReader.RetrieveFacts(position);

            if (!result.HasValue)
            {
                return(actualEvents.Any() ? spec.Fail(actualEvents) : spec.Fail());
            }

            var actualException = result.Value;

            return(_comparer.Compare(spec.Throws, actualException).Any()
                ? spec.Fail(actualException)
                : spec.Pass(actualException));
        }
        /// <summary>
        /// Asserts that the specification is met.
        /// </summary>
        /// <param name="builder">The specification builder.</param>
        /// <param name="comparer">The exception comparer.</param>
        public static void Assert(
            this IExceptionCentricAggregateConstructorTestSpecificationBuilder builder,
            IExceptionComparer comparer)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            var specification = builder.Build();
            var runner        = new ExceptionCentricAggregateConstructorTestRunner(comparer);
            var result        = runner.Run(specification);

            if (result.Failed)
            {
                if (result.ButException.HasValue)
                {
                    if (result.ButException.Value.GetType() != result.Specification.Throws.GetType())
                    {
                        using (var writer = new StringWriter())
                        {
                            writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                            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
                        }
                    }
                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        writer.WriteLine("  But found the following differences:");
                        foreach (var difference in comparer.Compare(result.Specification.Throws, result.ButException.Value))
                        {
                            writer.WriteLine("    {0}", difference.Message);
                        }

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

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

#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},", result.Specification.Throws);
                    writer.WriteLine("  But no exception occurred");

#if NUNIT
                    throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                    throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Asserts that the specification is met.
        /// </summary>
        /// <param name="builder">The specification builder.</param>
        /// <param name="runner"></param>
        /// <param name="comparer">The exception comparer.</param>
        /// <param name="logger">A logger.</param>
        public static void Assert(
            this IExceptionCentricTestSpecificationBuilder builder,
            IExceptionCentricTestSpecificationRunner runner,
            IExceptionComparer 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($"Throws: \r\n{specification.Throws.ToLogString(includeStackTrace: false)}");

            var result = runner.Run(specification);

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

                    if (result.ButException.Value.GetType() != result.Specification.Throws.GetType())
                    {
                        using (var writer = new StringWriter())
                        {
                            writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                            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
                        }
                    }

                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        writer.WriteLine("  But found the following differences:");

                        foreach (var difference in comparer.Compare(result.Specification.Throws, result.ButException.Value))
                        {
                            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 exception: None");

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

                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        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},", result.Specification.Throws);
                    writer.WriteLine("  But no exception occurred");

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

            if (result.ButException.HasValue)
            {
                logger.LogTrace($"Actual exception: \r\n{result.ButException.Value.ToLogString(includeStackTrace: true)}");
            }
        }
        /// <summary>
        /// Asserts that the specification is met.
        /// </summary>
        /// <param name="builder">The specification builder.</param>
        /// <param name="comparer">The exception comparer.</param>
        public static void Assert(this IExceptionCentricAggregateCommandTestSpecificationBuilder builder,
            IExceptionComparer comparer)
        {
            if (builder == null) throw new ArgumentNullException("builder");
            if (comparer == null) throw new ArgumentNullException("comparer");
            var specification = builder.Build();
            var runner = new ExceptionCentricAggregateCommandTestRunner(comparer);
            var result = runner.Run(specification);
            if (result.Failed)
            {
                if (result.ButException.HasValue)
                {
                    if (result.ButException.Value.GetType() != result.Specification.Throws.GetType())
                    {
                        using (var writer = new StringWriter())
                        {
                            writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                            writer.WriteLine("  But was:  {0}", result.ButException.Value);

#if NUNIT
                            throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                            throw new Xunit.Sdk.AssertException(writer.ToString());
#endif
                        }
                    }
                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        writer.WriteLine("  But found the following differences:");
                        foreach (var difference in comparer.Compare(result.Specification.Throws, result.ButException.Value))
                        {
                            writer.WriteLine("    {0}", difference.Message);
                        }

#if NUNIT
                        throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                        throw new Xunit.Sdk.AssertException(writer.ToString());
#endif
                    }
                }
                if (result.ButEvents.HasValue)
                {
                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        writer.WriteLine("  But was:  {0} event(s) ({1})",
                            result.ButEvents.Value.Length,
                            String.Join(",", result.ButEvents.Value.Select(_ => _.GetType().Name).ToArray()));

#if NUNIT
                        throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                        throw new Xunit.Sdk.AssertException(writer.ToString());
#endif
                    }
                }
                using (var writer = new StringWriter())
                {
                    writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                    writer.WriteLine("  But no exception occurred");

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