Minor rework of Complex Assertions for unit tests

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@88321 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2012-03-24 23:12:51 +00:00
parent c98eaf18f7
commit c9787e1224
2 changed files with 14 additions and 2 deletions

View File

@ -15,15 +15,17 @@ class Complex {
// Fix silly human errors // Fix silly human errors
if (strpos($complexNumber,'+-') !== FALSE) if (strpos($complexNumber,'+-') !== FALSE)
$complexNumber = str_replace('+-','-',$complexNumber); $complexNumber = str_replace('+-','-',$complexNumber);
if (strpos($complexNumber,'++') !== FALSE)
$complexNumber = str_replace('++','+',$complexNumber);
if (strpos($complexNumber,'--') !== FALSE) if (strpos($complexNumber,'--') !== FALSE)
$complexNumber = str_replace('--','-',$complexNumber); $complexNumber = str_replace('--','-',$complexNumber);
// Basic validation of string, to parse out real and imaginary parts, and any suffix // Basic validation of string, to parse out real and imaginary parts, and any suffix
$validComplex = preg_match('/^([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)?(([-+]?)([ij]?))$/ui',$complexNumber,$complexParts); $validComplex = preg_match('/^([\-\+]?(\d+\.?\d*|\d*\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?)([\-\+]?(\d+\.?\d*|\d*\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?)?(([\-\+]?)([ij]?))$/ui',$complexNumber,$complexParts);
if (!$validComplex) { if (!$validComplex) {
// Neither real nor imaginary part, so test to see if we actually have a suffix // Neither real nor imaginary part, so test to see if we actually have a suffix
$validComplex = preg_match('/^([-+]?)([ij])$/ui',$complexNumber,$complexParts); $validComplex = preg_match('/^([\-\+]?)([ij])$/ui',$complexNumber,$complexParts);
if (!$validComplex) { if (!$validComplex) {
throw new Exception('COMPLEX: Invalid complex number'); throw new Exception('COMPLEX: Invalid complex number');
} }

View File

@ -9,6 +9,16 @@ class complexAssert {
public function assertComplexEquals($expected, $actual, $delta = 0) public function assertComplexEquals($expected, $actual, $delta = 0)
{ {
if ($expected{0} === '#') {
// Expecting an error, so we do a straight string comparison
if ($expected === $actual) {
return TRUE;
}
$this->_errorMessage = 'Expected Error: ' .
$actual . ' !== ' . $expected;
return FALSE;
}
$expectedComplex = new Complex($expected); $expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual); $actualComplex = new Complex($actual);