Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-16-2011, 08:11 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
GS2 Testing / Assertions

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_maththis.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.1;
  
temp.1;
  
temp.right 2;
  
temp.wrong 0;
  
assert_equal((temp.temp.b), temp.right"Good Math Test");
  
assert_not_equal((temp.temp.b), temp.wrong"Bad Math Test");
}

function 
test_logic() {
  
// Small Logic Test
  
temp.1;
  
temp.1;
  
assert_equal(temp.atemp.b"Good Logic");
  
assert_not_equal(temp.atemp.b"Bad Logic");
}

function 
test_assertions() {
  
// Testing the Assertion Class Functionality
  
assert(assert(true), "Main Assertion");
  
assert(assert_equal(11), "Equal Assertion");
  
assert(assert_not_equal(12), "Not Equal Assertion");
  
assert(assert_value_in_array(1, {123}), "Value in Array Assertion");
  
assert(assert_not_in_array(1, {234}), "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(testmsg) {

  
// 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.ttest) {
      
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._teststhis._assertionsthis._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(condmsg) {
  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(abmsg) {
  return 
assert(== bmsg);
}

function 
assert_not_equal(abmsg) {
  return 
assert(!= bmsg);
}

function 
assert_value_in_array(abmsg) {
  return 
assert((a in b), msg);
}

function 
assert_not_in_array(abmsg) {
  return 
assert(!(a in b), msg);
}

function 
assert_null(amsg) {
  return 
assert(== NULLmsg);
}

function 
assert_not_null(amsg) {
  return 
assert(!= NULLmsg);
}

function 
assert_in_class(abmsg) {
  return 
assert(a.isinclass(b), msg);
}

function 
assert_success(msg) {
  
_test_success(msg);
  return 
assert(truemsg);


function 
assert_failure(msgtesting) {
  if (
testing) {
    
this._assertions++;
    
this._failures--;
  }
  return 
assert(falsemsg);
}

/*
   Test Messaging
*/

function _test_success(msg) {
  echo(
"Successful! (T" this._test "A" this._assertion+")" @ (msg ? (" - " msg) : ""));
}

function 
_test_failure(msg) {
  echo(
"Failure! (T" this._test "A" this._assertion+")" @ (msg ? (" - " msg) : ""));

__________________
Quote:
Reply With Quote
  #2  
Old 03-16-2011, 08:16 AM
nullify nullify is offline
Registerd Abuser
nullify's Avatar
Join Date: May 2004
Location: The cheese state.
Posts: 851
nullify has a spectacular aura about
This looks familiar.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:28 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.