From 2a27d312074598346e29bfe76bb87b81a00554d4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 25 Dec 2011 17:08:24 +0200 Subject: Improve the Unit test library --- system/libraries/Unit_test.php | 90 +++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 54 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/Unit_test.php b/system/libraries/Unit_test.php index 7afe40b09..dac1a5c7e 100644 --- a/system/libraries/Unit_test.php +++ b/system/libraries/Unit_test.php @@ -1,13 +1,13 @@ -active == FALSE) { @@ -110,11 +110,7 @@ class CI_Unit_test { } else { - if ($this->strict == TRUE) - $result = ($test === $expected) ? TRUE : FALSE; - else - $result = ($test == $expected) ? TRUE : FALSE; - + $result = ($this->strict == TRUE) ? ($test === $expected) : ($test == $expected); $extype = gettype($expected); } @@ -145,9 +141,9 @@ class CI_Unit_test { * @access public * @return string */ - function report($result = array()) + public function report($result = array()) { - if (count($result) == 0) + if (count($result) === 0) { $result = $this->result(); } @@ -176,10 +172,7 @@ class CI_Unit_test { } } - $temp = $this->_template_rows; - $temp = str_replace('{item}', $key, $temp); - $temp = str_replace('{result}', $val, $temp); - $table .= $temp; + $table .= str_replace(array('{item}', '{result}'), array($key, $val), $this->_template_rows); } $r .= str_replace('{rows}', $table, $this->_template); @@ -199,9 +192,9 @@ class CI_Unit_test { * @param bool * @return null */ - function use_strict($state = TRUE) + public function use_strict($state = TRUE) { - $this->strict = ($state == FALSE) ? FALSE : TRUE; + $this->strict = (bool) $state; } // -------------------------------------------------------------------- @@ -215,9 +208,9 @@ class CI_Unit_test { * @param bool * @return null */ - function active($state = TRUE) + public function active($state = TRUE) { - $this->active = ($state == FALSE) ? FALSE : TRUE; + $this->active = (bool) $state; } // -------------------------------------------------------------------- @@ -230,12 +223,12 @@ class CI_Unit_test { * @access public * @return array */ - function result($results = array()) + public function result($results = array()) { $CI =& get_instance(); $CI->load->language('unit_test'); - if (count($results) == 0) + if (count($results) === 0) { $results = $this->results; } @@ -289,7 +282,7 @@ class CI_Unit_test { * @param string * @return void */ - function set_template($template) + public function set_template($template) { $this->_template = $template; } @@ -304,16 +297,15 @@ class CI_Unit_test { * @access private * @return array */ - function _backtrace() + private function _backtrace() { if (function_exists('debug_backtrace')) { $back = debug_backtrace(); - - $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file']; - $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line']; - - return array('file' => $file, 'line' => $line); + return array( + 'file' => (isset($back[1]['file']) ? $back[1]['file'] : ''), + 'line' => (isset($back[1]['line']) ? $back[1]['line'] : '') + ); } return array('file' => 'Unknown', 'line' => 'Unknown'); } @@ -326,16 +318,12 @@ class CI_Unit_test { * @access private * @return string */ - function _default_template() + private function _default_template() { - $this->_template = "\n".''; - $this->_template .= '{rows}'; - $this->_template .= "\n".'
'; - - $this->_template_rows = "\n\t".''; - $this->_template_rows .= "\n\t\t".'{item}'; - $this->_template_rows .= "\n\t\t".'{result}'; - $this->_template_rows .= "\n\t".''; + $this->_template = "\n".'{rows}'."\n".'
'; + + $this->_template_rows = "\n\t\n\t\t".'{item}' + . "\n\t\t".'{result}'."\n\t"; } // -------------------------------------------------------------------- @@ -348,27 +336,21 @@ class CI_Unit_test { * @access private * @return void */ - function _parse_template() + private function _parse_template() { if ( ! is_null($this->_template_rows)) { return; } - if (is_null($this->_template)) - { - $this->_default_template(); - return; - } - - if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match)) + if (is_null($this->_template) OR ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match)) { $this->_default_template(); return; } - $this->_template_rows = $match['1']; - $this->_template = str_replace($match['0'], '{rows}', $this->_template); + $this->_template_rows = $match[1]; + $this->_template = str_replace($match[0], '{rows}', $this->_template); } } -- cgit v1.2.3-24-g4f1b