Browse files

Adding [beforeAll] and [beforeEach] hooks

  • Loading branch information...
1 parent 17e741a commit a742fd24bd00252d8f27ad6626b2a22cb43ac6c2 @bfad committed Aug 29, 2011
Showing with 301 additions and 1 deletion.
  1. +39 −1 lspec.inc
  2. +262 −0 tests/lspec_testGroup.test.inc
View
40 lspec.inc
@@ -44,6 +44,12 @@ define error_msg_lspecTestGroupHasBeforeEach => "You have already specified a [
define error_code_lspecTestGroupHasAfterEach => 106
define error_msg_lspecTestGroupHasAfterEach => "You have already specified an [afterEach] block"
+define error_code_lspecTestGroupHasBeforeAll => 107
+define error_msg_lspecTestGroupHasBeforeAll => "You have already specified a [beforeAll] block"
+
+define error_code_lspecTestGroupHasAfterAll => 108
+define error_msg_lspecTestGroupHasAfterAll => "You have already specified a [afterAll] block"
+
define lspec_testSuite => type {
// Tallying up the test suite
@@ -146,11 +152,19 @@ define lspec_testSuite => type {
fail_if(not .inTest, error_code_lspecNotInTest, error_msg_lspecNotInTest)
.numTestCases++
}
+ public addBeforeAll(block::capture) => {
+ fail_if(not .inTestGroup, error_code_lspecNotInTestGroup, error_msg_lspecNotInTestGroup)
+ .getCurrentTestGroup->beforeAll(#block)
+ }
public addBeforeEach(block::capture) => {
fail_if(not .inTestGroup, error_code_lspecNotInTestGroup, error_msg_lspecNotInTestGroup)
.getCurrentTestGroup->beforeEach(#block)
}
+ public addAfterAll(block::capture) => {
+ fail_if(not .inTestGroup, error_code_lspecNotInTestGroup, error_msg_lspecNotInTestGroup)
+ .getCurrentTestGroup->afterAll(#block)
+ }
public addAfterEach(block::capture) => {
fail_if(not .inTestGroup, error_code_lspecNotInTestGroup, error_msg_lspecNotInTestGroup)
@@ -308,7 +322,9 @@ define lspec_testGroup => type {
private suite::lspec_testSuite,
private tests = array,
private groups = array,
+ private beforeAll::capture,
private beforeEach::capture,
+ private afterAll::capture,
private afterEach::capture,
private parentGroup = null,
private currentTest = 0,
@@ -331,13 +347,28 @@ define lspec_testGroup => type {
public numTests => .tests->size
+ public beforeAll(block::capture) => {
+ fail_if(.beforeAll,
+ error_code_lspecTestGroupHasBeforeAll, error_msg_lspecTestGroupHasBeforeAll
+ )
+ .beforeAll = #block
+ #block->invoke
+ }
+
public beforeEach(block::capture) => {
fail_if(.beforeEach,
error_code_lspecTestGroupHasBeforeEach, error_msg_lspecTestGroupHasBeforeEach
)
.beforeEach = #block
}
+ public afterAll(block::capture) => {
+ fail_if(.afterAll,
+ error_code_lspecTestGroupHasAfterAll, error_msg_lspecTestGroupHasAfterAll
+ )
+ .afterAll = #block
+ }
+
public afterEach(block::capture) => {
fail_if(.afterEach,
error_code_lspecTestGroupHasAfterEach, error_msg_lspecTestGroupHasAfterEach
@@ -353,7 +384,8 @@ define lspec_testGroup => type {
public run => {
local(start) = micros
.parentSuite->enterTestGroup
- handle => {
+ handle => {
+ .afterAll->invoke
.parentSuite->leaveTestGroup
not .parentGroup? .parentSuite->runTime(micros - #start)
}
@@ -476,9 +508,15 @@ define it(label) => {
/*
* Before and after hooks
*/
+define beforeAll => {
+ lspec->addBeforeAll(givenBlock)
+}
define beforeEach => {
lspec->addBeforeEach(givenBlock)
}
+define afterAll => {
+ lspec->addAfterAll(givenBlock)
+}
define afterEach => {
lspec->addAfterEach(givenBlock)
}
View
262 tests/lspec_testGroup.test.inc
@@ -338,4 +338,266 @@ if(#expected != #result) => {
stdoutnl('Test Failed\n' + #test_desc)
stdoutnl(currentCapture->callStack->split('\n')->first)
abort
+}
+
+
+
+#test_desc = "It should throw an error if a [beforeAll] method isn't inside a [describe] method"
+lspec_setupForTesting
+try => {
+ local(success) = false
+ handle(error_code == error_code_lspecNotInTestGroup &&
+ error_msg == error_msg_lspecNotInTestGroup) =>
+ {
+ failure_clear
+ error_reset
+ #success = true
+ }
+
+ beforeAll => {}
+ if(not #success) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+ }
+}
+
+#success = false
+describe("Something") => {
+ beforeAll => {}
+ #success = true
+}
+if(not #success) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "It should throw an error if try to add a second [beforeAll] method"
+lspec_setupForTesting
+local(success) = false
+describe("Something") => {
+ beforeAll => {}
+ try => {
+ handle(error_code == error_code_lspecTestGroupHasBeforeAll &&
+ error_msg == error_msg_lspecTestGroupHasBeforeAll) =>
+ {
+ failure_clear
+ error_reset
+ #success = true
+ }
+ beforeAll => {}
+ }
+}
+if(not #success) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "The [beforeAll] code should be called before a test group"
+lspec_setupForTesting
+local(num_calls) = 0
+local(expected) = null
+describe("Something") => {
+ beforeAll => {
+ #num_calls++
+ }
+ it("does one thing") => {
+ #expected = #num_calls
+ }
+}
+if(1 != #expected) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+if(1 != #num_calls) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "The [beforeAll] code should be called once before all the tests in a test group"
+lspec_setupForTesting
+local(num_calls) = 0
+describe("Something") => {
+ beforeAll => {
+ #num_calls++
+ }
+ it("does one thing") => {}
+ it("does two thing") => {}
+ it("does six thing") => {}
+}
+if(1 != #num_calls) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "The [beforeAll] is only called once before any nested test groups or tests"
+lspec_setupForTesting
+local(result) = array
+describe("Something") => {
+ beforeAll => {
+ #result->insert(1)
+ }
+ it("does something") => {}
+ describe("as something") => {
+ beforeAll => {
+ #result->insert(2)
+ }
+ it("does something") => {}
+ describe("in this context") => {
+ beforeAll => {
+ #result->insert(3)
+ }
+ it("does something") => {}
+ }
+ }
+}
+local(expected) = array(1,2,3)
+if(#expected != #result) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "It should throw an error if an [afterAll] method isn't inside a [describe] method"
+lspec_setupForTesting
+try => {
+ local(success) = false
+ handle(error_code == error_code_lspecNotInTestGroup &&
+ error_msg == error_msg_lspecNotInTestGroup) =>
+ {
+ failure_clear
+ error_reset
+ #success = true
+ }
+
+ afterAll => {}
+ if(not #success) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+ }
+}
+
+#success = false
+describe("Something") => {
+ afterAll => {}
+ #success = true
+}
+if(not #success) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "It should throw an error if try to add a second [afterAll] method"
+lspec_setupForTesting
+local(success) = false
+describe("Something") => {
+ afterAll => {}
+ try => {
+ handle(error_code == error_code_lspecTestGroupHasAfterAll &&
+ error_msg == error_msg_lspecTestGroupHasAfterAll) =>
+ {
+ failure_clear
+ error_reset
+ #success = true
+ }
+ afterAll => {}
+ }
+}
+if(not #success) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "The [afterAll] code should be called after a test group"
+lspec_setupForTesting
+local(num_calls) = 0
+local(expected) = null
+describe("Something") => {
+ afterAll => {
+ #num_calls++
+ }
+ it("does one thing") => {
+ #expected = #num_calls
+ }
+}
+if(0 != #expected) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+if(1 != #num_calls) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "The [afterAll] code should be called after all tests in a test group"
+lspec_setupForTesting
+local(num_calls) = 0
+describe("Something") => {
+ afterAll => {
+ #num_calls++
+ }
+ it("does one thing") => {}
+ it("does two thing") => {}
+ it("does six thing") => {}
+}
+if(1 != #num_calls) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
+}
+
+
+
+#test_desc = "Each [afterAll] in nested test groups in inside-out order"
+lspec_setupForTesting
+local(result) = array
+describe("Something") => {
+ afterAll => {
+ #result->insert(1)
+ }
+ it("does something") => {}
+ describe("as something") => {
+ afterAll => {
+ #result->insert(2)
+ }
+ it("does something") => {}
+ describe("in this context") => {
+ afterAll => {
+ #result->insert(3)
+ }
+ it("does something") => {}
+ }
+ }
+}
+local(expected) = array(3,2,1)
+if(#expected != #result) => {
+ stdoutnl('Test Failed\n' + #test_desc)
+ stdoutnl(currentCapture->callStack->split('\n')->first)
+ abort
}

0 comments on commit a742fd2

Please sign in to comment.