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);
        }
        private RunResult Run <T>(TypedSpecification <T> spec, MemberInfo foundon)
        {
            var result = new RunResult {
                SpecificationName = spec.GetName()
            };
            RunResult runResult;
            object    sut;
            object    whenResult;
            Delegate  when;

            if (RunSetup(spec, result, out runResult, out sut, out whenResult, out when))
            {
                return(runResult);
            }
            var  fromWhen = when.Method.ReturnType == typeof(void) ? sut : whenResult;
            bool allOk    = true;
            //OK THIS IS A HACK! ITS HERE FOR PROFILING SUPPORT (I NEED TO BE ABLE TO GET THE METHOD OF THE ORIGINAL METHOD OF SPEC)
            var hack = foundon as MethodInfo;

            if (hack != null)
            {
                hack.CallMethod();
            }
            //YOU CANNOT PUT A METHOD CALL BETWEEN LAST AND THIS ONE!!!!
            allOk         = RunAssertions(spec, result, fromWhen);
            allOk         = RunTeardowns(spec, result);
            result.Passed = allOk;
            return(result);
        }
        private static bool RunTeardowns <T>(TypedSpecification <T> spec, RunResult result)
        {
            bool allOk = true;

            try
            {
                var Finally = spec.GetFinally();
                Finally.InvokeIfNotNull();
            }
            catch (Exception ex)
            {
                allOk          = false;
                result.Message = "Finally failed";
                result.Thrown  = ex.InnerException;
            }
            return(allOk);
        }
Beispiel #4
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);
        }
        private static bool RunSetup <T>(TypedSpecification <T> spec, RunResult result, out RunResult runResult, out object sut,
                                         out object whenResult, out Delegate when)
        {
            when       = null;
            sut        = null;
            whenResult = null;
            runResult  = null;
            try
            {
                var before = spec.GetBefore();
                before.InvokeIfNotNull();
            }
            catch (Exception ex)
            {
                result.MarkFailure("Before Failed", ex.InnerException);
                {
                    runResult = result;

                    return(true);
                }
            }
            sut = null;
            try
            {
                var given = spec.GetOn();
                sut       = given.DynamicInvoke();
                result.On = given;
            }
            catch (Exception ex)
            {
                result.MarkFailure("On Failed", ex.InnerException);
            }
            whenResult = null;
            try
            {
                when = spec.GetWhen();
                if (when == null)
                {
                    runResult = new RunResult
                    {
                        SpecificationName = spec.GetName(), Passed = false, Message = "No when on specification"
                    };
                    return(true);
                }
                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);
                {
                    runResult = result;
                    return(true);
                }
            }
            return(false);
        }
Beispiel #6
0
        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;

            var allOk = true;

            foreach (var assertion in spec.GetAssertions())
            {
                foreach (var expectationResult in assertion.Assert(fromWhen))
                {
                    result.Expectations.Add(expectationResult);
                    if (!expectationResult.Passed)
                    {
                        allOk = false;
                    }
                }
            }
            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);
        }