From 9fba92438519e1864c61a85df329d4ccce8f9544 Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Fri, 25 Feb 2011 23:18:41 -0500 Subject: [PATCH 1/7] modified mock_bot and tests to pass (stubbing EventMachine connection) --- lib/isaac/bot.rb | 4 +- test/helper.rb | 164 ++++++++++++++++++++++++++---------------- test/test_commands.rb | 20 +++--- test/test_events.rb | 14 ++-- test/test_helpers.rb | 2 +- test/test_irc.rb | 8 ++- test/test_parse.rb | 26 +++---- test/test_queue.rb | 16 ++--- 8 files changed, 151 insertions(+), 103 deletions(-) mode change 100644 => 100755 test/helper.rb diff --git a/lib/isaac/bot.rb b/lib/isaac/bot.rb index 21fa31e..dcefcf2 100644 --- a/lib/isaac/bot.rb +++ b/lib/isaac/bot.rb @@ -118,9 +118,11 @@ def invoke(block) class IRC < EventMachine::Connection def self.connect(bot, config) - EventMachine.connect(config.server, config.port, self, bot, config) + EventMachine.connect(config.server, config.port, IRCClient, bot, config) end + end + module IRCClient def initialize(bot, config) @bot, @config = bot, config @transfered = 0 diff --git a/test/helper.rb b/test/helper.rb old mode 100644 new mode 100755 index 8db722f..addd81a --- a/test/helper.rb +++ b/test/helper.rb @@ -1,60 +1,104 @@ -$LOAD_PATH.unshift 'lib' -require 'isaac' -require 'rubygems' -require 'test/unit' -require 'contest' -require 'rr' -require 'timeout' -begin - require 'ruby-debug' -rescue LoadError; end - -module Test::Unit::Assertions - def assert_empty_buffer(io) - assert_raise(Errno::EAGAIN) { io.read_nonblock 1 } - end -end - -class MockSocket - def self.pipe - socket1, socket2 = new, new - socket1.in, socket2.out = IO.pipe - socket2.in, socket1.out = IO.pipe - [socket1, socket2] - end - - attr_accessor :in, :out - def gets() - Timeout.timeout(1) {@in.gets} - end - def puts(m) @out.puts(m) end - def print(m) @out.print(m) end - def eof?() @in.eof? end - def empty? - begin - @in.read_nonblock(1) - false - rescue Errno::EAGAIN - true - end - end -end - -class Test::Unit::TestCase - include RR::Adapters::TestUnit - - def mock_bot(&b) - @socket, @server = MockSocket.pipe - stub(TCPSocket).open(anything, anything) {@socket} - bot = Isaac::Bot.new(&b) - bot.config.environment = :test - Thread.start { bot.start } - bot - end - - def bot_is_connected - assert_equal "NICK isaac\r\n", @server.gets - assert_equal "USER isaac 0 * :Isaac\r\n", @server.gets - 1.upto(4) {|i| @server.print ":localhost 00#{i}\r\n"} - end -end +$LOAD_PATH.unshift 'lib' +require 'isaac/bot' +require 'rubygems' +require 'test/unit' +require 'contest' +require 'rr' +require 'timeout' +begin + require 'ruby-debug' +rescue LoadError; end + +class MockSocket + def self.pipe + socket1, socket2 = new, new + socket1.in, socket2.out = IO.pipe + socket2.in, socket1.out = IO.pipe + [socket1, socket2] + end + + attr_accessor :in, :out + def gets() + Timeout.timeout(1) {@in.gets} + end + def puts(m) @out.puts(m) end + def print(m) @out.print(m) end + def eof?() @in.eof? end + def empty? + begin + @in.read_nonblock(1) + false + rescue Errno::EAGAIN + true + end + end +end + + +class FakeReactor + + def poll(io, &blk) + (@polls ||= {})[io] = lambda { + blk.call(io.read_nonblock(1)) + } + end + + def react! + @polls.each do |_, proc| + loop do + begin + proc.call + rescue Errno::EAGAIN + break + end + end + end + end + +end + + +class StubIRCClient + include Isaac::IRCClient + + attr_accessor :socket + + def send_data data + @socket.print data + end + +end + + +class Test::Unit::TestCase + include RR::Adapters::TestUnit + + def mock_bot(bot=nil, &b) + @socket, @server = MockSocket.pipe + bot ||= Isaac::Bot.new(&b) + @r = FakeReactor.new + stub(Isaac::IRC).connect(anything, anything) do + conn = StubIRCClient.new(bot, bot.config) + conn.socket = @socket + conn.post_init + @r.poll(@socket.in) { |data| conn.receive_data data } + conn + end + bot.config.environment = :test + bot.start + bot + end + + def bot_is_connected + assert_equal "NICK isaac\r\n", @server.gets + assert_equal "USER isaac 0 * :Isaac\r\n", @server.gets + 1.upto(4) {|i| @server.print ":localhost 00#{i}\r\n" } + end + + def react! + @r.react! + end + +end + + diff --git a/test/test_commands.rb b/test/test_commands.rb index 0dd0b31..da1c2a8 100644 --- a/test/test_commands.rb +++ b/test/test_commands.rb @@ -5,7 +5,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.raw "PRIVMSG foo :bar baz" + bot.raw "PRIVMSG foo :bar baz"; react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -13,7 +13,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.msg "foo", "bar baz" + bot.msg "foo", "bar baz"; react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -21,7 +21,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.action "foo", "bar" + bot.action "foo", "bar"; react! assert_equal "PRIVMSG foo :\001ACTION bar\001\r\n", @server.gets end @@ -29,7 +29,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.join "#foo", "#bar" + bot.join "#foo", "#bar"; react! assert_equal "JOIN #foo\r\n", @server.gets assert_equal "JOIN #bar\r\n", @server.gets end @@ -38,7 +38,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.part "#foo", "#bar" + bot.part "#foo", "#bar"; react! assert_equal "PART #foo\r\n", @server.gets assert_equal "PART #bar\r\n", @server.gets end @@ -47,7 +47,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.topic "#foo", "bar baz" + bot.topic "#foo", "bar baz"; react! assert_equal "TOPIC #foo :bar baz\r\n", @server.gets end @@ -55,7 +55,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.mode "#foo", "+o" + bot.mode "#foo", "+o"; react! assert_equal "MODE #foo +o\r\n", @server.gets end @@ -63,7 +63,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.kick "foo", "bar", "bein' a baz" + bot.kick "foo", "bar", "bein' a baz"; react! assert_equal "KICK foo bar :bein' a baz\r\n", @server.gets end @@ -71,7 +71,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.quit + bot.quit; react! assert_equal "QUIT\r\n", @server.gets end @@ -79,7 +79,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.quit "I'm outta here!" + bot.quit "I'm outta here!"; react! assert_equal "QUIT :I'm outta here!\r\n", @server.gets end end diff --git a/test/test_events.rb b/test/test_events.rb index c282239..db418b1 100644 --- a/test/test_events.rb +++ b/test/test_events.rb @@ -15,7 +15,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "Hey") + dispatch(:channel, :message => "Hey"); react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -25,7 +25,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "lolcat") + dispatch(:channel, :message => "lolcat"); react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -35,7 +35,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "Hey") + dispatch(:channel, :message => "Hey"); react! assert @server.empty? end @@ -44,7 +44,7 @@ def dispatch(type, env) on(:connect) {msg "foo", "bar baz"} } bot_is_connected - + react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -54,7 +54,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "foo bar") + dispatch(:channel, :message => "foo bar"); react! assert_equal "PRIVMSG foo :bar\r\n", @server.gets end @@ -68,7 +68,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "foo bar") + dispatch(:channel, :message => "foo bar"); react! assert_equal "foo\r\n", @server.gets assert_equal "bar\r\n", @server.gets @@ -82,7 +82,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "foo bar") + dispatch(:channel, :message => "foo bar"); react! assert_equal "foo\r\n", @server.gets end diff --git a/test/test_helpers.rb b/test/test_helpers.rb index 8393527..de14858 100644 --- a/test/test_helpers.rb +++ b/test/test_helpers.rb @@ -8,7 +8,7 @@ class TestHelpers < Test::Unit::TestCase } bot_is_connected - bot.irc.parse ":johnny!john@doe.com PRIVMSG isaac :hello, you!" + bot.irc.parse ":johnny!john@doe.com PRIVMSG isaac :hello, you!"; react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end end diff --git a/test/test_irc.rb b/test/test_irc.rb index 6cdf6b1..d4f2370 100644 --- a/test/test_irc.rb +++ b/test/test_irc.rb @@ -3,7 +3,7 @@ class TestIrc < Test::Unit::TestCase test "a new bot connects to IRC" do bot = mock_bot {} - + react! assert_equal "NICK isaac\r\n", @server.gets assert_equal "USER isaac 0 * :#{bot.config.realname}\r\n", @server.gets end @@ -12,6 +12,7 @@ class TestIrc < Test::Unit::TestCase bot = mock_bot { configure {|c| c.password = "foo"} } + react! assert_equal "PASS foo\r\n", @server.gets end @@ -19,8 +20,9 @@ class TestIrc < Test::Unit::TestCase bot = mock_bot { on(:connect) {raw "Connected!"} } + 2.times { @server.gets } # NICK / USER - bot.dispatch :connect + bot.dispatch :connect; react! assert @server.empty? end @@ -32,7 +34,7 @@ class TestIrc < Test::Unit::TestCase 2.times { @server.gets } # NICK / USER bot.dispatch :connect - 1.upto(4) {|i| @server.puts ":localhost 00#{i}"} + 1.upto(4) {|i| @server.puts ":localhost 00#{i}"}; react! assert_equal "Connected!\r\n", @server.gets end end diff --git a/test/test_parse.rb b/test/test_parse.rb index 87e7f19..f841860 100644 --- a/test/test_parse.rb +++ b/test/test_parse.rb @@ -5,7 +5,7 @@ class TestParse < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - @server.print "PING :foo.bar\r\n" + @server.print "PING :foo.bar\r\n"; react! assert_equal "PONG :foo.bar\r\n", @server.gets end @@ -15,7 +15,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":johnny!john@doe.com JOIN #foo\r\n" + @server.print ":johnny!john@doe.com JOIN #foo\r\n"; react! assert_equal "PRIVMSG #foo :bar baz\r\n", @server.gets end @@ -25,7 +25,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":johnny!john@doe.com PART #foo :Leaving\r\n" + @server.print ":johnny!john@doe.com PART #foo :Leaving\r\n"; react! assert_equal "PRIVMSG #foo :johnny left: Leaving\r\n", @server.gets end @@ -35,7 +35,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":johnny!john@doe.com QUIT :Leaving\r\n" + @server.print ":johnny!john@doe.com QUIT :Leaving\r\n"; react! assert_equal "PRIVMSG #foo :johnny quit: Leaving\r\n", @server.gets end @@ -45,7 +45,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":johnny!john@doe.com PRIVMSG isaac :hello, you!\r\n" + @server.print ":johnny!john@doe.com PRIVMSG isaac :hello, you!\r\n"; react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -55,7 +55,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":johnny!john@doe.com PRIVMSG #awesome :hello, folks!\r\n" + @server.print ":johnny!john@doe.com PRIVMSG #awesome :hello, folks!\r\n"; react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -65,7 +65,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print "PRIVMSG #awesome :hello, folks!\r\n" + @server.print "PRIVMSG #awesome :hello, folks!\r\n"; react! assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -80,7 +80,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.puts ":johnny!john@doe.com PRIVMSG isaac :hello, you!" + @server.puts ":johnny!john@doe.com PRIVMSG isaac :hello, you!"; react! assert_equal "johnny\r\n", @server.gets assert_equal "john\r\n", @server.gets assert_equal "doe.com\r\n", @server.gets @@ -99,7 +99,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.puts ":johnny!john@doe.com PRIVMSG #awesome :hello, folks!" + @server.puts ":johnny!john@doe.com PRIVMSG #awesome :hello, folks!"; react! assert_equal "johnny\r\n", @server.gets assert_equal "john\r\n", @server.gets assert_equal "doe.com\r\n", @server.gets @@ -115,7 +115,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":server 401 isaac jeff :No such nick/channel\r\n" + @server.print ":server 401 isaac jeff :No such nick/channel\r\n"; react! assert_equal "401\r\n", @server.gets end @@ -127,7 +127,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print "401 isaac jeff :No such nick/channel\r\n" + @server.print "401 isaac jeff :No such nick/channel\r\n"; react! assert_equal "401\r\n", @server.gets end @@ -137,7 +137,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":jeff!spicoli@name.com PRIVMSG isaac :\001VERSION\001\r\n" + @server.print ":jeff!spicoli@name.com PRIVMSG isaac :\001VERSION\001\r\n"; react! assert_equal "NOTICE jeff :\001VERSION Ridgemont 0.1\001\r\n", @server.gets end @@ -147,7 +147,7 @@ class TestParse < Test::Unit::TestCase } bot_is_connected - @server.print ":johnny!john@doe.com PRIVMSG #awesome :hello, folks!\r\n" + @server.print ":johnny!john@doe.com PRIVMSG #awesome :hello, folks!\r\n"; react! assert_equal "PRIVMSG foo :hello, folks! he said\r\n", @server.gets end end diff --git a/test/test_queue.rb b/test/test_queue.rb index 3fd5734..a4c170c 100644 --- a/test/test_queue.rb +++ b/test/test_queue.rb @@ -17,32 +17,32 @@ def flood_bot test "ping after sending 1472 consequent bytes" do - bot = flood_bot + bot = flood_bot; react! - bot.dispatch :connect + bot.dispatch(:connect); react! 16.times { @server.gets } assert_equal "PING :#{bot.config.server}\r\n", @server.gets assert @server.empty? end test "reset transfer amount at pong reply" do - bot = flood_bot + bot = flood_bot; react! - bot.dispatch :connect + bot.dispatch :connect; react! 16.times { @server.gets } @server.gets # PING message - @server.puts ":localhost PONG :localhost" + @server.puts ":localhost PONG :localhost"; react! assert_equal "this should not flood!\r\n", @server.gets end test "reset transfer amount at server ping" do - bot = flood_bot + bot = flood_bot; react! - bot.dispatch :connect + bot.dispatch :connect; react! 16.times { @server.gets } @server.gets # PING message triggered by transfer lock - @server.puts "PING :localhost" + @server.puts "PING :localhost"; react! assert_equal "this should not flood!\r\n", @server.gets end From a44fda229dfe199e472b4b2ac0a770d8f601ac84 Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Fri, 25 Feb 2011 23:44:52 -0500 Subject: [PATCH 2/7] simplified tests --- test/helper.rb | 1 + test/test_commands.rb | 20 ++++++++++---------- test/test_events.rb | 12 ++++++------ test/test_helpers.rb | 2 +- test/test_irc.rb | 6 +++--- test/test_queue.rb | 12 ++++++------ 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index addd81a..7cccb37 100755 --- a/test/helper.rb +++ b/test/helper.rb @@ -93,6 +93,7 @@ def bot_is_connected assert_equal "NICK isaac\r\n", @server.gets assert_equal "USER isaac 0 * :Isaac\r\n", @server.gets 1.upto(4) {|i| @server.print ":localhost 00#{i}\r\n" } + react! end def react! diff --git a/test/test_commands.rb b/test/test_commands.rb index da1c2a8..0dd0b31 100644 --- a/test/test_commands.rb +++ b/test/test_commands.rb @@ -5,7 +5,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.raw "PRIVMSG foo :bar baz"; react! + bot.raw "PRIVMSG foo :bar baz" assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -13,7 +13,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.msg "foo", "bar baz"; react! + bot.msg "foo", "bar baz" assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -21,7 +21,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.action "foo", "bar"; react! + bot.action "foo", "bar" assert_equal "PRIVMSG foo :\001ACTION bar\001\r\n", @server.gets end @@ -29,7 +29,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.join "#foo", "#bar"; react! + bot.join "#foo", "#bar" assert_equal "JOIN #foo\r\n", @server.gets assert_equal "JOIN #bar\r\n", @server.gets end @@ -38,7 +38,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.part "#foo", "#bar"; react! + bot.part "#foo", "#bar" assert_equal "PART #foo\r\n", @server.gets assert_equal "PART #bar\r\n", @server.gets end @@ -47,7 +47,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.topic "#foo", "bar baz"; react! + bot.topic "#foo", "bar baz" assert_equal "TOPIC #foo :bar baz\r\n", @server.gets end @@ -55,7 +55,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.mode "#foo", "+o"; react! + bot.mode "#foo", "+o" assert_equal "MODE #foo +o\r\n", @server.gets end @@ -63,7 +63,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.kick "foo", "bar", "bein' a baz"; react! + bot.kick "foo", "bar", "bein' a baz" assert_equal "KICK foo bar :bein' a baz\r\n", @server.gets end @@ -71,7 +71,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.quit; react! + bot.quit assert_equal "QUIT\r\n", @server.gets end @@ -79,7 +79,7 @@ class TestCommands < Test::Unit::TestCase bot = mock_bot {} bot_is_connected - bot.quit "I'm outta here!"; react! + bot.quit "I'm outta here!" assert_equal "QUIT :I'm outta here!\r\n", @server.gets end end diff --git a/test/test_events.rb b/test/test_events.rb index db418b1..a55d771 100644 --- a/test/test_events.rb +++ b/test/test_events.rb @@ -15,7 +15,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "Hey"); react! + dispatch(:channel, :message => "Hey") assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -25,7 +25,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "lolcat"); react! + dispatch(:channel, :message => "lolcat") assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -35,7 +35,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "Hey"); react! + dispatch(:channel, :message => "Hey") assert @server.empty? end @@ -54,7 +54,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "foo bar"); react! + dispatch(:channel, :message => "foo bar") assert_equal "PRIVMSG foo :bar\r\n", @server.gets end @@ -68,7 +68,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "foo bar"); react! + dispatch(:channel, :message => "foo bar") assert_equal "foo\r\n", @server.gets assert_equal "bar\r\n", @server.gets @@ -82,7 +82,7 @@ def dispatch(type, env) } bot_is_connected - dispatch(:channel, :message => "foo bar"); react! + dispatch(:channel, :message => "foo bar") assert_equal "foo\r\n", @server.gets end diff --git a/test/test_helpers.rb b/test/test_helpers.rb index de14858..8393527 100644 --- a/test/test_helpers.rb +++ b/test/test_helpers.rb @@ -8,7 +8,7 @@ class TestHelpers < Test::Unit::TestCase } bot_is_connected - bot.irc.parse ":johnny!john@doe.com PRIVMSG isaac :hello, you!"; react! + bot.irc.parse ":johnny!john@doe.com PRIVMSG isaac :hello, you!" assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end end diff --git a/test/test_irc.rb b/test/test_irc.rb index d4f2370..35e83bc 100644 --- a/test/test_irc.rb +++ b/test/test_irc.rb @@ -3,7 +3,7 @@ class TestIrc < Test::Unit::TestCase test "a new bot connects to IRC" do bot = mock_bot {} - react! + assert_equal "NICK isaac\r\n", @server.gets assert_equal "USER isaac 0 * :#{bot.config.realname}\r\n", @server.gets end @@ -12,7 +12,7 @@ class TestIrc < Test::Unit::TestCase bot = mock_bot { configure {|c| c.password = "foo"} } - react! + assert_equal "PASS foo\r\n", @server.gets end @@ -22,7 +22,7 @@ class TestIrc < Test::Unit::TestCase } 2.times { @server.gets } # NICK / USER - bot.dispatch :connect; react! + bot.dispatch :connect assert @server.empty? end diff --git a/test/test_queue.rb b/test/test_queue.rb index a4c170c..609c76c 100644 --- a/test/test_queue.rb +++ b/test/test_queue.rb @@ -17,18 +17,18 @@ def flood_bot test "ping after sending 1472 consequent bytes" do - bot = flood_bot; react! + bot = flood_bot - bot.dispatch(:connect); react! + bot.dispatch(:connect) 16.times { @server.gets } assert_equal "PING :#{bot.config.server}\r\n", @server.gets assert @server.empty? end test "reset transfer amount at pong reply" do - bot = flood_bot; react! + bot = flood_bot - bot.dispatch :connect; react! + bot.dispatch :connect 16.times { @server.gets } @server.gets # PING message @@ -37,9 +37,9 @@ def flood_bot end test "reset transfer amount at server ping" do - bot = flood_bot; react! + bot = flood_bot - bot.dispatch :connect; react! + bot.dispatch :connect 16.times { @server.gets } @server.gets # PING message triggered by transfer lock @server.puts "PING :localhost"; react! From 9238af37b98df165443230925fd1df8920c6991d Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Sat, 26 Feb 2011 21:14:47 -0500 Subject: [PATCH 3/7] add channels to config, auto join channels on connect if specified --- lib/isaac/bot.rb | 5 +++-- test/test_events.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/isaac/bot.rb b/lib/isaac/bot.rb index dcefcf2..0f293ab 100644 --- a/lib/isaac/bot.rb +++ b/lib/isaac/bot.rb @@ -3,7 +3,7 @@ module Isaac VERSION = '0.2.1' - Config = Struct.new(:server, :port, :ssl, :password, :nick, :realname, :version, :environment, :verbose, :encoding) + Config = Struct.new(:server, :port, :ssl, :password, :nick, :realname, :version, :environment, :verbose, :encoding, :channels) class Bot attr_accessor :config, :irc, :nick, :channel, :message, :user, :host, :match, @@ -11,7 +11,7 @@ class Bot def initialize(&b) @events = {} - @config = Config.new("localhost", 6667, false, nil, "isaac", "Isaac", 'isaac', :production, false, "utf-8") + @config = Config.new("localhost", 6667, false, nil, "isaac", "Isaac", 'isaac', :production, false, "utf-8", []) instance_eval(&b) if block_given? end @@ -73,6 +73,7 @@ def quit(message=nil) def start puts "Connecting to #{@config.server}:#{@config.port}" unless @config.environment == :test @irc = IRC.connect(self, @config) + on(:connect) { join *@config.channels } unless @config.channels.empty? end def message diff --git a/test/test_events.rb b/test/test_events.rb index a55d771..0fc4653 100644 --- a/test/test_events.rb +++ b/test/test_events.rb @@ -44,7 +44,7 @@ def dispatch(type, env) on(:connect) {msg "foo", "bar baz"} } bot_is_connected - react! + assert_equal "PRIVMSG foo :bar baz\r\n", @server.gets end @@ -86,4 +86,15 @@ def dispatch(type, env) assert_equal "foo\r\n", @server.gets end + + test "specified channels are joined on connect" do + @bot = mock_bot { + configure { |c| c.channels = ["#foo", "#bar"] } + } + bot_is_connected + + assert_equal "JOIN #foo\r\n", @server.gets + assert_equal "JOIN #bar\r\n", @server.gets + end + end From b15ca968fa1427048b22ed4afc89c919bcb0ee9d Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Sat, 26 Feb 2011 22:23:46 -0500 Subject: [PATCH 4/7] add Bot#configure_from to load config from file --- lib/isaac/bot.rb | 6 ++++++ test/test_configure_from.rb | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 test/test_configure_from.rb diff --git a/lib/isaac/bot.rb b/lib/isaac/bot.rb index dcefcf2..c79ea5e 100644 --- a/lib/isaac/bot.rb +++ b/lib/isaac/bot.rb @@ -20,6 +20,12 @@ def configure(&b) b.call(@config) end + def configure_from(file) + cfgfile = ::File.read(file) + cfgfile.sub!(/^__END__\n.*/, '') + eval "Proc.new {|config| " + cfgfile + "\n}.call(@config)", binding, file + end + def on(event, match=//, &block) match = match.to_s if match.is_a? Integer (@events[event] ||= []) << [Regexp.new(match), block] diff --git a/test/test_configure_from.rb b/test/test_configure_from.rb new file mode 100755 index 0000000..1ee23bc --- /dev/null +++ b/test/test_configure_from.rb @@ -0,0 +1,40 @@ +require 'helper' +require 'tempfile' + +class TestConfigureFrom < Test::Unit::TestCase + include Isaac + + def setup + @file = Tempfile.open('config') do |f| + f.write( +<<__EOF__ + config.server = "irc.foo.com" + config.nick = "foo" + config.realname = "Bar" +__EOF__ + ) + f + end + end + + def teardown + @file.unlink + end + + test "config is loaded from specified file" do + bot = Bot.new + + bot.configure_from(@file.path) + assert_equal "irc.foo.com", bot.config.server + assert_equal "foo", bot.config.nick + assert_equal "Bar", bot.config.realname + end + + test "default config is not changed" do + bot = Bot.new + + bot.configure_from(@file.path) + assert_equal 6667, bot.config.port + end + +end \ No newline at end of file From 07c0477d951b07fcab61de748ae13dacf1521a7c Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Sat, 26 Feb 2011 22:45:57 -0500 Subject: [PATCH 5/7] simplified configure_from --- lib/isaac/bot.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/isaac/bot.rb b/lib/isaac/bot.rb index c79ea5e..5e4cee5 100644 --- a/lib/isaac/bot.rb +++ b/lib/isaac/bot.rb @@ -23,7 +23,7 @@ def configure(&b) def configure_from(file) cfgfile = ::File.read(file) cfgfile.sub!(/^__END__\n.*/, '') - eval "Proc.new {|config| " + cfgfile + "\n}.call(@config)", binding, file + instance_eval( cfgfile ) end def on(event, match=//, &block) From b6138ebfd3dcd4a500e60c4e99bae5aad2bc2bd8 Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Sat, 26 Feb 2011 23:15:54 -0500 Subject: [PATCH 6/7] puts debug msgs to stdout explicitly --- lib/isaac/bot.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/isaac/bot.rb b/lib/isaac/bot.rb index 5e4cee5..3146e85 100644 --- a/lib/isaac/bot.rb +++ b/lib/isaac/bot.rb @@ -77,7 +77,7 @@ def quit(message=nil) end def start - puts "Connecting to #{@config.server}:#{@config.port}" unless @config.environment == :test + $stdout.puts "Connecting to #{@config.server}:#{@config.port}" unless @config.environment == :test @irc = IRC.connect(self, @config) end @@ -155,7 +155,7 @@ def receive_data(data) end def parse(input) - puts "<< #{input}" if @bot.config.verbose + $stdout.puts "<< #{input}" if @bot.config.verbose msg = Message.new(input) if ("001".."004").include? msg.command From d06a1878d220ac9d2b037563bafc9ec63cb811a5 Mon Sep 17 00:00:00 2001 From: Eric Gjertsen Date: Sun, 27 Feb 2011 16:44:50 -0500 Subject: [PATCH 7/7] move autojoin to irc after dispatch(:connect) instead of as on(:connect) event; move tests to test_autojoin --- lib/isaac/bot.rb | 2 +- test/test_events.rb | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/isaac/bot.rb b/lib/isaac/bot.rb index 0f293ab..dc12f7d 100644 --- a/lib/isaac/bot.rb +++ b/lib/isaac/bot.rb @@ -73,7 +73,6 @@ def quit(message=nil) def start puts "Connecting to #{@config.server}:#{@config.port}" unless @config.environment == :test @irc = IRC.connect(self, @config) - on(:connect) { join *@config.channels } unless @config.channels.empty? end def message @@ -158,6 +157,7 @@ def parse(input) if registered? @queue.unlock @bot.dispatch(:connect) + @bot.join(*@bot.config.channels) unless @bot.config.channels.empty? end elsif msg.command == "PRIVMSG" if msg.params.last == "\001VERSION\001" diff --git a/test/test_events.rb b/test/test_events.rb index 0fc4653..4bed2e7 100644 --- a/test/test_events.rb +++ b/test/test_events.rb @@ -87,14 +87,4 @@ def dispatch(type, env) assert_equal "foo\r\n", @server.gets end - test "specified channels are joined on connect" do - @bot = mock_bot { - configure { |c| c.channels = ["#foo", "#bar"] } - } - bot_is_connected - - assert_equal "JOIN #foo\r\n", @server.gets - assert_equal "JOIN #bar\r\n", @server.gets - end - end