root > WikiSense-trunk > common > WSTest.php

WSTest.php

application/x-php, 4277 bytes (load raw)
<?php
if (!defined("WS_CONSOLE")) die("bad entry point!");

define('WS_TEST_DONTCARE','__@@_DONTCARE_8923489745_@@__');

class WSTest {
        function WSTest( ) {
        }
       
        function tests() {
            $failed= 0;
            $passed= 0;
           
            $cls= get_class($this);
            $m= get_class_methods( $cls );
            foreach ($m as $mth) {
                if (preg_match('/TEST$/',$mth)) {
                    if ($mth==$cls) continue;
                    if (preg_match('/^_/',$mth)) continue;
                   
                    $ok= $this->runTest($mth);
                   
                    if ($ok) $passed+= 1;
                    else $failed+= 1;
                }
            }
           
            print "= $failed failed, $passed passed.\n";
            return $failed;
        }
       
        function runTest( $name ) {
            $ok= eval('return $this->'.$name.'();');
           
            print "* TEST: " . get_class($this) . ': ' . $name . ': ' . ($ok ? 'OK' : 'FAILED') . "\n";
            return $ok;
        }
       
        function setup() {
        }
       
        function teardown() {
        }

        function internalError( $msg ) {
            print "ERROR IN TEST: $msg\n";
        }
       
        function run() {
            $this->setup();
            $failed= $this->tests();
            $this->teardown();
           
            return $failed;
        }
       
        function loadAssertions($file) {
            #$f= dirname(__FILE__).'/'.$file;
            $f= $file;
            preg_match('!\.([^/.]+)$!',$f,$m);
            $ext= $m[1];
           
            if (!file_exists($file)) {
                $this->internalError("Assertion file not found: $f");
                return false;
            }
           
            $f= fopen($file, 'r');
           
            if (!$f) {
                $this->internalError("failed to open assertion file $file");
                return false;
            }
           
            $line= 0;
            while (1) {
                $s=fgets($f);
                $line+= 1;
               
                if ($s===false || $s===NULL || $s==='') break;
               
                $s= trim($s);
                if ($s==='') continue;
                if ($s[0]==='#' || $s[0]===';') continue;
               
                $args= explode("\t", $s);
               
                if (sizeof($args)<2) {
                    $this->internalError("$file [$line]: too few entries in assertion line (exopected at least two)\n$s");
                    fclose($f);
                    return false;
                }
               
                $cmd= array_shift($args);
                $args= $this->decodeFields($args);
               
                $ok= call_user_func_array(array($this, $cmd), $args);
            }
        }
 
        function assertEqual($val, $exp, $msg) {
            if ($val==$exp)  return true;
           
            $this->assertionError("( found $val, expected $exp )", $msg);
            return false;
        }       
       
        function assertionError($comp, $msg=NULL) {
            print "  - ASSERTION FAILED $comp";
            if ($msg) print ": $msg";
            print "\n";
        }       
       
        function decodeValue( $v ) {
            if ($v===NULL) return NULL;
            $low= strtolower(trim($v));
           
            if ($low==='') return NULL;
            else if ($low==='null') return NULL;
            else if ($low==='true') return true;
            else if ($low==='false') return false;
            else if ($low==='?' || $low==='any' || $low==='%') return WS_TEST_DONTCARE;
            else if (preg_match('/\d+/',$low)) return (int)$v;
            else if (preg_match('/\d+\.\d+/',$low)) return (float)$v;
            else if (preg_match('/^\s+"(.*)"\s+$/',$v,$m)) return $m[1];
            else if (preg_match('/^\s+\'(.*)\'\s+$/',$v,$m)) return $m[1];
            else return $v;
        }
       
        function & decodeFields( &$data ) {
            foreach ($data as $k => $v) {
                $data[$k]= $this->decodeValue($v);
            }
           
            return $data;
        }
       
}
?>