Permalink
Browse files

Demonstrate case statement

  • Loading branch information...
1 parent 1a9cd6e commit 512aac91e605e95b0e0f8b8a5ff5466d84a5a0fc Avdi Grimm committed Dec 31, 2009
Showing with 20 additions and 7 deletions.
  1. +5 −3 lib/cowsay.rb
  2. +15 −4 spec/cowsay_spec.rb
View
@@ -23,9 +23,11 @@ def say(message, options={})
if options[:out]
options[:out] << result
end
- destination = options[:out].try(:path) ||
- options[:out].try(:inspect) ||
- "return value"
+ 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
View
@@ -1,4 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'tempfile'
module Cowsay
describe Cow do
@@ -61,10 +62,10 @@ def set_child_exit_status(status)
end
it "should log the filename of output file" do
- out = StringIO.new
- out.stub!(:path).and_return("/OUTPUT_PATH")
- @log.should_receive(:info).with(/\/OUTPUT_PATH/)
- @it.say("moo", :out => out)
+ Tempfile.open('cowsay_spec') do |f|
+ @log.should_receive(:info).with(/#{f.path}/)
+ @it.say("moo", :out => f)
+ end
end
end
@@ -93,5 +94,15 @@ def set_child_exit_status(status)
end.should raise_error(ArgumentError)
end
end
+
+ 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
end
end

0 comments on commit 512aac9

Please sign in to comment.