private static bool RunAssertions <T>(TypedSpecification <T> spec, RunResult result, object fromWhen)
        {
            bool allOk = true;

            foreach (var exp in spec.GetAssertions())
            {
                var partiallyApplied = PartialApplicationVisitor.Apply(exp, fromWhen);
                try
                {
                    PAssert.IsTrue(partiallyApplied);
                    result.Expectations.Add(new ExpectationResult
                    {
                        Passed             = true,
                        Text               = PAssert.CreateSimpleFormatFor(partiallyApplied),
                        OriginalExpression = exp
                    });
                }
                catch (Exception ex)
                {
                    allOk = false;
                    result.Expectations.Add(new ExpectationResult
                    {
                        Passed             = false,
                        Text               = PAssert.CreateSimpleFormatFor(partiallyApplied),
                        OriginalExpression = exp,
                        Exception          = ex
                    });
                }
            }
            return(allOk);
        }
Esempio n. 2
0
        private RunResult Run <T>(TypedSpecification <T> spec)
        {
            var result = new RunResult {
                SpecificationName = spec.GetName()
            };

            try
            {
                var before = spec.GetBefore();
                before.InvokeIfNotNull();
            }
            catch (Exception ex)
            {
                result.MarkFailure("Before Failed", ex.InnerException);
                return(result);
            }
            object sut = null;

            try
            {
                var given = spec.GetOn();
                sut       = given.DynamicInvoke();
                result.On = given;
            }
            catch (Exception ex)
            {
                result.MarkFailure("On Failed", ex.InnerException);
            }
            object   whenResult = null;
            Delegate when;

            try
            {
                when = spec.GetWhen();
                if (when == null)
                {
                    return new RunResult {
                               SpecificationName = spec.GetName(), Passed = false, Message = "No when on specification"
                    }
                }
                ;
                if (when.Method.GetParameters().Length == 1)
                {
                    whenResult = when.DynamicInvoke(new[] { sut });
                }
                else
                {
                    whenResult = when.DynamicInvoke();
                }
                if (when.Method.ReturnType != typeof(void))
                {
                    result.Result = whenResult;
                }
                else
                {
                    result.Result = sut;
                }
            }
            catch (Exception ex)
            {
                result.MarkFailure("When Failed", ex.InnerException);
                return(result);
            }
            var  fromWhen = when.Method.ReturnType == typeof(void) ? sut : whenResult;
            bool allOk    = true;

            foreach (var exp in spec.GetAssertions())
            {
                var partiallyApplied = PartialApplicationVisitor.Apply(exp, fromWhen);
                try
                {
                    PAssert.IsTrue(partiallyApplied);
                    result.Expectations.Add(new ExpectationResult {
                        Passed = true, Text = PAssert.CreateSimpleFormatFor(partiallyApplied), OriginalExpression = exp
                    });
                }
                catch (Exception ex)
                {
                    allOk = false;
                    result.Expectations.Add(new ExpectationResult {
                        Passed = false, Text = PAssert.CreateSimpleFormatFor(partiallyApplied), OriginalExpression = exp, Exception = ex
                    });
                }
            }
            try
            {
                var Finally = spec.GetFinally();
                Finally.InvokeIfNotNull();
            }
            catch (Exception ex)
            {
                allOk          = false;
                result.Message = "Finally failed";
                result.Thrown  = ex.InnerException;
            }
            result.Passed = allOk;
            return(result);
        }
        public static void Document <T>(
            RunResult result,
            List <Expression <Action> > before,
            IEvent <T>[] given,
            ICommand <T> when,
            string decisions) where T : IIdentity
        {
            var passed = result.Passed ? "Passed" : "Failed";

            Console.WriteLine("{0}: {1} - {2}", Extensions.CleanupName(result.FoundOnMemberInfo.DeclaringType.Name), Extensions.CleanupName(result.Name), passed);

            if (before.Any())
            {
                Console.WriteLine();
                Console.WriteLine("Environment: ");
                foreach (var expression in before)
                {
                    PrintAdjusted("  ", PAssert.CreateSimpleFormatFor(expression));
                }
            }

            if (given.Any())
            {
                Console.WriteLine();
                Console.WriteLine("Given:");

                for (int i = 0; i < given.Length; i++)
                {
                    PrintEvil.PrintAdjusted(" " + (i + 1) + ". ", Describe.Object(given[i]).Trim());
                }
            }


            if (when != null)
            {
                Console.WriteLine();
                PrintAdjusted("When: ", Describe.Object(when).Trim());
            }

            Console.WriteLine();
            Console.WriteLine("Expectations:");
            foreach (var expecation in result.Expectations)
            {
                PrintAdjusted("  " + (expecation.Passed ? "[Passed]" : "[Failed]") + " ", expecation.Text.Trim());
                if (!expecation.Passed && expecation.Exception != null)
                {
                    PrintAdjusted("             ", expecation.Exception.Message);
                }
            }



            if (result.Thrown != null)
            {
                Console.WriteLine("Specification failed: " + result.Message.Trim());
                Console.WriteLine();
                Console.WriteLine(result.Thrown);
            }

            if (decisions.Length > 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Decisions made:");
                PrintAdjusted("  ", decisions.ToString());
            }

            Console.WriteLine(new string('-', 80));
            Console.WriteLine();
        }