From c9787e1224c5c7d9735663c1aac0e724b0ba7069 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 24 Mar 2012 23:12:51 +0000 Subject: [PATCH] Minor rework of Complex Assertions for unit tests git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@88321 2327b42d-5241-43d6-9e2a-de5ac946f064 --- unitTests/custom/Complex.php | 6 ++++-- unitTests/custom/complexAssert.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/unitTests/custom/Complex.php b/unitTests/custom/Complex.php index f5a67c01..c827baeb 100644 --- a/unitTests/custom/Complex.php +++ b/unitTests/custom/Complex.php @@ -15,15 +15,17 @@ class Complex { // Fix silly human errors if (strpos($complexNumber,'+-') !== FALSE) $complexNumber = str_replace('+-','-',$complexNumber); + if (strpos($complexNumber,'++') !== FALSE) + $complexNumber = str_replace('++','+',$complexNumber); if (strpos($complexNumber,'--') !== FALSE) $complexNumber = str_replace('--','-',$complexNumber); // 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) { // 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) { throw new Exception('COMPLEX: Invalid complex number'); } diff --git a/unitTests/custom/complexAssert.php b/unitTests/custom/complexAssert.php index 680aa2ea..486e0929 100644 --- a/unitTests/custom/complexAssert.php +++ b/unitTests/custom/complexAssert.php @@ -9,6 +9,16 @@ class complexAssert { 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); $actualComplex = new Complex($actual);