GS2 Testing and Assertions
If you're familiar with other programming languages you should have some knowledge of testing, and assertions.
If you aren't the only scripter on your server, it can be a pain (even worse when you find out too late) to come back and find that someone has broken your code.
How do you get around this? By testing your code. Unfortunately Graal/GS2 has no methods or built-in functionality for assertion-based tests. So I made some, and a nice little example on how to use it.
Testing DB-NPC Example
PHP Code:
function onCreated() {
// Testing Functionality
this.join("testing");
// Multiple Tests
temp.tests = {
this.test_math, this.test_logic
};
test_run(tests, "Small GS2 Test Suite");
// Single Test
test_run(this.test_assertions, "GS2 Assertions Test");
}
/*
Small Example Tests
*/
function test_math() {
// Small Math Test
temp.a = 1;
temp.b = 1;
temp.right = 2;
temp.wrong = 0;
assert_equal((temp.a + temp.b), temp.right, "Good Math Test");
assert_not_equal((temp.a - temp.b), temp.wrong, "Bad Math Test");
}
function test_logic() {
// Small Logic Test
temp.a = 1;
temp.b = 1;
assert_equal(temp.a, temp.b, "Good Logic");
assert_not_equal(temp.a, temp.b, "Bad Logic");
}
function test_assertions() {
// Testing the Assertion Class Functionality
assert(assert(true), "Main Assertion");
assert(assert_equal(1, 1), "Equal Assertion");
assert(assert_not_equal(1, 2), "Not Equal Assertion");
assert(assert_value_in_array(1, {1, 2, 3}), "Value in Array Assertion");
assert(assert_not_in_array(1, {2, 3, 4}), "Not in Array Assertion");
assert(assert_null(NULL), "NULL Assertion");
assert(assert_not_null(1), "Not NULL Assertion");
assert(assert_in_class(this, "testing"), "In Class Assertion");
assert(assert_success(), "Successful Assertion");
assert(!assert_failure("", true), "Failure Assertion");
}
Testing Example Output:
PHP Code:
The script of NPC Testing has been updated by fowlplay4
Running Small GS2 Test Suite
Failure! (T1A2) - Bad Math Test
Failure! (T2A2) - Bad Logic
2 test(s), 2 assertion(s), 2 failure(s)
Running GS2 Assertions Test
Successful! (T1A17)
Failure! (T1A19)
1 test(s), 20 assertion(s), 0 failure(s)
Testing Funcionality (class: testing):
PHP Code:
/*
Testing Functionality
*/
// Test Runner
// Accepts a single function or array of functions
function test_run(test, msg) {
// Echo Test Announcement
echo((msg ? ("Running " @ msg) : "Running test(s)"));
// Initialize Variables
this._assertion = this._assertions = this._errors = this._failures = 0;
this._test = 1;
this._tests = test.size();
// Check for Multiple Tests
if (this._tests > 0) {
// Run Each Test
for (temp.t: test) {
this._assertion = 0;
(@t)();
this._test++;
}
} else {
// Run Single Test
(@test)();
this._tests = 1;
}
// Echo Results
echo(format("%i test(s), %i assertion(s), %i failure(s)", this._tests, this._assertions, this._failures));
// Clean Up Variables
this._assertions = this._test = this._errors = "";
this._assertion = this._failures = this._tests = "";
}
// Primary Assertion Function
// Returns true or false depending on the condition
function assert(cond, msg) {
if (cond) {
// Increment Successful Assertions
this._assertions++;
this._assertion++;
return true;
} else {
// Increment Failures
this._failures++;
_test_failure(msg); // Display Test Error
this._assertion++;
return false;
}
}
/*
Test Assertions
Note: Assertions return the true or false result of
the condition passed to the assertion.
*/
function assert_equal(a, b, msg) {
return assert(a == b, msg);
}
function assert_not_equal(a, b, msg) {
return assert(a != b, msg);
}
function assert_value_in_array(a, b, msg) {
return assert((a in b), msg);
}
function assert_not_in_array(a, b, msg) {
return assert(!(a in b), msg);
}
function assert_null(a, msg) {
return assert(a == NULL, msg);
}
function assert_not_null(a, msg) {
return assert(a != NULL, msg);
}
function assert_in_class(a, b, msg) {
return assert(a.isinclass(b), msg);
}
function assert_success(msg) {
_test_success(msg);
return assert(true, msg);
}
function assert_failure(msg, testing) {
if (testing) {
this._assertions++;
this._failures--;
}
return assert(false, msg);
}
/*
Test Messaging
*/
function _test_success(msg) {
echo("Successful! (T" @ this._test @ "A" @ this._assertion+1 @ ")" @ (msg ? (" - " @ msg) : ""));
}
function _test_failure(msg) {
echo("Failure! (T" @ this._test @ "A" @ this._assertion+1 @ ")" @ (msg ? (" - " @ msg) : ""));
}