Example #1
0
        public void RunBefores(nspec instance)
        {
            // parent chain

            RecurseAncestors(c => c.RunBefores(instance));

            // class (method-level)

            if (BeforeInstance != null && BeforeInstanceAsync != null)
            {
                throw new ArgumentException("A single class cannot have both a sync and an async class-level 'before_each' set, please pick one of the two");
            }

            BeforeInstance.SafeInvoke(instance);

            BeforeInstanceAsync.SafeInvoke(instance);

            // context-level

            if (Before != null && BeforeAsync != null)
            {
                throw new ArgumentException("A single context cannot have both a 'before' and an 'beforeAsync' set, please pick one of the two");
            }

            Before.SafeInvoke();

            BeforeAsync.SafeInvoke();
        }
Example #2
0
        /// <summary>
        /// Test execution happens in three phases: this is the first phase.
        /// </summary>
        /// <remarks>
        /// Here all contexts and all their examples are run, collecting distinct exceptions
        /// from context itself (befores/ acts/ it/ afters), beforeAll, afterAll.
        /// </remarks>
        public virtual void Run(bool failFast, nspec instance = null, bool recurse = true)
        {
            if (failFast && Parent.HasAnyFailures())
            {
                return;
            }

            var runningInstance = builtInstance ?? instance;

            using (new ConsoleCatcher(output => this.CapturedOutput = output))
            {
                BeforeAllChain.Run(runningInstance);
            }

            // intentionally using for loop to prevent collection was modified error in sample specs
            for (int i = 0; i < runnables.Count; i++)
            {
                if (failFast && HasAnyFailures())
                {
                    return;
                }

                runnables[i].Exercise(runningInstance);
            }

            if (recurse)
            {
                Contexts.Do(c => c.Run(failFast, runningInstance, recurse));
            }

            // TODO wrap this as well in a ConsoleCatcher, not before adding tests about it
            AfterAllChain.Run(runningInstance);
        }
Example #3
0
        public void Exercise(Example example, nspec nspec)
        {
            if (example.Pending)
            {
                return;
            }

            if (contextLevelFailure != null)
            {
                example.Exception = contextLevelFailure;
                return;
            }

            try
            {
                RunBefores(nspec);

                RunActs(nspec);

                example.Run(nspec);

                Afters();
            }
            catch (TargetInvocationException e)
            {
                example.Exception = e.InnerException;
            }
            catch (Exception e)
            {
                example.Exception = e;
            }
        }
Example #4
0
        public void RunBefores(nspec instance)
        {
            // parent chain

            RecurseAncestors(c => c.RunBefores(instance));

            // class (method-level)

            if (BeforeInstance != null && BeforeInstanceAsync != null)
            {
                throw new ArgumentException("A single class cannot have both a sync and an async class-level 'before_each' set, please pick one of the two");
            }

            BeforeInstance.SafeInvoke(instance);

            BeforeInstanceAsync.SafeInvoke(instance);

            // context-level

            if (Before != null && BeforeAsync != null)
            {
                throw new ArgumentException("A single context cannot have both a 'before' and an 'beforeAsync' set, please pick one of the two");
            }

            Before.SafeInvoke();

            BeforeAsync.SafeInvoke();
        }
Example #5
0
        public override void Build(nspec unused = null)
        {
            BeforeAllChain.BuildMethodLevel(classHierarchy);

            BeforeChain.BuildMethodLevel(classHierarchy);

            ActChain.BuildMethodLevel(classHierarchy);

            AfterChain.BuildMethodLevel(classHierarchy);

            AfterAllChain.BuildMethodLevel(classHierarchy);

            try
            {
                var nspec = SpecType.CreateInstanceAs <nspec>();

                nspec.tagsFilter = tagsFilter ?? new Tags();

                base.Build(nspec);
            }
            catch (Exception ex)
            {
                cantCreateInstance = true;

                AddFailingExample(ex);
            }
        }
Example #6
0
        void RunBeforeAll(nspec instance)
        {
            // context-level

            if (BeforeAll != null && BeforeAllAsync != null)
            {
                throw new AsyncMismatchException(
                          "A single context cannot set both a 'beforeAll' and an 'beforeAllAsync', please pick one of the two");
            }

            if (BeforeAll != null && BeforeAll.IsAsync())
            {
                throw new AsyncMismatchException(
                          "'beforeAll' cannot be set to an async delegate, please use 'beforeAllAsync' instead");
            }

            BeforeAll.SafeInvoke();

            BeforeAllAsync.SafeInvoke();

            // class (method-level)

            if (BeforeAllInstance != null && BeforeAllInstanceAsync != null)
            {
                throw new AsyncMismatchException(
                          "A spec class with all its ancestors cannot set both sync and async class-level 'before_all' hooks, they should either be all sync or all async");
            }

            BeforeAllInstance.SafeInvoke(instance);

            BeforeAllInstanceAsync.SafeInvoke(instance);
        }
Example #7
0
        public void Exercise(Example example, nspec nspec)
        {
            if (example.Pending) return;

            if (contextLevelFailure != null)
            {
                example.Exception = contextLevelFailure;
                return;
            }

            try
            {
                RunBefores(nspec);

                RunActs(nspec);

                example.Run(nspec);

                Afters();
            }
            catch (TargetInvocationException e)
            {
                example.Exception = e.InnerException;
            }
            catch (Exception e)
            {
                example.Exception = e;
            }
        }
Example #8
0
        public void RunActs(nspec instance)
        {
            // parent chain

            RecurseAncestors(c => c.RunActs(instance));

            // class (method-level)

            if (ActInstance != null && ActInstanceAsync != null)
            {
                throw new ArgumentException("A single class cannot have both a sync and an async class-level 'act_each' set, please pick one of the two");
            }

            ActInstance.SafeInvoke(instance);

            ActInstanceAsync.SafeInvoke(instance);

            // context-level

            if (Act != null && ActAsync != null)
            {
                throw new ArgumentException("A single context cannot have both an 'act' and an 'actAsync' set, please pick one of the two");
            }

            Act.SafeInvoke();

            ActAsync.SafeInvoke();
        }
Example #9
0
        public bool RunAndHandleException(Action <nspec> action, nspec nspec, ref Exception exceptionToSet)
        {
            bool hasThrown = false;

            try
            {
                action(nspec);
            }
            catch (TargetInvocationException invocationException)
            {
                if (exceptionToSet == null)
                {
                    exceptionToSet = nspec.ExceptionToReturn(invocationException.InnerException);
                }

                hasThrown = true;
            }
            catch (Exception exception)
            {
                if (exceptionToSet == null)
                {
                    exceptionToSet = nspec.ExceptionToReturn(exception);
                }

                hasThrown = true;
            }

            return(hasThrown);
        }
Example #10
0
    public static TestEntity CreateEntity(this nspec spec)
    {
        var entity = new TestEntity();

        entity.Initialize(0, CID.TotalComponents, new Stack <IComponent> [CID.TotalComponents]);
        return(entity);
    }
Example #11
0
 public void Run(nspec instance)
 {
     if (CanRun(instance))
     {
         ContextUtils.RunAndHandleException(InvokeHooks, instance, ref exception);
     }
 }
Example #12
0
        public static bool RunAndHandleException(Action <nspec> action, nspec instance, ref Exception targetException)
        {
            bool      hasThrown      = false;
            Exception exceptionToSet = null;

            try
            {
                action(instance);
            }
            catch (TargetInvocationException invocationException)
            {
                exceptionToSet = instance.ExceptionToReturn(invocationException.InnerException);

                hasThrown = true;
            }
            catch (Exception exception)
            {
                exceptionToSet = instance.ExceptionToReturn(exception);

                hasThrown = true;
            }

            if (targetException == null && exceptionToSet != null)
            {
                targetException = exceptionToSet;
            }

            return(hasThrown);
        }
Example #13
0
        public override void Build(nspec unused = null)
        {
            BuildMethodLevelBefore();

            BuildMethodLevelBeforeAll();

            BuildMethodLevelAct();

            BuildMethodLevelAfter();

            BuildMethodLevelAfterAll();

            try
            {
                var nspec = SpecType.CreateInstanceAs <nspec>();

                nspec.tagsFilter = tagsFilter ?? new Tags();

                base.Build(nspec);
            }
            catch (Exception ex)
            {
                cantCreateInstance = true;

                AddFailingExample(ex);
            }
        }
Example #14
0
        public void RunAfters(nspec instance)
        {
            // context-level

            if (After != null && AfterAsync != null)
            {
                throw new ArgumentException("A single context cannot have both an 'after' and an 'afterAsync' set, please pick one of the two");
            }

            After.SafeInvoke();

            AfterAsync.SafeInvoke();

            // class (method-level)

            if (AfterInstance != null && AfterInstanceAsync != null)
            {
                throw new ArgumentException("A single class cannot have both a sync and an async class-level 'after_each' set, please pick one of the two");
            }

            AfterInstance.SafeInvoke(instance);

            AfterInstanceAsync.SafeInvoke(instance);

            // parent chain

            RecurseAncestors(c => c.RunAfters(instance));
        }
Example #15
0
        protected virtual void InvokeHooks(nspec instance)
        {
            // class (method-level)
            InvokeClassHooks(instance);

            // context-level
            InvokeContextHooks();
        }
Example #16
0
        public void RunBefores(nspec instance)
        {
            RecurseAncestors(c => c.RunBefores(instance));

            BeforeInstance.SafeInvoke(instance);

            Before.SafeInvoke();
        }
Example #17
0
        bool AnyUnfilteredExampleInSubTree(nspec nspec)
        {
            Func <ExampleBase, bool> shouldNotSkip = e => e.ShouldNotSkip(nspec.tagsFilter);

            bool anyExampleOrSubExample = Examples.Any(shouldNotSkip) || Contexts.Examples().Any(shouldNotSkip);

            return(anyExampleOrSubExample);
        }
Example #18
0
        public virtual void Build(nspec instance = null)
        {
            instance.Context = this;

            savedInstance = instance;

            Contexts.Do(c => c.Build(instance));
        }
Example #19
0
        public virtual void Build(nspec instance=null)
        {
            instance.Context = this;

            savedInstance = instance;

            Contexts.Do(c => c.Build(instance));
        }
Example #20
0
        public void RunActs(nspec instance)
        {
            RecurseAncestors(c => c.RunActs(instance));

            ActInstance.SafeInvoke(instance);

            Act.SafeInvoke();
        }
Example #21
0
        public void RunAfters(nspec instance)
        {
            After.SafeInvoke();

            AfterInstance.SafeInvoke(instance);

            RecurseAncestors(c => c.RunAfters(instance));
        }
Example #22
0
        public override void Run(bool failFast, nspec instance = null, bool recurse = true)
        {
            if (cantCreateInstance)
            {
                return;
            }

            base.Run(failFast, instance, recurse);
        }
Example #23
0
        /// <summary>
        /// Test execution happens in two phases: this is the first phase.
        /// </summary>
        /// <remarks>
        /// Here all contexts and all their examples are run, collecting distinct exceptions
        /// from context itself (befores/ acts/ it/ afters), beforeAll, afterAll.
        /// </remarks>
        public virtual void Run(ILiveFormatter formatter, bool failFast, nspec instance = null, bool recurse = true)
        {
            if (failFast && Parent.HasAnyFailures())
            {
                return;
            }

            var nspec = savedInstance ?? instance;

            bool runBeforeAfterAll = AnyUnfilteredExampleInSubTree(nspec);

            using (new ConsoleCatcher(output => this.CapturedOutput = output))
            {
                if (runBeforeAfterAll)
                {
                    RunAndHandleException(RunBeforeAll, nspec, ref ExceptionBeforeAll);
                }
            }

            // intentionally using for loop to prevent collection was modified error in sample specs
            for (int i = 0; i < Examples.Count; i++)
            {
                var example = Examples[i];

                if (failFast && example.Context.HasAnyFailures())
                {
                    return;
                }

                using (new ConsoleCatcher(output => example.CapturedOutput = output))
                {
                    Exercise(example, nspec);
                }

                if (example.HasRun && !alreadyWritten)
                {
                    WriteAncestors(formatter);
                    alreadyWritten = true;
                }

                if (example.HasRun)
                {
                    formatter.Write(example, Level);
                }
            }

            if (recurse)
            {
                Contexts.Do(c => c.Run(formatter, failFast, nspec));
            }

            if (runBeforeAfterAll)
            {
                RunAndHandleException(RunAfterAll, nspec, ref ExceptionAfterAll);
            }
        }
Example #24
0
        public override void Build(nspec instance = null)
        {
            BuildMethodLevelBefore();

            BuildMethodLevelAct();

            var nspec = type.Instance <nspec>();

            base.Build(nspec);
        }
Example #25
0
        public override void Build(nspec instance=null)
        {
            BuildMethodLevelBefore();

            BuildMethodLevelAct();

            var nspec = type.Instance<nspec>();

            base.Build(nspec);
        }
Example #26
0
        public override void Run(nspec nspec)
        {
            if (IsAsync)
            {
                throw new AsyncMismatchException(
                          "'it[]' cannot be set to an async delegate, please use 'itAsync[]' instead");
            }

            action();
        }
Example #27
0
        static void AddFailingExample(nspec instance, Exception targetEx)
        {
            var reportedEx = (targetEx.InnerException != null)
                ? targetEx.InnerException
                : targetEx;

            string exampleName = "Method context body throws an exception of type '{0}'".With(reportedEx.GetType().Name);

            instance.it[exampleName] = () => { throw new ContextBareCodeException(reportedEx); };
        }
Example #28
0
        public virtual void Run(nspec instance = null)
        {
            var nspec = savedInstance ?? instance;

            Contexts.Do(c => c.Run(nspec));

            for (int i = 0; i < Examples.Count; i++)
            {
                Exercise(Examples[i], nspec);
            }
        }
Example #29
0
        public override void RunPending(nspec nspec)
        {
            // don't run example body, as this example is being skipped;
            // just check for consistency in passed example body

            if (IsAsync)
            {
                throw new AsyncMismatchException(
                          "'xit' cannot be set to an async delegate, please use 'xitAsync' instead");
            }
        }
Example #30
0
 public override void Run(Formatters.ILiveFormatter formatter, bool failFast, nspec instance = null, bool recurse = true)
 {
     if (cantCreateInstance)
     {
         // flag the one and only failing example as being run;
         // nothing else is needed: no parents, no childs, no before/after hooks
         Examples.Single().HasRun = true;
     }
     else
     {
         base.Run(formatter, failFast, instance, recurse);
     }
 }
Example #31
0
        public override void Build(nspec instance)
        {
            base.Build(instance);

            try
            {
                method.Invoke(instance, null);
            }
            catch (Exception ex)
            {
                AddFailingExample(instance, ex);
            }
        }
Example #32
0
        public override void Build(nspec instance = null)
        {
            BuildMethodLevelBefore();

            BuildMethodLevelAct();

            BuildMethodLevelAfter();

            var nspec = type.Instance<nspec>();

            nspec.tagsFilter = tagsFilter ?? new Tags();

            base.Build(nspec);
        }
Example #33
0
        public override void Run(nspec instance = null)
        {
            var nspec = type.Instance <nspec>();

            base.Run(nspec);

            //haven't figured out how to write a failing test but
            //using regular iteration causes Collection was modified
            //exception when running samples (rake samples)
            for (int i = 0; i < Examples.Count; i++)
            {
                Run(Examples[i], nspec);
            }
        }
Example #34
0
        public override void Build(nspec instance)
        {
            base.Build(instance);

            try
            {
                method.Invoke(instance, null);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception executing context: {0}".With(FullContext()));

                throw e;
            }
        }
Example #35
0
        protected void InvokeClassHooks(nspec instance)
        {
            // class (method-level)

            if (ClassHook != null && AsyncClassHook != null)
            {
                throw new AsyncMismatchException(
                          $"A spec class with all its ancestors cannot set both sync and async " +
                          $"class-level '{classHookName}' hooks, they should either be all sync or all async");
            }

            ClassHook.SafeInvoke(instance);

            AsyncClassHook.SafeInvoke(instance);
        }
Example #36
0
        public override void Run(nspec instance)
        {
            base.Run(instance);

            try
            {
                method.Invoke(instance, null);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception executing context: {0}".With(FullContext()));

                throw e;
            }
        }
Example #37
0
        public void Run(nspec nspec)
        {
            if (method.ReturnType == typeof(void))
            {
                throw new ArgumentException("'async void' method-level {0} is not supported, please use 'async Task' instead", hookName);
            }

            if (method.ReturnType.IsGenericType)
            {
                throw new ArgumentException("'async Task<T>' method-level {0} is not supported, please use 'async Task' instead", hookName);
            }

            Func <Task> asyncWork = () => (Task)method.Invoke(nspec, null);

            asyncWork.Offload();
        }
        public void Run(nspec nspec)
        {
            if (method.ReturnType == typeof(void))
            {
                throw new ArgumentException("'async void' method-level {0} is not supported, please use 'async Task' instead", hookName);
            }

            if (method.ReturnType.IsGenericType)
            {
                throw new ArgumentException("'async Task<T>' method-level {0} is not supported, please use 'async Task' instead", hookName);
            }

            Func<Task> asyncWork = () => (Task)method.Invoke(nspec, null);

            asyncWork.Offload();
        }
Example #39
0
 public void Run(nspec nspec)
 {
     if (MethodLevelExample != null)
     {
         try
         {
             MethodLevelExample.Invoke(nspec, null);
         }
         catch (Exception e)
         {
             Exception = e.InnerException;
         }
     }
     else
     {
         action();
     }
 }
Example #40
0
        void RunBeforeAll(nspec instance)
        {
            // context-level

            if (BeforeAll != null && BeforeAllAsync != null)
            {
                throw new ArgumentException("A single context cannot have both a 'beforeAll' and an 'beforeAllAsync' set, please pick one of the two");
            }

            BeforeAll.SafeInvoke();

            BeforeAllAsync.SafeInvoke();

            // class (method-level)

            if (BeforeAllInstance != null && BeforeAllInstanceAsync != null)
            {
                throw new ArgumentException("A single class cannot have both a sync and an async class-level 'before_all' set, please pick one of the two");
            }

            BeforeAllInstance.SafeInvoke(instance);

            BeforeAllInstanceAsync.SafeInvoke(instance);
        }
Example #41
0
        public virtual void Run(nspec instance = null)
        {
            var nspec = savedInstance ?? instance;

            Contexts.Do(c => c.Run(nspec));

            for (int i = 0; i < Examples.Count; i++)
                Exercise(Examples[i], nspec);
        }
Example #42
0
        public void Run(nspec nspec)
        {
            if (MethodLevelExample != null) MethodLevelExample.Invoke(nspec, null);

            else action();
        }
Example #43
0
 public abstract void Run(nspec nspec);
Example #44
0
 public override void Run(nspec nspec)
 {
     action();
 }
Example #45
0
        public void RunAfters(nspec instance)
        {
            After.SafeInvoke();

            AfterInstance.SafeInvoke(instance);

            RecurseAncestors(c => c.RunAfters(instance));
        }
Example #46
0
 public override void Run(nspec nspec)
 {
     method.Invoke(nspec, null);
 }
Example #47
0
        bool AnyUnfilteredExampleInSubTree(nspec nspec)
        {
            Func<ExampleBase, bool> shouldNotSkip = e => e.ShouldNotSkip(nspec.tagsFilter);

            bool anyExampleOrSubExample = Examples.Any(shouldNotSkip) || Contexts.Examples().Any(shouldNotSkip);

            return anyExampleOrSubExample;
        }
Example #48
0
        public void Exercise(Example example, nspec nspec)
        {
            if (example.ShouldSkip(nspec.tagsFilter)) return;

            RunAndHandleException(RunBefores, nspec, ref Exception);

            RunAndHandleException(RunActs, nspec, ref Exception);

            RunAndHandleException(example.Run, nspec, ref example.Exception);

            RunAndHandleException(RunAfters, nspec, ref Exception);

            example.AssignProperException(Exception);
        }
Example #49
0
 public virtual void Run(nspec nspec)
 {
     action();
 }
Example #50
0
        void RunBeforeAll(nspec instance)
        {
            BeforeAll.SafeInvoke();

            BeforeAllInstance.SafeInvoke(instance);
        }
Example #51
0
        public void RunBefores(nspec instance)
        {
            RecurseAncestors(c => c.RunBefores(instance));

            BeforeInstance.SafeInvoke(instance);

            Before.SafeInvoke();
        }
Example #52
0
 public void RunAndHandleException(Action<nspec> action, nspec nspec, ref Exception exceptionToSet)
 {
     try
     {
         action(nspec);
     }
     catch (TargetInvocationException invocationException)
     {
         if (exceptionToSet == null) exceptionToSet = nspec.ExceptionToReturn(invocationException.InnerException);
     }
     catch (Exception exception)
     {
         if (exceptionToSet == null) exceptionToSet = nspec.ExceptionToReturn(exception);
     }
 }
Example #53
0
        public void RunActs(nspec instance)
        {
            if (Parent != null) Parent.RunActs(instance);

            if (ActInstance != null) ActInstance(instance);

            if (Act != null) Act();
        }
Example #54
0
        public void RunActs(nspec instance)
        {
            RecurseAncestors(c => c.RunActs(instance));

            ActInstance.SafeInvoke(instance);

            Act.SafeInvoke();
        }
Example #55
0
        public void RunBefores(nspec instance)
        {
            if (Parent != null) Parent.RunBefores(instance);

            if (BeforeInstance != null) BeforeInstance(instance);

            if (Before != null) Before();
        }
Example #56
0
        public virtual void Run(ILiveFormatter formatter, bool failFast, nspec instance = null)
        {
            if (failFast && Parent.HasAnyFailures()) return;

            var nspec = savedInstance ?? instance;

            if(AllExamples().Any(e=>e.ShouldNotSkip(nspec.tagsFilter))) RunAndHandleException(RunBeforeAll, nspec, ref Exception);

            //intentionally using for loop to prevent collection was modified error in sample specs
            for (int i = 0; i < Examples.Count; i++)
            {
                var example = Examples[i];
                if (failFast && example.Context.HasAnyFailures()) return;

                Exercise(example, nspec);

                if (example.HasRun && !alreadyWritten)
                {
                    WriteAncestors(formatter);
                    alreadyWritten = true;
                }

                if (example.HasRun) formatter.Write(example, Level);
            }

            Contexts.Do(c => c.Run(formatter, failFast, nspec));

            if (AllExamples().Count() > 0) RunAndHandleException(RunAfterAll, nspec, ref Exception);
        }
Example #57
0
        public void Exercise(ExampleBase example, nspec nspec)
        {
            if (example.ShouldSkip(nspec.tagsFilter)) return;

            RunAndHandleException(RunBefores, nspec, ref Exception);

            RunAndHandleException(RunActs, nspec, ref Exception);

            RunAndHandleException(example.Run, nspec, ref example.Exception);

            bool exceptionThrownInAfters = RunAndHandleException(RunAfters, nspec, ref Exception);

            // when an expected exception is thrown and is set to be cleared by 'expect<>',
            // a subsequent exception thrown in 'after' hooks would go unnoticed, so do not clear in this case

            if (exceptionThrownInAfters) ClearExpectedException = false;
        }
Example #58
0
        public virtual void Run(ILiveFormatter formatter, bool failFast, nspec instance = null)
        {
            if (failFast && Parent.HasAnyFailures()) return;

            var nspec = savedInstance ?? instance;

            for (int i = 0; i < Examples.Count; i++)
            {
                var example = Examples[i];

                if (failFast && example.Context.HasAnyFailures()) return;

                Exercise(example, nspec);

                if (example.HasRun && !alreadyWritten)
                {
                    WriteAncestors(formatter);
                    alreadyWritten = true;
                }

                if (example.HasRun) formatter.Write(example, Level);
            }

            Contexts.Do(c => c.Run(formatter, failFast, nspec));
        }
Example #59
0
        public void SetInstanceContext(nspec instance)
        {
            if (BeforeInstance != null) Before = () => BeforeInstance(instance);

            if (ActInstance != null) Act = () => ActInstance(instance);

            if(Parent!=null) Parent.SetInstanceContext(instance);
        }
Example #60
0
        public void RunAfterAll(nspec instance)
        {
            AfterAll.SafeInvoke();

            AfterAllInstance.SafeInvoke(instance);
        }