Skip to content

Commit 443da8c

Browse files
authored
Merge pull request #31 from bfrymire/event-firing-around-run
Add event functions for run and make name an argument for BaseTestClass
2 parents b2e0c69 + 3b4d0b7 commit 443da8c

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed
Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
/**
2-
* Base "class" test constructors will inherit from
2+
* Base "class" that test constructors will inherit from
33
* @constructor BaseTestClass
4+
* @param {string} name - Name of class
45
*/
5-
function BaseTestClass() {
6+
function BaseTestClass(_name) {
67

78
name = undefined;
89
static setUp = undefined;
910
static __setUp__ = undefined;
1011
static tearDown = undefined;
1112
static __tearDown__ = undefined;
13+
static __onRunBegin__ = undefined
14+
static __onRunEnd__ = undefined
15+
setName(_name);
1216

1317
// Give self cripsyStructUnpack() function
1418
crispyMixinStructUnpack(self);
1519

20+
1621
/**
17-
* Set the name of the TestCase
22+
* Set name of class object
1823
* @function setName
19-
* @param {string} name - Name of the test
24+
* @param {string} name - Name of the object
2025
*/
2126
static setName = function(_name) {
2227
if !is_string(_name) {
@@ -25,4 +30,44 @@ function BaseTestClass() {
2530
name = _name;
2631
}
2732

33+
/**
34+
* Event to be called at the beginning of run
35+
* @function onRunBegin
36+
* @param [method] func - Method to override __onRunBegin__ with
37+
*/
38+
static onRunBegin = function() {
39+
if argument_count > 0 {
40+
var _func = argument[0];
41+
if is_method(_func) {
42+
__onRunBegin__ = method(self, _func);
43+
} else {
44+
crispyThrowExpected(self, "onRunBegin", "method", typeof(_func));
45+
}
46+
} else {
47+
if is_method(__onRunBegin__) {
48+
__onRunBegin__();
49+
}
50+
}
51+
}
52+
53+
/**
54+
* Event to be called at the end of run
55+
* @function onRunEnd
56+
* @param [method] func - Method to override __onRunEnd__ with
57+
*/
58+
static onRunEnd = function() {
59+
if argument_count > 0 {
60+
var _func = argument[0];
61+
if is_method(_func) {
62+
__onRunEnd__ = method(self, _func);
63+
} else {
64+
crispyThrowExpected(self, "onRunEnd", "method", typeof(_func));
65+
}
66+
} else {
67+
if is_method(__onRunEnd__) {
68+
__onRunEnd__();
69+
}
70+
}
71+
}
72+
2873
}

scripts/TestCase/TestCase.gml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
* @param {method} func - Test assertion to run for TestCase
66
* @param [struct] unpack - Struct for crispyStructUnpack
77
*/
8-
function TestCase(_name, _func) : BaseTestClass() constructor {
8+
function TestCase(_name, _func) : BaseTestClass(_name) constructor {
99

1010
if !is_method(_func) {
1111
crispyThrowExpected(self, "", "method", typeof(_func));
1212
}
1313

14-
setName(_name);
1514
class = instanceof(self);
1615
parent = undefined;
1716
test = undefined;
@@ -365,7 +364,9 @@ function TestCase(_name, _func) : BaseTestClass() constructor {
365364
*/
366365
static run = function() {
367366
setUp();
367+
onRunBegin();
368368
test();
369+
onRunEnd();
369370
tearDown();
370371
}
371372

@@ -387,6 +388,7 @@ function TestCase(_name, _func) : BaseTestClass() constructor {
387388

388389
/**
389390
* Run struct unpacker if unpack argument was provided
391+
* Stays after all variables are initialized so it may be overwritten
390392
*/
391393
if argument_count > 2 {
392394
crispyStructUnpack(argument[2]);

scripts/TestRunner/TestRunner.gml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* @param {string} name - Name of runner
55
* @param [struct] unpack - Struct for crispyStructUnpack
66
*/
7-
function TestRunner(_name) : BaseTestClass() constructor {
7+
function TestRunner(_name) : BaseTestClass(_name) constructor {
88

9-
setName(_name);
109
start_time = 0;
1110
stop_time = 0;
1211
total_time = 0;
@@ -97,8 +96,10 @@ function TestRunner(_name) : BaseTestClass() constructor {
9796
setUp();
9897
var _len = array_length(suites);
9998
for(var i = 0; i < _len; i++) {
99+
onRunBegin();
100100
suites[i].run();
101101
captureLogs(suites[i]);
102+
onRunEnd();
102103
}
103104
tearDown();
104105
}
@@ -288,6 +289,7 @@ function TestRunner(_name) : BaseTestClass() constructor {
288289

289290
/**
290291
* Run struct unpacker if unpack argument was provided
292+
* Stays after all variables are initialized so it may be overwritten
291293
*/
292294
if argument_count > 1 {
293295
crispyStructUnpack(argument[1]);

scripts/TestSuite/TestSuite.gml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* @param {string} name - Name of suite
55
* @param [struct] unpack - Struct for crispyStructUnpack
66
*/
7-
function TestSuite(_name) : BaseTestClass() constructor {
7+
function TestSuite(_name) : BaseTestClass(_name) constructor {
88

9-
setName(_name);
109
parent = undefined;
1110
tests = [];
1211

@@ -76,13 +75,16 @@ function TestSuite(_name) : BaseTestClass() constructor {
7675
setUp();
7776
var _len = array_length(tests);
7877
for(var i = 0; i < _len; i++) {
78+
onRunBegin();
7979
tests[i].run();
80+
onRunEnd();
8081
}
8182
tearDown();
8283
}
8384

8485
/**
8586
* Run struct unpacker if unpack argument was provided
87+
* Stays after all variables are initialized so it may be overwritten
8688
*/
8789
if argument_count > 1 {
8890
crispyStructUnpack(argument[1]);

0 commit comments

Comments
 (0)