<commit_before><?php // vim:ts=4:sw=4:et:fdm=marker
/*
 * Undocumented
 *
 * @link http://agiletoolkit.org/
*//*
==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2012 <NAME> <<EMAIL>>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
 =====================================================ATK4=*/
/** Tester page is a basic implementation of a testing environment for Agile Toolkit.
    See Documentation for testing */
class Page_Tester extends Page {
    public $variances=array();
    public $input;
    public $responses=array();
    public $auto_test=true;

    /** Redefine this to the value generated by a test */
    public $proper_responses=null;

    function setVariance($arr){
        $this->variances=$arr;
        foreach($arr as $key=>$item){
            if(is_numeric($key))$key=$item;
            $this->grid->addColumn('html',$key.'_inf',$key.' info');
            $this->grid->addColumn('html,wrap',$key.'_res',$key.' result');
        }
    }
    function init(){
        parent::init();
        $this->grid=$this->add('Grid');
        $this->grid->addColumn('template','name')->setTemplate('<a href="'.$this->api->url(null,array('testonly'=>'')).'<?$name?>"><?$name?></a>');

        //$this->setVariance(array('GiTemplate','SMlite'));
        $this->setVariance(array('Test'));

        if(!$this->auto_test)return;    // used for multi-page testing
        
        $this->runTests();

        if(!$_GET['testonly']){
            $f=$this->add('Form');
            $ff=$f->addField('text','responses');
            $this->responses=
    '    public $proper_responses=array(
            '.join(',
            ',$this->responses).'
        );';
            $ff->set($this->responses);
            $ff->js('click')->select();
        }
    }
    public $cnt;
    function ticker(){
        $this->cnt++;
    }
    function silentTest($test_obj=null){
        if(!$test_obj)$test_obj=$this;

        $total=$success=$fail=$exceptions=0;
        $speed=$memory=0;

        $tested=array();
        foreach(get_class_methods($test_obj) as $method){
            if(strpos($method,'test_')===0){
                $m=substr($method,5);
            }elseif(strpos($method,'prepare_')===0){
                $m=substr($method,8);
            }else continue;
            if($tested[$m])continue;$tested[$m]=true;

            foreach($this->variances as $key=>$vari){
                if(is_numeric($key))$key=$vari;

                // Input is a result of preparation function
                if(method_exists($test_obj,'prepare_'.$m)){
                    $input=$test_obj->{'prepare_'.$m}($vari,$method);
                }else{
                    $input=$test_obj->prepare($vari,$method);
                }

                $this->input=$input;

                $test_func=method_exists($test_obj,'test_'.$m)?
                    'test_'.$m:'test';

                $total++;

                $me=memory_get_peak_usage();
                $ms=microtime(true);

                $this->cnt=0;
                declare(ticks=1);
                register_tick_function(array($this,'ticker'));

                try{
                    $result=(string)$test_obj->$test_func($input[0],$input[1],$input[2]);
                    $ms=microtime(true)-$ms;
                    $me=($mend=memory_get_peak_usage())-$me;

                    $result=$this->formatResult($row,$key,$result);

                    $k=$key.'_'.$m;
                    if($this->proper_responses[$k]==$result && isset($this->proper_responses[$k])){
                        $success++;
                    }else{
                        $fail++;
                    }
                }catch (Exception $e){
                    $exceptions++;

                    $ms=microtime(true)-$ms;
                    $me=($mend=memory_get_peak_usage())-$me;
                }

                unregister_tick_function(array($this,'ticker'));

                $speed+=$this->cnt*1;
                $memory+=$me;
            }
        }
        return array(
            'total'=>$total,
            'success'=>$success,
            'exceptions'=>$exceptions,
            'fail'=>$fail,
            'speed'=>$speed,
            'memory'=>$memory
            );
    }
    function runTests($test_obj=null){

        if(!$test_obj)$test_obj=$this;

        $tested=array();
        $data=array();
        foreach(get_class_methods($test_obj) as $method){
            $m='';
            if(strpos($method,'test_')===0){
                $m=substr($method,5);
            }elseif(strpos($method,'prepare_')===0){
                $m=substr($method,8);
            }else continue;

            if(isset($_GET['testonly']) && 'test_'.$_GET['testonly']!=$method)continue;

            // Do not retest same function even if it has both prepare and test
            if($tested[$m])continue;$tested[$m]=true;

            // Row contains test result data
            $row=array('name'=>$m,'id'=>$m);

            foreach($this->variances as $key=>$vari){
                if(is_numeric($key))$key=$vari;

                // Input is a result of preparation function
                if(method_exists($test_obj,'prepare_'.$m)){
                    $input=$test_obj->{'prepare_'.$m}($vari,$method);
                }else{
                    $input=$test_obj->prepare($vari,$method);
                }

                $this->input=$input;

                $test_func=method_exists($test_obj,'test_'.$m)?
                    'test_'.$m:'test';

                // Test speed
                $me=memory_get_peak_usage();
                $ms=microtime(true);
                $this->cnt=0;
                declare(ticks=1);
                register_tick_function(array($this,'ticker'));
                try{
                    $result=(string)$test_obj->$test_func($input[0],$input[1],$input[2]);
                }catch (Exception $e){

                    if($_GET['tester_details']==$row['name'] && $_GET['vari']==$vari){
                        throw $e;
                    }


                    $result='Exception: '.(method_exists($e,'getText')?
                        $e->getText():
                        $e->getMessage());

                    $ll=$this->add('P');
                    $v=$ll->add('View')
                        ->setElement('a')
                        ->setAttr('href','#')
                        ->set('More details')
                        ->js('click')->univ()->frameURL('Exception Details for test '.$row['name'],
                        $this->api->url(null,array('tester_details'=>$row['name'],'vari'=>$vari)))
                        ;

                    $result.=$ll->getHTML();
                }
                $ms=microtime(true)-$ms;
                $me=($mend=memory_get_peak_usage())-$me;
                unregister_tick_function(array($this,'ticker'));<commit_msg>fix popups which used to appear simultaniously<commit_after><?php // vim:ts=4:sw=4:et:fdm=marker
/*
 * Undocumented
 *
 * @link http://agiletoolkit.org/
*//*
==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2012 <NAME> <<EMAIL>>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
 =====================================================ATK4=*/
/** Tester page is a basic implementation of a testing environment for Agile Toolkit.
    See Documentation for testing */
class Page_Tester extends Page {
    public $variances=array();
    public $input;
    public $responses=array();
    public $auto_test=true;

    /** Redefine this to the value generated by a test */
    public $proper_responses=null;

    function setVariance($arr){
        $this->variances=$arr;
        foreach($arr as $key=>$item){
            if(is_numeric($key))$key=$item;
            $this->grid->addColumn('html',$key.'_inf',$key.' info');
            $this->grid->addColumn('html,wrap',$key.'_res',$key.' result');
        }
    }
    function init(){
        parent::init();
        $this->grid=$this->add('Grid');
        $this->grid->addColumn('template','name')->setTemplate('<a href="'.$this->api->url(null,array('testonly'=>'')).'<?$name?>"><?$name?></a>');

        //$this->setVariance(array('GiTemplate','SMlite'));
        $this->setVariance(array('Test'));

        if(!$this->auto_test)return;    // used for multi-page testing
        
        $this->runTests();

        if(!$_GET['testonly']){
            $f=$this->add('Form');
            $ff=$f->addField('text','responses');
            $this->responses=
    '    public $proper_responses=array(
            '.join(',
            ',$this->responses).'
        );';
            $ff->set($this->responses);
            $ff->js('click')->select();
        }
    }
    public $cnt;
    function ticker(){
        $this->cnt++;
    }
    function silentTest($test_obj=null){
        if(!$test_obj)$test_obj=$this;

        $total=$success=$fail=$exceptions=0;
        $speed=$memory=0;

        $tested=array();
        foreach(get_class_methods($test_obj) as $method){
            if(strpos($method,'test_')===0){
                $m=substr($method,5);
            }elseif(strpos($method,'prepare_')===0){
                $m=substr($method,8);
            }else continue;
            if($tested[$m])continue;$tested[$m]=true;

            foreach($this->variances as $key=>$vari){
                if(is_numeric($key))$key=$vari;

                // Input is a result of preparation function
                if(method_exists($test_obj,'prepare_'.$m)){
                    $input=$test_obj->{'prepare_'.$m}($vari,$method);
                }else{
                    $input=$test_obj->prepare($vari,$method);
                }

                $this->input=$input;

                $test_func=method_exists($test_obj,'test_'.$m)?
                    'test_'.$m:'test';

                $total++;

                $me=memory_get_peak_usage();
                $ms=microtime(true);

                $this->cnt=0;
                declare(ticks=1);
                register_tick_function(array($this,'ticker'));

                try{
                    $result=(string)$test_obj->$test_func($input[0],$input[1],$input[2]);
                    $ms=microtime(true)-$ms;
                    $me=($mend=memory_get_peak_usage())-$me;

                    $result=$this->formatResult($row,$key,$result);

                    $k=$key.'_'.$m;
                    if($this->proper_responses[$k]==$result && isset($this->proper_responses[$k])){
                        $success++;
                    }else{
                        $fail++;
                    }
                }catch (Exception $e){
                    $exceptions++;

                    $ms=microtime(true)-$ms;
                    $me=($mend=memory_get_peak_usage())-$me;
                }

                unregister_tick_function(array($this,'ticker'));

                $speed+=$this->cnt*1;
                $memory+=$me;
            }
        }
        return array(
            'total'=>$total,
            'success'=>$success,
            'exceptions'=>$exceptions,
            'fail'=>$fail,
            'speed'=>$speed,
            'memory'=>$memory
            );
    }
    function runTests($test_obj=null){

        if(!$test_obj)$test_obj=$this;

        $tested=array();
        $data=array();
        foreach(get_class_methods($test_obj) as $method){
            $m='';
            if(strpos($method,'test_')===0){
                $m=substr($method,5);
            }elseif(strpos($method,'prepare_')===0){
                $m=substr($method,8);
            }else continue;

            if(isset($_GET['testonly']) && 'test_'.$_GET['testonly']!=$method)continue;

            // Do not retest same function even if it has both prepare and test
            if($tested[$m])continue;$tested[$m]=true;

            // Row contains test result data
            $row=array('name'=>$m,'id'=>$m);

            foreach($this->variances as $key=>$vari){
                if(is_numeric($key))$key=$vari;

                // Input is a result of preparation function
                if(method_exists($test_obj,'prepare_'.$m)){
                    $input=$test_obj->{'prepare_'.$m}($vari,$method);
                }else{
                    $input=$test_obj->prepare($vari,$method);
                }

                $this->input=$input;

                $test_func=method_exists($test_obj,'test_'.$m)?
                    'test_'.$m:'test';

                // Test speed
                $me=memory_get_peak_usage();
                $ms=microtime(true);
                $this->cnt=0;
                declare(ticks=1);
                register_tick_function(array($this,'ticker'));
                try{
                    $result=(string)$test_obj->$test_func($input[0],$input[1],$input[2]);
                }catch (Exception $e){

                    if($_GET['tester_details']==$row['name'] && $_GET['vari']==$vari){
                        throw $e;
                    }


                    $result='Exception: '.(method_exists($e,'getText')?
                        $e->getText():
                        $e->getMessage());

                    $ll=$this->add('P',$row['name']);
                    $v=$ll->add('View')
                        ->setElement('a')
                        ->setAttr('href','#')
                        ->set('More details')
                        ->js('click')->univ()->frameURL('Exception Details for test '.$row['name'],
                        $this->api->url(null,array('tester_details'=>$row['name'],'vari'=>$vari)))
                        ;

                    $result.=$ll->getHTML();
                }
                $ms=microtime(true)-$ms;
                $me=($mend=memory_get_peak_usage())-$me;
                unregister_tick_function(array($this,'ticker'));
{'fraction_non_alphanumeric': 0.18789854045628454, 'fraction_numerical': 0.0049596145670965, 'mean_word_length': 1.7201772981306611, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5481288', 'n_tokens_mistral': 4165, 'n_tokens_neox': 3910, 'n_words': 557}