Show
Ignore:
Timestamp:
03/10/2008 08:20:33 AM (10 months ago)
Author:
Geert
Message:

Setting svn properties

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/unit_test/libraries/Unit_Test.php

    • Property svn:eol-style set to LF
    • Property copyright set to Copyright (c) 2007 Kohana Team
    • Property svn:keywords set to Id
    r2248 r2265  
    2929                } 
    3030 
    31                 Log::add('debug', 'Unit_Test Library initialized'); 
    32  
    3331                // Recursively iterate over all test paths 
    3432                foreach ($this->paths as $test_path) 
     
    5755                                $reflector = new ReflectionClass($class); 
    5856 
     57                                // Test classes must extend Unit_Test_Case 
     58                                if ( ! $reflector->isSubclassOf(new ReflectionClass('Unit_Test_Case'))) 
     59                                        break; 
     60 
    5961                                // Loop through all the class methods 
    6062                                foreach ($reflector->getMethods() as $method) 
     
    8385                                                $this->results[$class][$method_name] = TRUE; 
    8486                                        } 
    85                                         catch (Exception $e) 
     87                                        catch (Kohana_Unit_Test_Exception $e) 
    8688                                        { 
    8789                                                $this->results[$class][$method_name] = $e; 
     
    104106         * Generates nice test results. 
    105107         * 
    106          * @param   boolean      set to TRUE to echo the output instead of returning it 
    107          * @return  string|void  rendered test results html 
    108          */ 
    109         public function report($print = FALSE) 
    110         { 
    111                 return View::factory('kohana_unit_test')->set('results', $this->results)->render($print); 
     108         * @return  string  rendered test results html 
     109         */ 
     110        public function report() 
     111        { 
     112                return empty($this->results) ? '' : View::factory('kohana_unit_test')->set('results', $this->results)->render(); 
    112113        } 
    113114 
     
    135136        } 
    136137 
    137 } // End Unit_Test 
     138} // End Unit_Test_Core 
     139 
     140 
     141abstract class Unit_Test_Case { 
     142 
     143        public function assert_true($value, $debug = NULL) 
     144        { 
     145                if ($value != TRUE) 
     146                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_true', gettype($value), var_export($value, TRUE)), $debug); 
     147        } 
     148 
     149        public function assert_true_strict($value, $debug = NULL) 
     150        { 
     151                if ($value !== TRUE) 
     152                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_true_strict', gettype($value), var_export($value, TRUE)), $debug); 
     153        } 
     154 
     155        public function assert_false($value, $debug = NULL) 
     156        { 
     157                if ($value != FALSE) 
     158                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_false', gettype($value), var_export($value, TRUE)), $debug); 
     159        } 
     160 
     161        public function assert_false_strict($value, $debug = NULL) 
     162        { 
     163                if ($value !== FALSE) 
     164                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_false_strict', gettype($value), var_export($value, TRUE)), $debug); 
     165        } 
     166 
     167        public function assert_equal($value1, $value2, $debug = NULL) 
     168        { 
     169                if ($value1 != $value2) 
     170                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_equal', gettype($value1), var_export($value1, TRUE), gettype($value2), var_export($value2, TRUE)), $debug); 
     171        } 
     172 
     173        public function assert_not_equal($value1, $value2, $debug = NULL) 
     174        { 
     175                if ($value1 == $value2) 
     176                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_equal', gettype($value1), var_export($value1, TRUE), gettype($value2), var_export($value2, TRUE)), $debug); 
     177        } 
     178 
     179        public function assert_identical($value1, $value2, $debug = NULL) 
     180        { 
     181                if ($value1 !== $value2) 
     182                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_identical', gettype($value1), var_export($value1, TRUE), gettype($value2), var_export($value2, TRUE)), $debug); 
     183        } 
     184 
     185        public function assert_not_identical($value1, $value2, $debug = NULL) 
     186        { 
     187                if ($value1 === $value2) 
     188                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_identical', gettype($value1), var_export($value1, TRUE), gettype($value2), var_export($value2, TRUE)), $debug); 
     189        } 
     190 
     191        public function assert_boolean($value, $debug = NULL) 
     192        { 
     193                if ( ! is_bool($value)) 
     194                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_boolean', gettype($value), var_export($value, TRUE)), $debug); 
     195        } 
     196 
     197        public function assert_not_boolean($value, $debug = NULL) 
     198        { 
     199                if (is_bool($value)) 
     200                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_boolean', gettype($value), var_export($value, TRUE)), $debug); 
     201        } 
     202 
     203        public function assert_integer($value, $debug = NULL) 
     204        { 
     205                if ( ! is_int($value)) 
     206                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_integer', gettype($value), var_export($value, TRUE)), $debug); 
     207        } 
     208 
     209        public function assert_not_integer($value, $debug = NULL) 
     210        { 
     211                if (is_int($value)) 
     212                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_integer', gettype($value), var_export($value, TRUE)), $debug); 
     213        } 
     214 
     215        public function assert_float($value, $debug = NULL) 
     216        { 
     217                if ( ! is_float($value)) 
     218                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_float', gettype($value), var_export($value, TRUE)), $debug); 
     219        } 
     220 
     221        public function assert_not_float($value, $debug = NULL) 
     222        { 
     223                if (is_float($value)) 
     224                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_float', gettype($value), var_export($value, TRUE)), $debug); 
     225        } 
     226 
     227        public function assert_array($value, $debug = NULL) 
     228        { 
     229                if ( ! is_array($value)) 
     230                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_array', gettype($value), var_export($value, TRUE)), $debug); 
     231        } 
     232 
     233        public function assert_not_array($value, $debug = NULL) 
     234        { 
     235                if (is_array($value)) 
     236                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_array', gettype($value), var_export($value, TRUE)), $debug); 
     237        } 
     238 
     239        public function assert_object($value, $debug = NULL) 
     240        { 
     241                if ( ! is_object($value)) 
     242                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_object', gettype($value), var_export($value, TRUE)), $debug); 
     243        } 
     244 
     245        public function assert_not_object($value, $debug = NULL) 
     246        { 
     247                if (is_object($value)) 
     248                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_object', gettype($value), var_export($value, TRUE)), $debug); 
     249        } 
     250 
     251        public function assert_null($value, $debug = NULL) 
     252        { 
     253                if ($value !== NULL) 
     254                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_null', gettype($value), var_export($value, TRUE)), $debug); 
     255        } 
     256 
     257        public function assert_not_null($value, $debug = NULL) 
     258        { 
     259                if ($value === NULL) 
     260                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_null', gettype($value), var_export($value, TRUE)), $debug); 
     261        } 
     262 
     263        public function assert_empty($value, $debug = NULL) 
     264        { 
     265                if ( ! empty($value)) 
     266                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_empty', gettype($value), var_export($value, TRUE)), $debug); 
     267        } 
     268 
     269        public function assert_not_empty($value, $debug = NULL) 
     270        { 
     271                if (empty($value)) 
     272                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_empty', gettype($value), var_export($value, TRUE)), $debug); 
     273        } 
     274 
     275        public function assert_pattern($value, $regex, $debug = NULL) 
     276        { 
     277                if ( ! is_string($value) OR ! is_string($regex) OR ! preg_match($regex, $value)) 
     278                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_pattern', var_export($value, TRUE), var_export($regex, TRUE)), $debug); 
     279        } 
     280 
     281        public function assert_not_pattern($value, $regex, $debug = NULL) 
     282        { 
     283                if ( ! is_string($value) OR ! is_string($regex) OR preg_match($regex, $value)) 
     284                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_pattern', var_export($value, TRUE), var_export($regex, TRUE)), $debug); 
     285        } 
     286 
     287} // End Unit_Test_Case 
     288 
     289 
     290class Kohana_Unit_Test_Exception extends Exception { 
     291 
     292        protected $message = ''; 
     293        protected $debug = NULL; 
     294        protected $file = ''; 
     295        protected $line = ''; 
     296 
     297        /** 
     298         * Set exception message and debug 
     299         * 
     300         * @param   string  message 
     301         * @param   mixed   debug info 
     302         * @return  void 
     303         */ 
     304        public function __construct($message, $debug = NULL) 
     305        { 
     306                // Failure message 
     307                $this->message = (string) $message; 
     308 
     309                // Extra user-defined debug info 
     310                $this->debug = $debug; 
     311 
     312                // Retrieve failure location 
     313                $trace = $this->getTrace(); 
     314                $this->file = $trace[0]['file']; 
     315                $this->line = $trace[0]['line']; 
     316        } 
     317 
     318        /** 
     319         * Magically gets an object property. 
     320         * 
     321         * @param   string  property key 
     322         * @return  mixed   variable value if the key is found 
     323         * @return  void    if the key is not found 
     324         */ 
     325        public function __get($key) 
     326        { 
     327                if (isset($this->$key)) 
     328                        return $this->$key; 
     329        } 
     330 
     331} // End Kohana_Unit_Test_Exception