Skip to content
This repository
Browse code

add Tester for automated tests

  • Loading branch information...
commit 923b6c5556e2270d2c7595da9849bfba4131be6c 1 parent 0ce7718
Romans Malinovskis romaninsh authored

Showing 1 changed file with 95 additions and 0 deletions. Show diff stats Hide diff stats

  1. +95 0 lib/Page/Tester.php
95 lib/Page/Tester.php
... ... @@ -0,0 +1,95 @@
  1 +<?php
  2 +class Page_Tester extends Page {
  3 + public $variances=array();
  4 + function setVariance($arr){
  5 + $this->variances=$arr;
  6 + foreach($arr as $key=>$item){
  7 + if(is_numeric($key))$key=$item;
  8 + $this->grid->addColumn('html',$key.'_inf',$key.' info');
  9 + $this->grid->addColumn('text,wrap',$key.'_res',$key.' result');
  10 + }
  11 + }
  12 + function init(){
  13 + parent::init();
  14 + $this->grid=$this->add('Grid');
  15 + $this->grid->addColumn('text','name');
  16 +
  17 + //$this->setVariance(array('GiTemplate','SMlite'));
  18 + $this->setVariance(array('Test'));
  19 +
  20 + $this->runTests();
  21 + }
  22 + function runTests($test_obj=null){
  23 +
  24 + if(!$test_obj)$test_obj=$this;
  25 +
  26 + $tested=array();
  27 + $data=array();
  28 + foreach(get_class_methods($test_obj) as $method){
  29 + $m='';
  30 + if(substr($method,0,5)=='test_'){
  31 + $m=substr($method,5);
  32 + }elseif(substr($method,0,8)=='prepare_'){
  33 + $m=substr($method,8);
  34 + }else continue;
  35 +
  36 + // Do not retest same function even if it has both prepare and test
  37 + if($tested[$m])continue;$tested[$m]=true;
  38 +
  39 + // Row contains test result data
  40 + $row=array('name'=>$m);
  41 +
  42 + foreach($this->variances as $key=>$vari){
  43 + if(is_numeric($key))$key=$vari;
  44 +
  45 + // Input is a result of preparation function
  46 + if(method_exists($test_obj,'prepare_'.$m)){
  47 + $input=$test_obj->{'prepare_'.$m}($vari,$method);
  48 + }else{
  49 + $input=$test_obj->prepare($vari,$method);
  50 + }
  51 +
  52 + $test_func=method_exists($test_obj,'test_'.$m)?
  53 + 'test_'.$m:'test';
  54 +
  55 + // Test speed
  56 + $me=memory_get_peak_usage();
  57 + $ms=microtime(true);
  58 + /*
  59 + $limit=20;$hl=round($limit /2);
  60 + for($i=0;$i<$limit;$i++){
  61 + //$result=call_user_func_array(array($test_obj,$test_func),$input);
  62 + */
  63 + $result=(string)$test_obj->$test_func($input[0],$input[1]);
  64 + //$this->$method($vari);
  65 + /*
  66 + if($i==$hl){
  67 + $meh=memory_get_peak_usage();
  68 + }
  69 + */
  70 + //}
  71 + $ms=microtime(true)-$ms;
  72 + $me=($mend=memory_get_peak_usage())-$me;
  73 + $x=$row[$key.'_inf']='Speed: '.round($ms,3).'<br/>Memory: '.$me;
  74 +
  75 + $x=$row[$key.'_res']=$result;
  76 + }
  77 +
  78 + $data[]=$row;
  79 + }
  80 + $this->grid->setStaticSource($data);
  81 + }
  82 + function expect($value,$expectation){
  83 + return $value==$expectation?'OK':'ERR';
  84 + }
  85 +
  86 + function _prepare($t,$str){
  87 + $result='';
  88 +
  89 + for($i=0;$i<100;$i++){
  90 + $result.=$str;
  91 + }
  92 + return array($this->add($t),$result);
  93 + }
  94 +
  95 +}

0 comments on commit 923b6c5

Please sign in to comment.
Something went wrong with that request. Please try again.