Changeset 2247

Show
Ignore:
Timestamp:
03/09/2008 05:59:16 AM (7 months ago)
Author:
Geert
Message:

Unit_Test library update:

  • User Test classes now must extend Unit_Test_Case instead of Unit_Test_Core. This way the Unit_Test_Core $paths and $results properties cannot be overwritten. Updated example test class: http://pastie.textmate.org/163587
  • Moved test results html to separate view
  • Added assert_empty() and assert_pattern()
Location:
trunk/system
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Unit_Test.php

    r2246 r2247  
    2121         * @return  void 
    2222         */ 
    23         public function __construct($paths = NULL) 
    24         { 
    25                 // Use default test path: application/tests/ 
    26                 if ($paths === NULL) 
     23        public function __construct($paths) 
     24        { 
     25                // Normalize all given test paths 
     26                foreach ((array) $paths as $path) 
    2727                { 
    28                         $this->paths = array(APPPATH.'tests/'); 
     28                        $this->paths[] = str_replace('\\', '/', realpath($path)).'/'; 
    2929                } 
    3030 
    31                 // Normalize all given test paths 
    32                 else 
    33                 { 
    34                         foreach ((array) $paths as $path) 
    35                         { 
    36                                 $this->paths[] = str_replace('\\', '/', realpath($path)).'/'; 
    37                         } 
    38                 } 
    39  
    4031                Log::add('debug', 'Unit_Test Library initialized'); 
     32 
     33                // Automatically run tests 
     34                $this->run(); 
    4135        } 
    4236 
     
    8377                                                continue; 
    8478 
    85                                         // Instantiate class 
     79                                        // Instantiate Test class 
    8680                                        $object = new $class; 
     81 
     82                                        // Test classes must extend Unit_Test_Case 
     83                                        if ( ! $object instanceof Unit_Test_Case) 
     84                                                break; 
    8785 
    8886                                        // Run setup method 
     
    108106                                                $object->teardown(); 
    109107                                        } 
     108 
     109                                        // Cleanup 
     110                                        unset($object); 
    110111                                } 
    111112                        } 
    112113                } 
    113114 
     115                // Return raw results array 
    114116                return $this->results; 
    115117        } 
     
    118120         * Generates nice test results. 
    119121         * 
    120          * @param   boolean  set to TRUE to echo the output instead of returning it 
    121          * @return  string   if print is FALSE 
    122          * @return  void     if print is TRUE 
    123          */ 
    124         public function results($print = FALSE) 
    125         { 
    126                 foreach ($this->results as $class => $methods) 
    127                 { 
    128                         echo '<p style="font-weight:bold">', html::specialchars($class), ':</p>'; 
    129                         echo '<ul>'; 
    130  
    131                         foreach ($methods as $method => $result) 
    132                         { 
    133                                 echo '<li style="color:', ($result === TRUE) ? 'green' : 'red', '">'; 
    134                                 echo html::specialchars($method), ': ', ($result === TRUE) ? 'passed' : 'failed'; 
    135                                 echo '</li>'; 
    136                         } 
    137  
    138                         echo '</ul>'; 
    139                         echo '<hr />'; 
    140                 } 
    141         } 
    142  
    143  
    144         public function assert_true($value, $strict = TRUE) 
     122         * @param   boolean      set to TRUE to echo the output instead of returning it 
     123         * @return  string|void  rendered test results html 
     124         */ 
     125        public function render($print = FALSE) 
     126        { 
     127                $view = new View('kohana_unit_test'); 
     128                $view->results = $this->results; 
     129                return $view->render($print); 
     130        } 
     131 
     132        /** 
     133         * Magically convert this object to a string. 
     134         * 
     135         * @return  string 
     136         */ 
     137        public function __toString() 
     138        { 
     139                return $this->render(); 
     140        } 
     141 
     142} // End Unit_Test_Core 
     143 
     144 
     145class Unit_Test_Case { 
     146 
     147        public function assert_true($value, $strict = FALSE) 
    145148        { 
    146149                if ($strict === TRUE AND $value !== TRUE) 
     
    151154        } 
    152155 
    153         public function assert_false($value, $strict = TRUE) 
     156        public function assert_false($value, $strict = FALSE) 
    154157        { 
    155158                if ($strict === TRUE AND $value !== FALSE) 
     
    160163        } 
    161164 
    162         public function assert_equal($value1, $value2, $strict = TRUE) 
     165        public function assert_equal($value1, $value2, $strict = FALSE) 
    163166        { 
    164167                if ($strict === TRUE AND $value1 !== $value2) 
     
    169172        } 
    170173 
    171         public function assert_not_equal($value1, $value2, $strict = TRUE) 
     174        public function assert_not_equal($value1, $value2, $strict = FALSE) 
    172175        { 
    173176                if ($strict === TRUE AND $value1 === $value2) 
     
    250253        } 
    251254 
    252 } // End Unit_Test 
     255        public function assert_empty($value) 
     256        { 
     257                if ( ! empty($value)) 
     258                        throw new Exception; 
     259        } 
     260 
     261        public function assert_not_empty($value) 
     262        { 
     263                if (empty($value)) 
     264                        throw new Exception; 
     265        } 
     266 
     267        public function assert_pattern($value, $regex) 
     268        { 
     269                if ( ! preg_match($regex, $value)) 
     270                        throw new Exception; 
     271        } 
     272 
     273        public function assert_not_pattern($value, $regex) 
     274        { 
     275                if (preg_match($regex, $value)) 
     276                        throw new Exception; 
     277        } 
     278 
     279} // End Unit_Test_Case 
  • trunk/system/libraries/View.php

    r2152 r2247  
    2727         * @param   array   pre-load data 
    2828         * @param   string  type of file: html, css, js, etc. 
     29         * @return  object 
    2930         */ 
    3031        public static function factory($name, $data = NULL, $type = NULL)