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(); }
/// <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); }
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; } }
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); } }
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); }
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; } }
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(); }
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); }
public static TestEntity CreateEntity(this nspec spec) { var entity = new TestEntity(); entity.Initialize(0, CID.TotalComponents, new Stack <IComponent> [CID.TotalComponents]); return(entity); }
public void Run(nspec instance) { if (CanRun(instance)) { ContextUtils.RunAndHandleException(InvokeHooks, instance, ref exception); } }
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); }
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); } }
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)); }
protected virtual void InvokeHooks(nspec instance) { // class (method-level) InvokeClassHooks(instance); // context-level InvokeContextHooks(); }
public void RunBefores(nspec instance) { RecurseAncestors(c => c.RunBefores(instance)); BeforeInstance.SafeInvoke(instance); Before.SafeInvoke(); }
bool AnyUnfilteredExampleInSubTree(nspec nspec) { Func <ExampleBase, bool> shouldNotSkip = e => e.ShouldNotSkip(nspec.tagsFilter); bool anyExampleOrSubExample = Examples.Any(shouldNotSkip) || Contexts.Examples().Any(shouldNotSkip); return(anyExampleOrSubExample); }
public virtual void Build(nspec instance = null) { instance.Context = this; savedInstance = instance; Contexts.Do(c => c.Build(instance)); }
public virtual void Build(nspec instance=null) { instance.Context = this; savedInstance = instance; Contexts.Do(c => c.Build(instance)); }
public void RunActs(nspec instance) { RecurseAncestors(c => c.RunActs(instance)); ActInstance.SafeInvoke(instance); Act.SafeInvoke(); }
public void RunAfters(nspec instance) { After.SafeInvoke(); AfterInstance.SafeInvoke(instance); RecurseAncestors(c => c.RunAfters(instance)); }
public override void Run(bool failFast, nspec instance = null, bool recurse = true) { if (cantCreateInstance) { return; } base.Run(failFast, instance, recurse); }
/// <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); } }
public override void Build(nspec instance = null) { BuildMethodLevelBefore(); BuildMethodLevelAct(); var nspec = type.Instance <nspec>(); base.Build(nspec); }
public override void Build(nspec instance=null) { BuildMethodLevelBefore(); BuildMethodLevelAct(); var nspec = type.Instance<nspec>(); base.Build(nspec); }
public override void Run(nspec nspec) { if (IsAsync) { throw new AsyncMismatchException( "'it[]' cannot be set to an async delegate, please use 'itAsync[]' instead"); } action(); }
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); }; }
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); } }
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"); } }
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); } }
public override void Build(nspec instance) { base.Build(instance); try { method.Invoke(instance, null); } catch (Exception ex) { AddFailingExample(instance, ex); } }
public override void Build(nspec instance = null) { BuildMethodLevelBefore(); BuildMethodLevelAct(); BuildMethodLevelAfter(); var nspec = type.Instance<nspec>(); nspec.tagsFilter = tagsFilter ?? new Tags(); base.Build(nspec); }
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); } }
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; } }
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); }
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; } }
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(); }
public void Run(nspec nspec) { if (MethodLevelExample != null) { try { MethodLevelExample.Invoke(nspec, null); } catch (Exception e) { Exception = e.InnerException; } } else { action(); } }
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); }
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); }
public void Run(nspec nspec) { if (MethodLevelExample != null) MethodLevelExample.Invoke(nspec, null); else action(); }
public abstract void Run(nspec nspec);
public override void Run(nspec nspec) { action(); }
public override void Run(nspec nspec) { method.Invoke(nspec, null); }
bool AnyUnfilteredExampleInSubTree(nspec nspec) { Func<ExampleBase, bool> shouldNotSkip = e => e.ShouldNotSkip(nspec.tagsFilter); bool anyExampleOrSubExample = Examples.Any(shouldNotSkip) || Contexts.Examples().Any(shouldNotSkip); return anyExampleOrSubExample; }
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); }
public virtual void Run(nspec nspec) { action(); }
void RunBeforeAll(nspec instance) { BeforeAll.SafeInvoke(); BeforeAllInstance.SafeInvoke(instance); }
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); } }
public void RunActs(nspec instance) { if (Parent != null) Parent.RunActs(instance); if (ActInstance != null) ActInstance(instance); if (Act != null) Act(); }
public void RunBefores(nspec instance) { if (Parent != null) Parent.RunBefores(instance); if (BeforeInstance != null) BeforeInstance(instance); if (Before != null) Before(); }
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); }
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; }
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)); }
public void SetInstanceContext(nspec instance) { if (BeforeInstance != null) Before = () => BeforeInstance(instance); if (ActInstance != null) Act = () => ActInstance(instance); if(Parent!=null) Parent.SetInstanceContext(instance); }
public void RunAfterAll(nspec instance) { AfterAll.SafeInvoke(); AfterAllInstance.SafeInvoke(instance); }