Skip to content

Instantly share code, notes, and snippets.

@BennieCopeland
Last active January 4, 2016 01:52
Show Gist options
  • Save BennieCopeland/cb9f2b27c81dd6a795ec to your computer and use it in GitHub Desktop.
Save BennieCopeland/cb9f2b27c81dd6a795ec to your computer and use it in GitHub Desktop.
Changes to NSpec docs to show execution order
using NSpec;
using System;
namespace NSpecExecutionOrder
{
abstract class parent_class : nspec // this can be either an abstract class, or just a class
{
public const int indentSize = 3;
public static string order = "\n\n";
public static int indent = 0;
void Print(string s)
{
Console.WriteLine(s);
}
protected void Increase(string s)
{
Write(s);
indent += indentSize;
}
protected void Decrease(string s)
{
indent -= indentSize;
Write(s);
}
protected void Write(string s)
{
order += s.PadLeft(s.Length + indent);
}
void before_all()
{
Increase("parent: before all\n");
}
void before_each()
{
Write("parent: before each\n");
}
void it_works_parent_1()
{
Write("parent: it works 1\n");
}
void it_works_parent_2()
{
Write("parent: it works 2\n");
}
void after_each()
{
Write("parent: after each\n\n");
}
void after_all()
{
Decrease("parent: after all\n");
Print(order);
}
}
class child_class : parent_class
{
void before_all()
{
Increase("child: before all\n");
}
void before_each()
{
Write("child: before each\n");
}
void it_works_child_3()
{
Write("child: it works 3\n");
}
void it_works_child_4()
{
Write("child: it works 4\n");
}
void after_each()
{
Write("child: after each\n");
}
void after_all()
{
Decrease("child: after all\n");
}
void method_level_context()
{
beforeAll = () => Increase("method: before all\n");
before = () => Write("method: before each\n");
it["it works method 5"] = () => Write("method: it works 5\n");
it["it works method 6"] = () => Write("method: it works 6\n");
after = () => Write("method: after each\n");
afterAll = () => Decrease("method: after all\n");
context["sub context"] = () =>
{
beforeAll = () => Increase("sub: before all\n");
before = () => Write("sub: before each\n");
it["it works sub 7"] = () => Write("sub: it works 7 \n");
it["it works sub 8"] = () => Write("sub: it works 8 \n");
after = () => Write("sub: after each\n");
afterAll = () => Decrease("sub: after all\n");
};
}
}
}
parent: before all
child: before all
parent: before each
child: before each
parent: it works 1
child: after each
parent: after each
parent: before each
child: before each
parent: it works 2
child: after each
parent: after each
parent: before each
child: before each
child: it works 3
child: after each
parent: after each
parent: before each
child: before each
child: it works 4
child: after each
parent: after each
method: before all
parent: before each
child: before each
method: before each
method: it works 5
method: after each
child: after each
parent: after each
parent: before each
child: before each
method: before each
method: it works 6
method: after each
child: after each
parent: after each
sub: before all
parent: before each
child: before each
method: before each
sub: before each
sub: it works 7
sub: after each
method: after each
child: after each
parent: after each
parent: before each
child: before each
method: before each
sub: before each
sub: it works 8
sub: after each
method: after each
child: after each
parent: after each
sub: after all
method: after all
child: after all
parent: after all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment