Skip to content

Instantly share code, notes, and snippets.

@thefringeninja
Created April 13, 2012 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefringeninja/2374644 to your computer and use it in GitHub Desktop.
Save thefringeninja/2374644 to your computer and use it in GitHub Desktop.
Simple.Testing Doc Generator Pretty Print Enumerables
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