Skip to content
This repository has been archived by the owner on Dec 28, 2018. It is now read-only.

Commit

Permalink
tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
sr committed Nov 18, 2008
1 parent c50a20f commit 377d8f6
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions irc_shooter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,116 @@ def say(message)
@socket.puts "PRIVMSG #{@channel} :#{message}"
end
end

if $0 == __FILE__
begin
require 'spec'
rescue LoadError
abort "No test for you :-("
end

describe "Shooter" do
def create_shooter(&block)
@shooter ||= Shooter.new("irc.freenode.net", 6667, "john", &block || lambda {})
end

setup do
@socket = mock("socket", :puts => "", :eof? => true, :gets => "")
TCPSocket.stub!(:open).and_return(@socket)
end

it "should exists" do
Shooter.should be_an_instance_of Class
end

describe "When using Shooter.shoot" do
def do_shoot(&block)
Shooter.shoot("irc://irc.freenode.net:6667/foo", :as => "john", &block || lambda {})
end

it "raises ArgumentError if no block given" do
lambda { do_shoot(nil) }.should raise_error(ArgumentError)
end

it "creates a new shooter using URI and :as option" do
Shooter.should_receive(:new).with("irc.freenode.net", 6667, "john")
do_shoot
end

it "join channel using URI's path" do
create_shooter.should_receive(:join).with("foo")
Shooter.stub!(:new).and_yield(create_shooter)
do_shoot
end

it "passes given block to join" do
pending
end
end

describe "When initializing" do
it "raises ArgumentError if no block given" do
lambda do
create_shooter(nil)
end.should raise_error(ArgumentError)
end

it "opens a TCPSocket to the given host on the given port" do
TCPSocket.should_receive(:open).with("irc.freenode.net", 6667).and_return(@socket)
create_shooter
end

it "sets its nick" do
@socket.should_receive(:puts).with("NICK john")
create_shooter
end

it "yields itself" do
create_shooter { |shooter| shooter.should respond_to(:join) }
end

it "quits" do
@socket.should_receive(:puts).with("QUIT")
create_shooter
end
end

describe "When joining a channel" do
def do_join(&block)
create_shooter { |shooter| shooter.join('foo', &block || lambda {}) }
end

it "raises ArgumentError if no block given" do
lambda do
do_join(nil)
end.should raise_error(ArgumentError)
end

it "joins the given channel" do
@socket.should_receive(:puts).with("JOIN #foo")
do_join
end

it "yields itself" do
do_join { |channel| channel.should respond_to(:say) }
end

it "parts the given channel" do
@socket.should_receive(:puts).with("PART #foo")
do_join
end
end

describe "When saying something" do
it "should say the given message in the channel" do
@socket.should_receive(:puts).with("PRIVMSG #foo :bar")
create_shooter { |shooter| shooter.join("foo") { |channel| channel.say "bar" } }
end

it "should stfu and return nil if not joined to a channel" do
@socket.should_not_receive(:puts).with("PRIVMSG #foo :bar")
create_shooter { |shooter| shooter.say("bar").should be_nil }
end
end
end
end

1 comment on commit 377d8f6

@karmi
Copy link

@karmi karmi commented on 377d8f6 Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sr: I LOVE the `if $0 == FILE` Cmd+R testing idea! :)

Please sign in to comment.