public Specification SpecFactory(string description, Action function, Definition suite, string codeBase, int lineNumber, int column, string fileName, string className) { var spec = new Specification() { Id = String.Format("spec://{0}/{1}:{2}", className, lineNumber, column), // Id = suite.Parent.Description + ".spec" + string.Format("{0}x{1}", lineNumber, column), /* it.Id*/ Description = description, Fn = function, Parent = suite, CodeBase = codeBase, LineNumber = lineNumber, Column = column, FileName = fileName, ClassName = className }; if(_executableLookupTable.Any(x=>x.Id == spec.Id)) { var t = (from p in _executableLookupTable where p.Id.StartsWith(spec.Id) select p).Count(); spec.Id = spec.Id + string.Format("({0})", t); } _executableLookupTable.Add(spec); return spec; }
public void AddSpecToSuites(Definition suite, Action specDefinition) { var parentSuite = _currentSuite; _currentSuite = suite; parentSuite.AddChild(_currentSuite); SafeExecute(specDefinition); _currentSuite = parentSuite; }
private void PrintDefinition(Definition def, int level = 2) { Console.ForegroundColor = IsDefinitionASpec(def) ? ConsoleColor.White : ConsoleColor.Gray; if (!IsRootDefinition(def)) { Console.WriteLine("{0}{1}", new String(' ', level), Truncate(def.Description, 70)); } def.Children.ForEach(x => { PrintDefinition(x, level + 1); }); if (IsDefinitionASpec(def)) { Console.SetCursorPosition(1, Console.CursorTop - 1); var result = _execresult.FirstOrDefault(x => x.Id == def.Id); PrintSpecStatus(result.RanSuccesfully, result.Enabled); if (result.Enabled && !result.RanSuccesfully) { Console.WriteLine("{0}{1}", new String(' ', level), CleanUpForConsole(Truncate(result.ExecutionResult.Trim(), 120))); } } }
private void AddAfterEach(Definition spec, List<Definition> specifications) { if (spec.AfterEach.Count > 0) { foreach (var hook in spec.AfterEach) { specifications.Add(hook); } } if (spec.AfterAll.Count > 0) { foreach (var hook in spec.AfterAll.AsEnumerable()) { specifications.Add(hook); } } if (spec.Parent != null) { AddAfterEach(spec.Parent, specifications); } }
private void AddBeforeEachs(Definition spec, List<Definition> specifications) { //insert current beforeachs if (spec.BeforeEach.Count > 0) { foreach (var hook in spec.BeforeEach.AsEnumerable().Reverse()) { specifications.Insert(0, hook); } } if (spec.BeforeAll.Count > 0) { foreach (var hook in spec.BeforeAll.AsEnumerable().Reverse()) { specifications.Insert(0, hook); } } if (spec.Parent != null) { AddBeforeEachs(spec.Parent, specifications); } }
private void SafeExecute(Definition child) { try { child.StartTime = DateTime.Now; if (child.Enabled) { child.ExecutionStatus = ExecStatus.Running; if (IsAsyncAppliedToDelegate(child.Fn)) { child.Fn1 = async () => { child.Fn(); }; asyncExecute(child); } else { execute(child); } child.RanSuccesfully = true; } else { child.RanSuccesfully = false; } } catch (Exception e) { child.ExecutionResult = e.Message; child.RanSuccesfully = false; child.Parent.RanSuccesfully = false; child.StackTrace = e.StackTrace; } finally { child.EndTime = DateTime.Now; child.ExecutionStatus = ExecStatus.Completed; } }
private async void asyncExecute(Definition child) { await child.Fn1(); }
private void execute(Definition child) { child.Fn(); }
public void AddChild(Definition spec) { Children.Add(spec); }
public Hook EachFactory(string description, Action fnAction, Definition suite) { return new Hook() {Description = description, Fn = fnAction, Enabled = true, Parent = suite}; }
private bool IsRootDefinition(Definition def) { return def.Parent == null; }
private bool IsDefinitionASpec(Definition definition) { return definition.GetType().IsAssignableFrom(typeof (Specification)) && !definition.GetType().IsAssignableFrom(typeof (Suite)); }