Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
 public void Document(RunResult result)
 {
     Console.WriteLine("Do something");
 }