Permalink
Browse files

Demonstrate verbose array-ification.

  • Loading branch information...
1 parent 512aac9 commit 29e207739eea9ea3a14e572ff41bd81498c4fc30 Avdi Grimm committed Dec 31, 2009
Showing with 34 additions and 20 deletions.
  1. +27 −18 lib/cowsay.rb
  2. +7 −2 spec/cowsay_spec.rb
View
@@ -12,29 +12,38 @@ def say(message, options={})
if options[:strings] && options[:strings][:eyes]
command << " -e '#{options[:strings][:eyes]}'"
end
- result = @io_class.popen(command, "w+") do |process|
- result = begin
- process.write(message)
- process.close_write
- result = process.read
- rescue Errno::EPIPE
- message
+
+ messages = case message
+ when Array then message
+ when nil then []
+ else [message]
end
- if options[:out]
- options[:out] << result
+ results = []
+ messages.each do |message|
+ @io_class.popen(command, "w+") do |process|
+ results << begin
+ process.write(message)
+ process.close_write
+ result = process.read
+ rescue Errno::EPIPE
+ message
+ end
end
- destination = case options[:out]
- when nil then "return value"
- when File then options[:out].path
- else options[:out].inspect
- end
- @logger.info "Wrote to #{destination}"
- result
end
+ output = results.join("\n")
+ if options[:out]
+ options[:out] << output
+ end
+ destination = case options[:out]
+ when nil then "return value"
+ when File then options[:out].path
+ else options[:out].inspect
+ end
+ @logger.info "Wrote to #{destination}"
if $? && ![0,172].include?($?.exitstatus)
- raise ArgumentError, $?.exitstatus.to_s
+ raise ArgumentError, "Command exited with status #{$?.exitstatus.to_s}"
end
- result
+ output
end
end
end
View
@@ -11,6 +11,7 @@ def set_child_exit_status(status)
end
before :each do
+ set_child_exit_status(0)
@process = stub("process", :read => "OUTPUT").as_null_object
@io_class = stub("IO Class")
@log = stub("Log").as_null_object
@@ -97,12 +98,16 @@ def set_child_exit_status(status)
context "multiple messages" do
it "should render each message in order" do
- @io_class.should_receive(:popen).and_yield(@process).ordered
@process.should_receive(:write).with("foo").ordered
- @io_class.should_receive(:popen).and_yield(@process).ordered
@process.should_receive(:write).with("bar").ordered
@it.say(["foo", "bar"])
end
end
+
+ context "nil message" do
+ it "should return empty string" do
+ @it.say(nil).should be == ""
+ end
+ end
end
end

0 comments on commit 29e2077

Please sign in to comment.