Changeset 2270

Show
Ignore:
Timestamp:
03/10/2008 09:41:45 AM (7 months ago)
Author:
Geert
Message:

Made assert methods chainable. Thanks, allain.

Location:
trunk/modules/unit_test
Files:
2 modified

Legend:

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

    r2265 r2270  
    145145                if ($value != TRUE) 
    146146                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_true', gettype($value), var_export($value, TRUE)), $debug); 
     147 
     148                return $this; 
    147149        } 
    148150 
     
    151153                if ($value !== TRUE) 
    152154                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_true_strict', gettype($value), var_export($value, TRUE)), $debug); 
     155 
     156                return $this; 
    153157        } 
    154158 
     
    157161                if ($value != FALSE) 
    158162                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_false', gettype($value), var_export($value, TRUE)), $debug); 
     163 
     164                return $this; 
    159165        } 
    160166 
     
    163169                if ($value !== FALSE) 
    164170                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_false_strict', gettype($value), var_export($value, TRUE)), $debug); 
     171 
     172                return $this; 
    165173        } 
    166174 
     
    169177                if ($value1 != $value2) 
    170178                        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); 
     179 
     180                return $this; 
    171181        } 
    172182 
     
    175185                if ($value1 == $value2) 
    176186                        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); 
     187 
     188                return $this; 
    177189        } 
    178190 
     
    181193                if ($value1 !== $value2) 
    182194                        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); 
     195 
     196                return $this; 
    183197        } 
    184198 
     
    187201                if ($value1 === $value2) 
    188202                        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); 
     203 
     204                return $this; 
    189205        } 
    190206 
     
    193209                if ( ! is_bool($value)) 
    194210                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_boolean', gettype($value), var_export($value, TRUE)), $debug); 
     211 
     212                return $this; 
    195213        } 
    196214 
     
    199217                if (is_bool($value)) 
    200218                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_boolean', gettype($value), var_export($value, TRUE)), $debug); 
     219 
     220                return $this; 
    201221        } 
    202222 
     
    205225                if ( ! is_int($value)) 
    206226                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_integer', gettype($value), var_export($value, TRUE)), $debug); 
     227 
     228                return $this; 
    207229        } 
    208230 
     
    211233                if (is_int($value)) 
    212234                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_integer', gettype($value), var_export($value, TRUE)), $debug); 
     235 
     236                return $this; 
    213237        } 
    214238 
     
    217241                if ( ! is_float($value)) 
    218242                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_float', gettype($value), var_export($value, TRUE)), $debug); 
     243 
     244                return $this; 
    219245        } 
    220246 
     
    223249                if (is_float($value)) 
    224250                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_float', gettype($value), var_export($value, TRUE)), $debug); 
     251 
     252                return $this; 
    225253        } 
    226254 
     
    229257                if ( ! is_array($value)) 
    230258                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_array', gettype($value), var_export($value, TRUE)), $debug); 
     259 
     260                return $this; 
    231261        } 
    232262 
     
    235265                if (is_array($value)) 
    236266                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_array', gettype($value), var_export($value, TRUE)), $debug); 
     267 
     268                return $this; 
    237269        } 
    238270 
     
    241273                if ( ! is_object($value)) 
    242274                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_object', gettype($value), var_export($value, TRUE)), $debug); 
     275 
     276                return $this; 
    243277        } 
    244278 
     
    247281                if (is_object($value)) 
    248282                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_object', gettype($value), var_export($value, TRUE)), $debug); 
     283 
     284                return $this; 
    249285        } 
    250286 
     
    253289                if ($value !== NULL) 
    254290                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_null', gettype($value), var_export($value, TRUE)), $debug); 
     291 
     292                return $this; 
    255293        } 
    256294 
     
    259297                if ($value === NULL) 
    260298                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_null', gettype($value), var_export($value, TRUE)), $debug); 
     299 
     300                return $this; 
    261301        } 
    262302 
     
    265305                if ( ! empty($value)) 
    266306                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_empty', gettype($value), var_export($value, TRUE)), $debug); 
     307 
     308                return $this; 
    267309        } 
    268310 
     
    271313                if (empty($value)) 
    272314                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_empty', gettype($value), var_export($value, TRUE)), $debug); 
     315 
     316                return $this; 
    273317        } 
    274318 
     
    277321                if ( ! is_string($value) OR ! is_string($regex) OR ! preg_match($regex, $value)) 
    278322                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_pattern', var_export($value, TRUE), var_export($regex, TRUE)), $debug); 
     323 
     324                return $this; 
    279325        } 
    280326 
     
    283329                if ( ! is_string($value) OR ! is_string($regex) OR preg_match($regex, $value)) 
    284330                        throw new Kohana_Unit_Test_Exception(Kohana::lang('unit_test.assert_not_pattern', var_export($value, TRUE), var_export($regex, TRUE)), $debug); 
     331 
     332                return $this; 
    285333        } 
    286334 
  • trunk/modules/unit_test/tests/Example_Test.php

    r2267 r2270  
    1515        { 
    1616                $var = TRUE; 
    17                 $this->assert_true($var); 
    18                 $this->assert_true_strict($var); 
    19                 $this->assert_false( ! $var); 
    20                 $this->assert_false_strict( ! $var); 
     17                $this 
     18                        ->assert_true($var) 
     19                        ->assert_true_strict($var) 
     20                        ->assert_false( ! $var) 
     21                        ->assert_false_strict( ! $var); 
    2122        } 
    2223 
     
    2425        { 
    2526                $var = '5'; 
    26                 $this->assert_equal($var, 5); 
    27                 $this->assert_not_equal($var, 6); 
    28                 $this->assert_identical($var, '5'); 
    29                 $this->assert_not_identical($var, 5); 
     27                $this 
     28                        ->assert_equal($var, 5) 
     29                        ->assert_not_equal($var, 6) 
     30                        ->assert_identical($var, '5') 
     31                        ->assert_not_identical($var, 5); 
    3032        } 
    3133 
    3234        public function type_test() 
    3335        { 
    34                 $this->assert_boolean(TRUE); 
    35                 $this->assert_not_boolean('TRUE'); 
    36                 $this->assert_integer(123); 
    37                 $this->assert_not_integer('123'); 
    38                 $this->assert_float(1.23); 
    39                 $this->assert_not_float(123); 
    40                 $this->assert_array(array(1, 2, 3)); 
    41                 $this->assert_not_array('array()'); 
    42                 $this->assert_object(new stdClass); 
    43                 $this->assert_not_object('X'); 
    44                 $this->assert_null(NULL); 
    45                 $this->assert_not_null(0); 
    46                 $this->assert_empty('0'); 
    47                 $this->assert_not_empty('1'); 
     36                $this 
     37                        ->assert_boolean(TRUE) 
     38                        ->assert_not_boolean('TRUE') 
     39                        ->assert_integer(123) 
     40                        ->assert_not_integer('123') 
     41                        ->assert_float(1.23) 
     42                        ->assert_not_float(123) 
     43                        ->assert_array(array(1, 2, 3)) 
     44                        ->assert_not_array('array()') 
     45                        ->assert_object(new stdClass) 
     46                        ->assert_not_object('X') 
     47                        ->assert_null(NULL) 
     48                        ->assert_not_null(0) 
     49                        ->assert_empty('0') 
     50                        ->assert_not_empty('1'); 
    4851        } 
    4952 
     
    5154        { 
    5255                $var = "Kohana\n"; 
    53                 $this->assert_pattern($var, '/^Kohana$/'); 
    54                 $this->assert_not_pattern($var, '/^Kohana$/D'); 
     56                $this 
     57                        ->assert_pattern($var, '/^Kohana$/') 
     58                        ->assert_not_pattern($var, '/^Kohana$/D'); 
    5559        } 
    5660