Created
October 24, 2011 09:10
-
-
Save meza/1308639 to your computer and use it in GitHub Desktop.
TDD vs. Regular Expressions - coding kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TimeValidator | |
{ | |
public function isValid($time_specification) | |
{ | |
return 1 == preg_match($this->buildRegExp(), $time_specification); | |
} | |
private function buildRegExp() | |
{ | |
$minutes = '([0-5][0-9])'; | |
$hours_less_than_24 = '([0-1][0-9]|2[0-3])'; | |
$time_spec_24 = "$hours_less_than_24:$minutes"; | |
$hours_less_than13 = '(0[1-9]|1[012])'; | |
$ampm = '( [AP]M)'; | |
$time_spec_12 = "$hours_less_than13:$minutes$ampm"; | |
$time_specification_matcher = "/^($time_spec_24|$time_spec_12)\$/D"; | |
return $time_specification_matcher; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'TimeValidator.php'; | |
class TimeValidatorTest extends PHPUnit_Framework_TestCase | |
{ | |
public function setUp() | |
{ | |
$this->validator = new TimeValidator(); | |
} | |
/** | |
* @dataProvider provideValidTimeSpecifications | |
*/ | |
public function testValidTimeSpecificationsAreAccepted($time_specification) | |
{ | |
$this->assertTrue($this->validator->isValid($time_specification)); | |
} | |
public function provideValidTimeSpecifications() | |
{ | |
return array( | |
'24 hour time specification' => array('10:42'), | |
'12 hour time specification in the morning' => array('10:42 AM'), | |
'12 hour time specification in the afternoon' => array('10:42 PM'), | |
'12 hour time specification with leading zero' => array('01:42 PM'), | |
'24 hour time specification with leading zero' => array('00:42'), | |
'First minute of a 24 hour specification' => array('00:00'), | |
'First minute of a 12 hour specification' => array('01:00 AM'), | |
'Last 12 hour time specification in the morning' => array('12:59 AM'), | |
'Last 12 hour time specification in the afternoon' => array('12:59 PM'), | |
'Last 24 hour time specification' => array('23:59'), | |
); | |
} | |
/** | |
* @dataProvider provideInvalidTimeSpecifications | |
*/ | |
public function testInvalidTimeSpecificationsAreNotAccepted($time_specification) | |
{ | |
$this->assertFalse($this->validator->isValid($time_specification)); | |
} | |
public function provideInvalidTimeSpecifications() | |
{ | |
return array( | |
'Time specification must contain at least one colon' => array('1042'), | |
'Time specification must contain at most one colon' => array('10::42'), | |
'Time specification must not contain colon after minutes' => array('10:42:'), | |
'Hours must be numeric' => array('ab:42'), | |
'Hours must be less than 24' => array('24:42'), | |
'Minutes must be numeric' => array('10:ab'), | |
'Minutes must be less than 60' => array('10:60'), | |
'Hours must be two-digit' => array('1:42'), | |
'Minutes must be two-digit' => array('10:4'), | |
'12 hour format must end with AM or PM' => array('10:42 XY'), | |
'24 hour format must be exactly 5 characters long' => array('14:42 PM'), | |
'Hour part of 12 hour format must be less than 13' => array('13:00 PM'), | |
'12 hour format must be exactly 8 characters long' => array('12:45 AM'), | |
'Multiline strings are not accepted' => array("10:42\n"), | |
'12 hour time specification does not allow hours to be zero' => array('00:42 PM'), | |
); | |
} | |
private $validator; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment