Source of demoCheck.php
<?php
/* simple demonstration of check functions */
require_once('./check.php'); // always needed
print('<h1>Simple check demo</h1>'); // Good to know what we're doing!
/* set up a class and function to work with */
class foo{
var $localData = null;
function __construct($Data=null){
$this->localData=$Data;
}
// This function multiplies by two... ...or does it?
function TimesTwo($ShouldBeNumber){
return $ShouldBeNumber * 2;
}
// This function demonstrates deliberate exceptions
function FailOnNegative($Arg){
if($Arg<0){
throw new Exception('Negative not allowed');
}
return 1234;
}
}
function DoubleLetters($ArgString){
// Return the first character of the string duplicated. eg "Peter"-->"PP"
// This function THROWS AN ERROR for empty string
// And DOESN'T WORK PROPERLY if the 1st character is > 'Z'
if($ArgString==''){
throw new Exception('Must have at least one letter');
}
if($ArgString[0]>'Z'){
return 'Silly!';
}else{
return $ArgString[0] . $ArgString[0];
}
}
/******************* Testing foo->TimesTwo *******************/ // Notice how tests are in blocks
check::StartBlock('foo TimesTwo'); // block start and heading
check::PassShort(); // label and OK for passes
//check::ShowOkBlockDetails(false); // [1] Uncomment and see what happens. Suppresses 100% success details
$fooObj = new foo();
check::Is('Ftt.0','foo',$fooObj); // hope so!
check::Test('Ftt.1',4,$fooObj->TimesTwo(2)); // 2 x 2 = 4
check::Test('Silly',5,$fooObj->TimesTwo(2)); // 2 x 2 <> 5 <== To illustrate a fail
check::Test('Ftt.2',4,$fooObj->TimesTwo(2.00000)); // 2 x 2 = 4
check::Test('Ftt.3',4.00002,$fooObj->TimesTwo(2.00001));// 2 x 2 = 4 (ish)
check::Test('Ftt.4',4,$fooObj->TimesTwo(' 2 ')); // 2 x 2 = 4 String conversion
// The next test detetects the unusual situation where we DON'T want a match
// PHP coerces TRUE to 1 which is very naughty if we're not expecting it.
// It fails which is what we want it to do so we can pay more attention
// to argument checking by rewriting the method.
// (Or we may consider it such a bizarre occurence that we remove the test.)
check::TestNot('Ftt.5',2,$fooObj->TimesTwo(true)); // True x 2 = 2 !! [A]
/* exception checking */
check::FailLong(); // increase verbosity of error
check::Trap('Ftt.10','','TimesTwo',$fooObj,null); // should be an error
// The next line causes a fatal error. Things like this are best strangled at birth
// check::Trap('Ftt.11','oper','TimesTwo',$fooObj,array(array(1,2,3))); //[3]
check::EndBlock();
/******************* Testing foo->FailOnNegative *******************/
check::StartBlock('foo FailOnNegative'); // block start and heading
check::Trap('Ffon.1','','FailOnNegative',$fooObj,0); // no error
check::Trap('Ffon.2','','FailOnNegative',$fooObj,1); // no error
check::Trap('Ffon.3','Neg','FailOnNegative',$fooObj,-1); // ERROR!
check::EndBlock(true);
/******************* Testing DoubleLetters() *******************/
check::StartBlock('function DoubleLetters'); // block start and heading
check::Trap('funDL.1','Must','DoubleLetters',null,''); // see if error trap works
// illustrate lots of passes and a fail
check::PassDot(); // one dot per pass
check::Label('funDL.2 ');
for($i=64;$i<200;$i++){
$c = chr($i);
if(! check::Test('',$c.$c,DoubleLetters($c))){break;} //<-- Should fail after 26 iterations
}
check::EndBlock();
/******************* Object dumping *******************/
$a = array();
$a[] = new foo('Now is the winter');
$a[] = new foo(array(false,true,5,7,null,13,array(1,7),'Twenty three'));
$bigObj = new foo($a);
check::StartBlock('object dumping');
check::Show('bigObj',$bigObj);
check::EndBlock();
$output = '';
// $output = '#n#d#t#f.htm' [2] This will write results to a file
check::Finish($output); // writes buffer if buffering. Shows grand totals
check::Help(); // quick reference
?>