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

Commit

Permalink
MockSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
vangberg committed Mar 2, 2009
1 parent aecc3d2 commit 992aa8b
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions shout-bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ def say(message)
require "context"
require "rr"

class MockSocket
attr_accessor :in, :out
def gets() @in.gets end
def puts(m) @out.puts(m) end
def eof?() true end
end

class ShoutBot
include Test::Unit::Assertions
end
Expand All @@ -89,7 +96,10 @@ class Test::Unit::TestCase
include RR::Adapters::TestUnit

def setup
@socket = StringIO.new
@socket, @server = MockSocket.new, MockSocket.new
@socket.in, @server.out = IO.pipe
@server.in, @socket.out = IO.pipe

stub(TCPSocket).open(anything, anything) {@socket}
end
end
Expand All @@ -101,8 +111,7 @@ def create_shoutbot(&block)

def create_shoutbot_and_register(&block)
create_shoutbot &block
@socket.rewind
2.times { @socket.gets }
2.times { @server.gets } # NICK + USER
end

test "raises error if no block given" do
Expand All @@ -113,23 +122,27 @@ def create_shoutbot_and_register(&block)

test "registers to the irc server" do
create_shoutbot
@socket.rewind
assert_equal "NICK john\n", @socket.gets
assert_equal "USER john john john :john\n", @socket.gets
assert_equal "NICK john\n", @server.gets
assert_equal "USER john john john :john\n", @server.gets
end

test "sends password if specified" do
ShoutBot.new("irc.freenode.net", 6667, "john", "malbec") {}
@socket.rewind
assert_equal "PASSWORD malbec\n", @socket.gets
# hey, retarded test!
ShoutBot.new("irc.freenode.net", 6667, "john", "malbec") {@socket}
assert_equal "PASSWORD malbec\n", @server.gets
end

test "falls back to port 6667 if not specified" do
# talk about retarded test
mock(TCPSocket).open("irc.freenode.net", 6667) {StringIO.new}
mock(TCPSocket).open("irc.freenode.net", 6667) {@socket}
ShoutBot.new("irc.freenode.net", nil, "john") {}
end

test "quits after doing its job" do
create_shoutbot_and_register {}
assert_equal "QUIT\n", @server.gets
end

test "raises error if no block is given to join" do
create_shoutbot do |bot|
assert_raises(ArgumentError) {bot.join "integrity"}
Expand All @@ -140,7 +153,11 @@ def create_shoutbot_and_register(&block)
create_shoutbot_and_register do |bot|
bot.join("integrity") {}
end
assert_equal "JOIN #integrity\n", @socket.gets
assert_equal "JOIN #integrity\n", @server.gets
end

test "doesn't do anything until receiving RPL_MYINFO / 004" do
# pending
end

test "joins channel and says something" do
Expand All @@ -149,16 +166,16 @@ def create_shoutbot_and_register(&block)
c.say "foo bar!"
end
end
@socket.gets # JOIN
assert_equal "PRIVMSG #integrity :foo bar!\n", @socket.gets
@server.gets # JOIN
assert_equal "PRIVMSG #integrity :foo bar!\n", @server.gets
end

test "sends private message to user" do
create_shoutbot_and_register do |bot|
bot.channel = "sr"
bot.say "Look Ma, new tests!"
end
assert_equal "PRIVMSG sr :Look Ma, new tests!\n", @socket.gets
assert_equal "PRIVMSG sr :Look Ma, new tests!\n", @server.gets
end
end

Expand Down

0 comments on commit 992aa8b

Please sign in to comment.