Skip to content

Commit

Permalink
use one test object per test case
Browse files Browse the repository at this point in the history
  • Loading branch information
linkrope committed Aug 21, 2016
1 parent e8c2ada commit 797c6ca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 50 deletions.
30 changes: 15 additions & 15 deletions example.d
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,27 @@ class TestFixture
}

@BeforeAll
public static void setUpClass()
public static void setUpAll()
{
debug writeln("@BeforeClass");
debug writeln("@BeforeAll");
}

@AfterAll
public static void tearDownClass()
public static void tearDownAll()
{
debug writeln("@AfterClass");
debug writeln("@AfterAll");
}

@BeforeEach
public void setUp()
{
debug writeln("@Before");
debug writeln("@BeforeEach");
}

@AfterEach
public void tearDown()
{
debug writeln("@After");
debug writeln("@AfterEach");
}

@Test
Expand All @@ -124,7 +124,7 @@ class TestReuse : TestFixture
@BeforeEach
public override void setUp()
{
debug writeln("@Before override");
debug writeln("@BeforeEach override");
}
}

Expand Down Expand Up @@ -164,16 +164,16 @@ class TestingThisAndThat
assert(false);
}

// expected exception can be further verified
@Test
public void testException()
{
import std.exception : enforce;
// expected exception can be further verified
@Test
public void testException()
{
import std.exception : enforce;

auto exception = expectThrows(enforce(false));
auto exception = expectThrows(enforce(false));

assertEquals("Enforcement failed", exception.msg);
}
assertEquals("Enforcement failed", exception.msg);
}
}

/**
Expand Down
78 changes: 43 additions & 35 deletions src/dunit/framework.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ struct TestClass
Disabled[string] disabled;

Object function() create;
void function(Object o) beforeAll;
void function() beforeAll;
void function(Object o) beforeEach;
void delegate(Object o, string test) test;
void function(Object o) afterEach;
void function(Object o) afterAll;
void function() afterAll;
}

TestClass[] testClasses;
Expand Down Expand Up @@ -190,50 +190,50 @@ body
foreach (testListener; testListeners)
testListener.enterClass(testClass.name);

// TODO testObject per test; static BeforeAll and AfterAll
Object testObject = null;
bool classSetUp = true; // not yet failed
bool initialized = false;
bool setUp = false;

// run each @Test of the class
foreach (test; tests)
{
bool success = true;
bool ignore = cast(bool)(test in testClass.disabled);
bool success = false;

foreach (testListener; testListeners)
testListener.enterTest(test);
scope (exit)
foreach (testListener; testListeners)
testListener.exitTest(success);

// create test object on demand
if (!ignore && testObject is null)
{
if (classSetUp)
{
classSetUp = tryRun("this",
{ testObject = testClass.create(); });
}
if (classSetUp)
{
classSetUp = tryRun("@BeforeAll",
{ testClass.beforeAll(testObject); });
}
}

if (ignore || !classSetUp)
if (test in testClass.disabled || (initialized && !setUp))
{
string reason = testClass.disabled.get(test, Disabled.init).reason;

foreach (testListener; testListeners)
testListener.skip(reason);
success = false;
continue;
}

success = tryRun("@BeforeEach",
{ testClass.beforeEach(testObject); });
// use lazy initialization to run @BeforeAll
// (failure or error can only be reported for a given test)
if (!initialized)
{
setUp = tryRun("@BeforeAll",
{ testClass.beforeAll(); });
initialized = true;
}

Object testObject = null;

if (setUp)
{
success = tryRun("this",
{ testObject = testClass.create(); });
}
if (success)
{
success = tryRun("@BeforeEach",
{ testClass.beforeEach(testObject); });
}
if (success)
{
success = tryRun("@Test",
Expand All @@ -244,11 +244,10 @@ body
&& success;
}
}

if (testObject !is null && classSetUp)
if (setUp)
{
tryRun("@AfterAll",
{ testClass.afterAll(testObject); });
{ testClass.afterAll(); });
}
}

Expand Down Expand Up @@ -672,10 +671,10 @@ private TestClass[] unitTestFunctions()

testClass.tests = ["unittest"];
testClass.create = () => null;
testClass.beforeAll = (o) {};
testClass.beforeAll = () {};
testClass.beforeEach = (o) {};
testClass.afterEach = (o) {};
testClass.afterAll = (o) {};
testClass.afterAll = () {};

foreach (moduleInfo; ModuleInfo)
{
Expand Down Expand Up @@ -712,9 +711,9 @@ mixin template UnitTest()
mixin("return new " ~ typeof(this).stringof ~ "();");
}

static void beforeAll(Object o)
static void beforeAll()
{
mixin(_sequence(_members!(typeof(this), BeforeAll)));
mixin(_static(_members!(typeof(this), BeforeAll)));
}

static void beforeEach(Object o)
Expand All @@ -732,9 +731,9 @@ mixin template UnitTest()
mixin(_sequence(_members!(typeof(this), AfterEach)));
}

static void afterAll(Object o)
static void afterAll()
{
mixin(_sequence(_members!(typeof(this), AfterAll)));
mixin(_static(_members!(typeof(this), AfterAll)));
}

testClass.create = &create;
Expand All @@ -758,6 +757,15 @@ mixin template UnitTest()
return block;
}

private static string _static(in string[] memberFunctions)
{
string block = null;

foreach (memberFunction; memberFunctions)
block ~= memberFunction ~ "();\n";
return block;
}

private static string _sequence(in string[] memberFunctions)
{
string block = "auto testObject = cast(" ~ typeof(this).stringof ~ ") o;\n";
Expand Down

0 comments on commit 797c6ca

Please sign in to comment.