private SpecInfo getRunInfo(SpecificationMethodInfo spec) { var formatter = getFormatter(spec.Specification); var specInfo = new SpecInfo(formatter); specInfo.Name = getSpecName(spec.MethodInfo.DeclaringType, spec.MethodInfo); spec.UpdateSkipInformation(specInfo); if (specInfo.Skipped.IsSkipped) { spec.Specification.EnrichDescription(specInfo, formatter); return specInfo; } specInfo.ReportSpecExecutionHasTriggered(); try { spec.Specification.Run(specInfo, getFormatter(spec.Specification)); } catch (Exception e) { specInfo.Exception = e; } return specInfo; }
public void VerifyTo(object[] input, SpecInfo results, MessageFormatter formatter) { if (input.Any(x => _comparer.Compare(x, _expected).AreEqual)) { results.ReportExpectationPass(this); return; } results.ReportExpectationFail(this, new ExpectationFailedException(this, input)); }
private SpecInfo getdescriptionInfo(SpecificationMethodInfo spec) { var formatter = getFormatter(spec.Specification); var specInfo = new SpecInfo(formatter); specInfo.Name = getSpecName(spec.MethodInfo.DeclaringType, spec.MethodInfo); spec.Specification.EnrichDescription(specInfo, getFormatter(spec.Specification)); spec.UpdateSkipInformation(specInfo); return specInfo; }
public void Specification(MethodInfo testMethod) { var inconclusive = false; var testResult = new SpecInfo(); var skip = testMethod.GetCustomAttributes().OfType<SkipAttributeContract>() .FirstOrDefault(); if (skip != null) { testResult.ReportSkipped(skip.Reason); inconclusive = true; } else { testResult.ReportSpecExecutionHasTriggered(); } try { testResult.Name = getSpecName(testMethod.DeclaringType, testMethod); var spec = testMethod.Invoke(this, new object[0]) as Specification; var formatter = MessageFormatterRegistry.GetFormatter(spec.SpecificationCategory); testResult.UseFormatter(formatter); if (skip != null) spec.EnrichDescription(testResult, formatter); else testResult = spec.Run(testResult, formatter); } catch (Exception e) { testResult.Exception = e; } var sb = new StringBuilder(); sb.AppendLine(getDescription(testResult)); var description = sb.ToString(); if(inconclusive) Assert.Inconclusive(testResult.Skipped.Reason); else if (!testResult.Passed) Assert.Fail(description); else { Console.WriteLine(description); Assert.Pass(description); } }
public SpecCategory Add(SpecInfo spec) { _specs.Add(spec); return this; }
private string getDescription(SpecInfo spec) { if (spec.Passed) return spec.ToString(); var prefix = spec.Skipped.IsSkipped ? " ??? " : " ==> "; var basicString = spec.ToString(); var lines = basicString.Split(new[] {Environment.NewLine}, StringSplitOptions.None); var sb = new StringBuilder(); for (int i = 0; i < lines.Length; i++) { if (i != 0 && i != 1 && i != lines.Length - 1) sb.Append(prefix); sb.AppendLine(lines[i]); } return sb.ToString(); }
public void DescribeTo(SpecInfo spec, MessageFormatter formatter) { spec.ReportExpectation(formatter.FormatMessage(_expected)); }
private static bool shouldDisplayWhenException(SpecInfo spec) { return !spec.When.Passed && spec.Exception != null && spec.Givens.All(x => x.Passed); }
private static void printWhen(StringBuilder sb, SpecInfo spec) { sb.AppendLine("When"); sb.Append(" - "); sb.AppendLine(spec.HasExecutionBeenTriggered ? string.Format("{0} [{1}]", spec.When.Description, spec.When.Passed ? "Passed" : "Failed") : spec.When.Description); if (shouldDisplayWhenException(spec)) { sb.AppendLine(spec.Exception.ToString()); sb.AppendLine(spec.Exception.StackTrace); } }
private static void printThen(StringBuilder sb, SpecInfo spec) { sb.AppendLine("Then"); foreach (var then in spec.Thens) { sb.Append(" - "); var description = spec.HasExecutionBeenTriggered ? string.Format("{0} [{1}]", then.Description, then.Passed ? "Passed" : "Failed") : then.Description; sb.AppendLine(description); if (!then.Passed && then.Exception != null) { sb.AppendLine("_____________________________________________"); sb.AppendLine(then.Exception.ToString()); sb.AppendLine("_____________________________________________"); sb.AppendLine(); } } }
private static void printSkipped(StringBuilder sb, SpecInfo specInfo) { if (specInfo.Skipped.IsSkipped) { sb.AppendLine(specInfo.Skipped.ToString()); } }
private static void printName(StringBuilder sb, SpecInfo specInfo) { sb.AppendLine(specInfo.Name); sb.AppendLine("============================================================="); }
private static void printGiven(StringBuilder sb, SpecInfo spec) { if (spec.Givens.Length == 0) return; sb.AppendLine("Given"); foreach (var given in spec.Givens) { var description = spec.HasExecutionBeenTriggered ? string.Format("{0} [{1}]", given.Description, given.Passed ? "Passed" : "Failed") : given.Description; sb.Append(" - "); sb.AppendLine(description); } if (!spec.Givens.All(x => x.Passed) && spec.Exception != null) { sb.AppendLine(spec.Exception.ToString()); } }
public void UpdateSkipInformation(SpecInfo specInfo) { var skipAttribute = MethodInfo.DeclaringType.GetCustomAttributes().FirstOrDefault(x => x is SkipAttributeContract) ?? MethodInfo.GetCustomAttributes().FirstOrDefault(x => x is SkipAttributeContract); if (skipAttribute == null) return; var typed = (SkipAttributeContract)skipAttribute; string reason = typed.Reason; specInfo.ReportSkipped(reason); }