Changeset 2247
- Timestamp:
- 03/09/2008 05:59:16 AM (7 months ago)
- Location:
- trunk/system
- Files:
-
- 1 added
- 2 modified
-
libraries/Unit_Test.php (modified) (8 diffs)
-
libraries/View.php (modified) (1 diff)
-
views/kohana_unit_test.php (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/libraries/Unit_Test.php
r2246 r2247 21 21 * @return void 22 22 */ 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) 27 27 { 28 $this->paths = array(APPPATH.'tests/');28 $this->paths[] = str_replace('\\', '/', realpath($path)).'/'; 29 29 } 30 30 31 // Normalize all given test paths32 else33 {34 foreach ((array) $paths as $path)35 {36 $this->paths[] = str_replace('\\', '/', realpath($path)).'/';37 }38 }39 40 31 Log::add('debug', 'Unit_Test Library initialized'); 32 33 // Automatically run tests 34 $this->run(); 41 35 } 42 36 … … 83 77 continue; 84 78 85 // Instantiate class79 // Instantiate Test class 86 80 $object = new $class; 81 82 // Test classes must extend Unit_Test_Case 83 if ( ! $object instanceof Unit_Test_Case) 84 break; 87 85 88 86 // Run setup method … … 108 106 $object->teardown(); 109 107 } 108 109 // Cleanup 110 unset($object); 110 111 } 111 112 } 112 113 } 113 114 115 // Return raw results array 114 116 return $this->results; 115 117 } … … 118 120 * Generates nice test results. 119 121 * 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 145 class Unit_Test_Case { 146 147 public function assert_true($value, $strict = FALSE) 145 148 { 146 149 if ($strict === TRUE AND $value !== TRUE) … … 151 154 } 152 155 153 public function assert_false($value, $strict = TRUE)156 public function assert_false($value, $strict = FALSE) 154 157 { 155 158 if ($strict === TRUE AND $value !== FALSE) … … 160 163 } 161 164 162 public function assert_equal($value1, $value2, $strict = TRUE)165 public function assert_equal($value1, $value2, $strict = FALSE) 163 166 { 164 167 if ($strict === TRUE AND $value1 !== $value2) … … 169 172 } 170 173 171 public function assert_not_equal($value1, $value2, $strict = TRUE)174 public function assert_not_equal($value1, $value2, $strict = FALSE) 172 175 { 173 176 if ($strict === TRUE AND $value1 === $value2) … … 250 253 } 251 254 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 27 27 * @param array pre-load data 28 28 * @param string type of file: html, css, js, etc. 29 * @return object 29 30 */ 30 31 public static function factory($name, $data = NULL, $type = NULL)
