Permalink
Browse files

first functional version

  • Loading branch information...
Avdi Grimm
Avdi Grimm committed Dec 28, 2009
1 parent e0b22c2 commit 72d091abbc5e1c31486c6daa9fa7138b4e641e19
Showing with 55 additions and 4 deletions.
  1. +15 −0 lib/cowsay.rb
  2. +39 −3 spec/cowsay_spec.rb
  3. +1 −1 spec/spec.opts
View
@@ -0,0 +1,15 @@
+module Cowsay
+ class Cow
+ def initialize(options={})
+ @io_class = options.fetch(:io_class){IO}
+ end
+
+ def say(message)
+ @io_class.popen("cowsay", "w+") do |process|
+ process.write(message)
+ process.close_write
+ process.read
+ end
+ end
+ end
+end
View
@@ -1,7 +1,43 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe "Cowsay" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+module Cowsay
+ describe Cow do
+ before :each do
+ @process = stub("process", :read => "OUTPUT").as_null_object
+ @io_class = stub("IO Class")
+ @io_class.stub!(:popen).and_yield(@process)
+ @it = Cow.new(:io_class => @io_class)
+ end
+
+ it "should be able to say hello" do
+ @process.should_receive(:write).with("hello")
+ @it.say("hello")
+ end
+
+ it "should start the cowsay process" do
+ @io_class.should_receive(:popen).with("cowsay", anything)
+ @it.say("foo")
+ end
+
+ it "should close the cowsay process after writing" do
+ @process.should_receive(:write).ordered
+ @process.should_receive(:close_write).ordered
+ @it.say("foo")
+ end
+
+ it "should read process output after closing process input" do
+ @process.should_receive(:close_write).ordered
+ @process.should_receive(:read).ordered
+ @it.say("foo")
+ end
+
+ it "should return the result of reading from the process" do
+ @it.say("foo").should be == "OUTPUT"
+ end
+
+ it "should open cowsay process for read/write" do
+ @io_class.should_receive(:popen).with(anything, 'w+')
+ @it.say("foo")
+ end
end
end
View
@@ -1 +1 @@
---color
+-fs

0 comments on commit 72d091a

Please sign in to comment.