Permalink
Browse files

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
  • Loading branch information...
1 parent 82641e5 commit 0ee8889e6d9fb26873a0d8a87832f8c201757d9d @davidpersson davidpersson committed Apr 24, 2009
Showing with 141 additions and 56 deletions.
  1. +36 −30 cake/console/libs/shell.php
  2. +105 −26 cake/tests/cases/console/libs/shell.test.php
View
66 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
View
131 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
@@ -148,32 +165,6 @@ function testInitialize() {
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
*
* @return void
@@ -211,6 +202,94 @@ function testIn() {
$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
*
* @return void

0 comments on commit 0ee8889

Please sign in to comment.