Exemplo n.º 1
0
        public List <FailedAnalyzedAssertion> AnalyzeAssertionFailures()
        {
            var treeProvider = new AssertionTreeProvider(_context.Assertion.Expression);

            var tree = treeProvider.GetTree();

            var executor = new AssertionTreeExecutor(tree, _context.AssertionException);

            var failedParts = executor.Execute();

            var failedAssertions = new List <FailedAnalyzedAssertion>(failedParts.Length);

            foreach (var part in failedParts)
            {
                try
                {
                    if (part.Exception == null)
                    {
                        var friendlyMessageProvider = new FriendlyMessageProvider(_context, part);

                        var friendlyMessage = friendlyMessageProvider.TryGetFriendlyMessage();

                        var failedAssertion = new FailedAnalyzedAssertion(part, friendlyMessage?.Message, friendlyMessage?.Pattern);

                        failedAssertions.Add(failedAssertion);
                    }
                    else
                    {
                        failedAssertions.Add(new FriendlyMessageProviderForException(_context).AnalyzeException(part));
                    }
                }
                catch
                {
                    failedAssertions.Add(new FailedAnalyzedAssertion(part, null, null));
                }
            }

            return(failedAssertions);
        }
        private string FormatExceptionMessage(AssertionFailureContext context, FailedAnalyzedAssertion failedAssertion, Exception?originalException)
        {
            var assertionExpression = ExpressionToString(failedAssertion.Assertion.Expression);

            string result;

            if (failedAssertion.Message == null)
            {
                result = $"Assertion failed: {assertionExpression}";
            }
            else
            {
                result = $@"{failedAssertion.Message}

Assertion: {assertionExpression}";
            }

            if (_assertion.Message != null)
            {
                result += $@"

Message: {(_assertion.Message is string s ? s : Serializer.Serialize(_assertion.Message))}";
            }

            if (_assertion.Context != null)
            {
                result += $@"

Context: {ExpressionToString(_assertion.Context.Body)} = {Serializer.Serialize(EvaluateExpression(_assertion.Context.Body))}
";
            }

            if (originalException != null)
            {
                if (failedAssertion.CauseOfException != null)
                {
                    result += $@"

Cause of exception: {ExpressionToString(failedAssertion.CauseOfException)}";
                }

                result += $@"

Exception: {originalException.Message}

StackTrace: {originalException.StackTrace}";
            }

            var locals = LocalsProvider.LocalsToString(failedAssertion.Assertion.Expression, context.EvaluatedExpressions);

            if (locals != null)
            {
                result += $@"

Locals:

{locals}
";
            }

            result += Environment.NewLine;

            return(result);
        }