Created
April 13, 2012 07:01
-
-
Save thefringeninja/2374644 to your computer and use it in GitHub Desktop.
Simple.Testing Doc Generator Pretty Print Enumerables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SimpleRunner.RunAllInAssembly(typeof(Program).Assembly).ForEach(PrintSpec); | |
} | |
private static string NicePrint(object target) | |
{ | |
if (target == null) | |
{ | |
return "???"; | |
} | |
var s = target as string; | |
if (s != null) | |
{ | |
return s; | |
} | |
if (target is IEnumerable) | |
{ | |
return (target as IEnumerable) | |
.OfType<object>() | |
.Aggregate(new StringBuilder(), | |
(builder, x) => builder.AppendLine(x.ToString()), | |
builder => builder.ToString()); | |
} | |
return target.ToString(); | |
} | |
private static void PrintSpec(RunResult result) | |
{ | |
var passed = result.Passed ? "Passed" : "Failed"; | |
Console.WriteLine(result.Name.Replace('_', ' ') + " - " + passed); | |
var on = result.GetOnResult(); | |
if (on != null) | |
{ | |
Console.WriteLine(); | |
Console.WriteLine("On:"); | |
Console.WriteLine(NicePrint(on)); | |
Console.WriteLine(); | |
} | |
if (result.Result != null) | |
{ | |
Console.WriteLine(); | |
Console.WriteLine("Results with:"); | |
if (result.Result is Exception) | |
Console.WriteLine(result.Result.GetType() + "\n" + ((Exception)result.Result).Message); | |
else | |
Console.WriteLine(NicePrint(result.Result)); | |
Console.WriteLine(); | |
} | |
Console.WriteLine("Expectations:"); | |
foreach (var expecation in result.Expectations) | |
{ | |
if (expecation.Passed) | |
Console.WriteLine("\t" + expecation.Text + " " + (expecation.Passed ? "Passed" : "Failed")); | |
else | |
Console.WriteLine(expecation.Exception.Message); | |
} | |
if (result.Thrown != null) | |
{ | |
Console.WriteLine("Specification failed: " + result.Message); | |
Console.WriteLine(); | |
Console.WriteLine(result.Thrown); | |
} | |
Console.WriteLine(new string('-', 80)); | |
Console.WriteLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment