From 0ee8889e6d9fb26873a0d8a87832f8c201757d9d Mon Sep 17 00:00:00 2001 From: davidpersson Date: Fri, 24 Apr 2009 21:04:07 +0200 Subject: [PATCH] Adding Shell::nl method Updating out to not use newline feature of SD::stdout Updating out, err and hr methods to utilize the new method Rewording parameters of out, err and hr Changing handling of multiple messages Adding tests for nl, hr and error Updating tests for out and err Updating docblocks --- cake/console/libs/shell.php | 66 +++++----- cake/tests/cases/console/libs/shell.test.php | 131 +++++++++++++++---- 2 files changed, 141 insertions(+), 56 deletions(-) diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index 01855bcebe3..a0a194d7065 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -338,53 +338,59 @@ function in($prompt, $options = null, $default = null) { } } /** - * Outputs to the stdout filehandle. + * Outputs a single or multiple messages to stdout. * - * @param string $string String to output. - * @param boolean $newline If true, the outputs gets an added newline. + * @param mixed $message A string or a an array of strings to output + * @param mixed $after Appended to message, if true a newline is used * @access public */ - function out($string, $newline = true) { - if (is_array($string)) { - $str = ''; - foreach ($string as $message) { - $str .= $message ."\n"; - } - $string = $str; + function out($message, $after = true) { + if (is_array($message)) { + $message = implode($this->nl(), $message); } - return $this->Dispatch->stdout($string, $newline); + $this->Dispatch->stdout($message . $this->nl($after), false); } /** - * Outputs to the stderr filehandle. + * Outputs a single or multiple error messages to stderr. * - * @param string $string Error text to output. - * @param boolean $newline If true, the outputs gets an added newline. + * @param mixed $message A string or a an array of strings to output + * @param mixed $after Appended to message, if true a newline is used * @access public */ - function err($string, $newline = true) { - if (is_array($string)) { - $str = ''; - foreach ($string as $message) { - $str .= $message ."\n"; - } - $string = $str; + function err($message, $after = true) { + if (is_array($message)) { + $message = implode($this->nl(), $message); } - return $this->Dispatch->stderr($string . ($newline ? "\n" : '')); + $this->Dispatch->stderr($message . $this->nl($after)); } /** - * Outputs a series of minus characters to the standard output, acts as a visual separator. + * Returns a single or multiple linefeeds sequences. * - * @param boolean $newline If true, the outputs gets an added newline. + * @param mixed $format If true returns a linefeed sequence, if false null, + * if a string is given that is returned, + * if an integer is given it is used as a multiplier to return multiple linefeed sequences * @access public + * @return string */ - function hr($newline = false) { - if ($newline) { - $this->out("\n"); + function nl($format = true) { + if (is_string($format)) { + return $format; } - $this->out('---------------------------------------------------------------'); - if ($newline) { - $this->out("\n"); + if (is_integer($format)) { + return str_repeat("\n", $format); } + return $format ? "\n" : null; + } +/** + * Outputs a series of minus characters to the standard output, acts as a visual separator. + * + * @param mixed $surround If true, the outputs gets surrounded by newlines. + * @access public + */ + function hr($surround = false) { + $this->out(null, $surround); + $this->out('---------------------------------------------------------------'); + $this->out(null, $surround); } /** * Displays a formatted error message diff --git a/cake/tests/cases/console/libs/shell.test.php b/cake/tests/cases/console/libs/shell.test.php index 8d03541fc3f..6144842a40a 100644 --- a/cake/tests/cases/console/libs/shell.test.php +++ b/cake/tests/cases/console/libs/shell.test.php @@ -48,6 +48,23 @@ * @subpackage cake.tests.cases.console.libs */ class TestShell extends Shell { +/** + * stopped property + * + * @var integer + * @access public + */ + var $stopped; +/** + * stop method + * + * @param integer $status + * @access protected + * @return void + */ + function _stop($status = 0) { + $this->stopped = $status; + } } /** * TestAppleTask class @@ -147,32 +164,6 @@ function testInitialize() { Configure::write('pluginPaths', $_back['pluginPaths']); Configure::write('modelPaths', $_back['modelPaths']); } -/** - * testOut method - * - * @return void - * @access public - */ - function testOut() { - $this->Shell->Dispatch->expectAt(0, 'stdout', array('Just a test', true)); - $this->Shell->out('Just a test'); - - $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", true)); - $this->Shell->out(array('Just', 'a', 'test')); - } -/** - * testErr method - * - * @return void - * @access public - */ - function testErr() { - $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n")); - $this->Shell->err('Just a test'); - - $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n\n")); - $this->Shell->err(array('Just', 'a', 'test')); - } /** * testIn method * @@ -210,6 +201,94 @@ function testIn() { $result = $this->Shell->in('Just a test?', 'y/n', 'n'); $this->assertEqual($result, 'n'); } +/** + * testOut method + * + * @return void + * @access public + */ + function testOut() { + $this->Shell->Dispatch->expectAt(0, 'stdout', array("Just a test\n", false)); + $this->Shell->out('Just a test'); + + $this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", false)); + $this->Shell->out(array('Just', 'a', 'test')); + + $this->Shell->Dispatch->expectAt(2, 'stdout', array("Just\na\ntest\n\n", false)); + $this->Shell->out(array('Just', 'a', 'test'), 2); + } +/** + * testErr method + * + * @return void + * @access public + */ + function testErr() { + $this->Shell->Dispatch->expectAt(0, 'stderr', array("Just a test\n")); + $this->Shell->err('Just a test'); + + $this->Shell->Dispatch->expectAt(1, 'stderr', array("Just\na\ntest\n")); + $this->Shell->err(array('Just', 'a', 'test')); + + $this->Shell->Dispatch->expectAt(2, 'stderr', array("Just\na\ntest\n\n")); + $this->Shell->err(array('Just', 'a', 'test'), 2); + } +/** + * testNl + * + * @access public + * @return void + */ + function testNl() { + $this->assertEqual($this->Shell->nl(), "\n"); + $this->assertEqual($this->Shell->nl(true), "\n"); + $this->assertEqual($this->Shell->nl(false), ""); + $this->assertEqual($this->Shell->nl(2), "\n\n"); + $this->assertEqual($this->Shell->nl(1), "\n"); + $this->assertEqual($this->Shell->nl("custom\n"), "custom\n"); + } +/** + * testHr + * + * @access public + * @return void + */ + function testHr() { + $bar = '---------------------------------------------------------------'; + + $this->Shell->Dispatch->expectAt(0, 'stdout', array('', false)); + $this->Shell->Dispatch->expectAt(1, 'stdout', array($bar . "\n", false)); + $this->Shell->Dispatch->expectAt(2, 'stdout', array('', false)); + $this->Shell->hr(); + + $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n", false)); + $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false)); + $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n", false)); + $this->Shell->hr(true); + + $this->Shell->Dispatch->expectAt(3, 'stdout', array("\n\n", false)); + $this->Shell->Dispatch->expectAt(4, 'stdout', array($bar . "\n", false)); + $this->Shell->Dispatch->expectAt(5, 'stdout', array("\n\n", false)); + $this->Shell->hr(2); + } +/** + * testError + * + * @access public + * @return void + */ + function testError() { + $this->Shell->Dispatch->expectAt(0, 'stderr', array("Error: Foo Not Found\n")); + $this->Shell->error('Foo Not Found'); + $this->assertIdentical($this->Shell->stopped, 1); + + $this->Shell->stopped = null; + + $this->Shell->Dispatch->expectAt(1, 'stderr', array("Error: Foo Not Found\n")); + $this->Shell->Dispatch->expectAt(2, 'stderr', array("Searched all...\n")); + $this->Shell->error('Foo Not Found', 'Searched all...'); + $this->assertIdentical($this->Shell->stopped, 1); + } /** * testLoadTasks method *