Skip to content

Commit

Permalink
switched assert example to check cowfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Avdi Grimm committed Jan 10, 2010
1 parent db149a9 commit 84ddeef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/cowsay.rb
Expand Up @@ -8,12 +8,14 @@ def initialize(options={})
end

def say(message, options={})
out = options[:out]
assert(out.nil? || out.respond_to?(:<<))
assert(options[:cowfile].nil? || File.exist?(options[:cowfile]))
command = "cowsay"
if options[:strings] && options[:strings][:eyes]
command << " -e '#{options[:strings][:eyes]}'"
end
if options[:cowfile]
command << " -f #{options[:cowfile]}"
end

messages = case message
when Array then message
Expand All @@ -33,8 +35,8 @@ def say(message, options={})
end
end
output = results.join("\n")
if out
out << output
if options[:out]
options[:out] << output
end
destination = case options[:out]
when nil then "return value"
Expand Down
17 changes: 13 additions & 4 deletions spec/cowsay_spec.rb
Expand Up @@ -110,11 +110,20 @@ def set_child_exit_status(status)
end
end

context "bad :out option" do
it "should raise ArgumentError" do
context "given a cowfile" do
it "should supply a -f argument on the command line" do
File.stub!(:exist?).and_return(true)
@io_class.should_receive(:popen).with(/-f COWFILE/, anything)
@it.say("moo", :cowfile => "COWFILE")
end
end

context "given an invalid cowfile" do
it "should raise an error" do
File.stub!(:exist?).and_return(false)
lambda do
@it.say("whatever", :out => Object.new)
end.should raise_error
@it.say("moo", :cowfile => "COWFILE")
end.should raise_error(Exception)
end
end
end
Expand Down

0 comments on commit 84ddeef

Please sign in to comment.