spox-mod_spox 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/CHANGELOG +5 -0
  2. data/README.rdoc +61 -1
  3. data/bin/mod_spox +1 -7
  4. data/data/mod_spox/extras/AutoKick.rb +3 -2
  5. data/data/mod_spox/extras/AutoMode.rb +2 -1
  6. data/data/mod_spox/extras/AutoRejoin.rb +5 -4
  7. data/data/mod_spox/extras/Bouncer.rb +243 -131
  8. data/data/mod_spox/extras/FloodKicker.rb +2 -1
  9. data/data/mod_spox/extras/Fortune.rb +5 -1
  10. data/data/mod_spox/extras/Karma.rb +2 -1
  11. data/data/mod_spox/extras/Logger.rb +11 -9
  12. data/data/mod_spox/extras/NickServ.rb +2 -1
  13. data/data/mod_spox/extras/PhpCli.rb +1 -1
  14. data/data/mod_spox/extras/PhpFuncLookup.rb +2 -1
  15. data/data/mod_spox/extras/RegexTracker.rb +2 -1
  16. data/data/mod_spox/extras/Roulette.rb +2 -1
  17. data/data/mod_spox/extras/Seen.rb +10 -8
  18. data/data/mod_spox/extras/Topten.rb +2 -1
  19. data/data/mod_spox/extras/Translate.rb +2 -1
  20. data/data/mod_spox/extras/Twitter.rb +1 -1
  21. data/data/mod_spox/plugins/Authenticator.rb +7 -7
  22. data/data/mod_spox/plugins/Banner.rb +6 -6
  23. data/data/mod_spox/plugins/BotNick.rb +2 -1
  24. data/data/mod_spox/plugins/Initializer.rb +4 -4
  25. data/data/mod_spox/plugins/Joiner.rb +1 -1
  26. data/data/mod_spox/plugins/PluginLoader.rb +1 -1
  27. data/data/mod_spox/plugins/Ponger.rb +8 -8
  28. data/data/mod_spox/plugins/Status.rb +1 -1
  29. data/lib/mod_spox/Bot.rb +27 -27
  30. data/lib/mod_spox/BotConfig.rb +17 -21
  31. data/lib/mod_spox/Filter.rb +29 -0
  32. data/lib/mod_spox/FilterManager.rb +63 -0
  33. data/lib/mod_spox/Helpers.rb +120 -14
  34. data/lib/mod_spox/Loader.rb +1 -1
  35. data/lib/mod_spox/MessageFactory.rb +10 -2
  36. data/lib/mod_spox/Pipeline.rb +66 -61
  37. data/lib/mod_spox/PluginManager.rb +5 -5
  38. data/lib/mod_spox/PriorityQueue.rb +21 -8
  39. data/lib/mod_spox/Sockets.rb +6 -6
  40. data/lib/mod_spox/Timer.rb +3 -3
  41. data/lib/mod_spox/Version.rb +2 -2
  42. data/lib/mod_spox/handlers/UserHost.rb +32 -0
  43. data/lib/mod_spox/handlers/Whois.rb +7 -7
  44. data/lib/mod_spox/messages/incoming/Pong.rb +11 -2
  45. data/lib/mod_spox/messages/incoming/UserHost.rb +24 -0
  46. data/lib/mod_spox/messages/internal/FilterAdd.rb +20 -0
  47. data/lib/mod_spox/messages/internal/FilterList.rb +18 -0
  48. data/lib/mod_spox/messages/internal/FilterListing.rb +22 -0
  49. data/lib/mod_spox/messages/internal/FilterRemove.rb +20 -0
  50. data/lib/mod_spox/messages/internal/Incoming.rb +15 -0
  51. data/lib/mod_spox/migrations/006_ignore.rb +21 -0
  52. data/lib/mod_spox/models/Nick.rb +11 -0
  53. data/lib/mod_spox/rfc2812.rb +2 -1
  54. data/lib/mod_spox/rfc2812_full.rb +185 -0
  55. data/tests/BotHolder.rb +7 -1
  56. data/tests/handlers/tc_Created.rb +28 -8
  57. data/tests/handlers/tc_Invite.rb +11 -9
  58. data/tests/handlers/tc_Join.rb +36 -16
  59. data/tests/handlers/tc_Kick.rb +27 -6
  60. data/tests/handlers/tc_Mode.rb +23 -13
  61. data/tests/handlers/tc_Names.rb +29 -9
  62. data/tests/handlers/tc_Nick.rb +30 -8
  63. data/tests/handlers/tc_Part.rb +30 -20
  64. data/tests/handlers/tc_Ping.rb +32 -19
  65. data/tests/handlers/tc_Pong.rb +28 -8
  66. data/tests/handlers/tc_Privmsg.rb +33 -13
  67. data/tests/handlers/tc_Quit.rb +30 -17
  68. data/tests/handlers/tc_Who.rb +8 -3
  69. data/tests/handlers/tc_Whois.rb +7 -3
  70. data/tests/lib/tc_BotConfig.rb +35 -0
  71. data/tests/lib/tc_Helpers.rb +139 -0
  72. data/tests/lib/tc_PriorityQueue.rb +31 -0
  73. metadata +17 -2
@@ -10,31 +10,44 @@ class TestPingHandler < Test::Unit::TestCase
10
10
  :w_server => ':not.configured PING :test',
11
11
  :bad => 'PING fail whale'
12
12
  }
13
+ @queue = Queue.new
14
+ @bot.pipeline.hook(self, :gather, 'ModSpox::Messages::Incoming::Ping')
15
+ require 'mod_spox/handlers/Ping'
16
+ @handler = ModSpox::Handlers::Ping.new({})
13
17
  end
14
18
 
15
- def test_wo_server
16
- assert_equal(:PING, @bot.factory.find_key(@test[:wo_server]))
17
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:wo_server])].process(@test[:wo_server])
18
- assert_kind_of(ModSpox::Messages::Incoming::Ping, result)
19
- assert_equal(@test[:wo_server], result.raw_content)
20
- assert_kind_of(String, result.string)
21
- assert_kind_of(String, result.server)
22
- assert_equal('not.configured', result.server)
23
- assert_equal('not.configured', result.string)
19
+ def gather(m)
20
+ @queue << m
21
+ end
22
+
23
+ def test_indirect
24
+ @bot.factory << @test[:wo_server]
25
+ sleep(0.1)
26
+ assert_equal(1, @queue.size)
27
+ m = @queue.pop
28
+ check_result(m)
29
+ assert_equal(m.raw_content, @test[:wo_server])
30
+ assert_equal('not.configured', m.server)
31
+ assert_equal('not.configured', m.string)
32
+ end
33
+
34
+ def test_direct
35
+ m = @handler.process(@test[:w_server])
36
+ check_result(m)
37
+ assert_equal(m.raw_content, @test[:w_server])
38
+ assert_equal('not.configured', m.server)
39
+ assert_equal('test', m.string)
40
+ end
41
+
42
+ def test_unexpected
43
+ assert_raise(ModSpox::Exceptions::GeneralException) do
44
+ @handler.process(@test[:bad])
45
+ end
24
46
  end
25
47
 
26
- def test_w_server
27
- assert_equal(:PING, @bot.factory.find_key(@test[:w_server]))
28
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:w_server])].process(@test[:w_server])
48
+ def check_result(result)
29
49
  assert_kind_of(ModSpox::Messages::Incoming::Ping, result)
30
- assert_equal(@test[:w_server], result.raw_content)
31
50
  assert_kind_of(String, result.string)
32
51
  assert_kind_of(String, result.server)
33
- assert_equal('not.configured', result.server)
34
- assert_equal('test', result.string)
35
- end
36
-
37
- def test_unexpected
38
- assert_raise(ModSpox::Exceptions::GeneralException){@bot.factory.handlers[@bot.factory.find_key(@test[:bad])].process(@test[:bad])}
39
52
  end
40
53
  end
@@ -9,20 +9,40 @@ class TestPongHandler < Test::Unit::TestCase
9
9
  :good => ':swiftco.wa.us.dal.net PONG swiftco.wa.us.dal.net :FOO',
10
10
  :bad => ':bad PONG fail'
11
11
  }
12
+ @queue = Queue.new
13
+ @bot.pipeline.hook(self, :gather, 'ModSpox::Messages::Incoming::Pong')
14
+ require 'mod_spox/handlers/Pong'
15
+ @handler = ModSpox::Handlers::Pong.new({})
12
16
  end
13
17
 
14
- def test_expected
15
- assert_equal(:PONG, @bot.factory.find_key(@test[:good]))
16
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:good])].process(@test[:good])
17
- assert_kind_of(ModSpox::Messages::Incoming::Ping, result)
18
+ def gather(m)
19
+ @queue << m
20
+ end
21
+
22
+ def test_indirect
23
+ @bot.factory << @test[:good]
24
+ sleep(0.1)
25
+ assert_equal(1, @queue.size)
26
+ m = @queue.pop
27
+ check_result(m)
28
+ end
29
+
30
+ def test_direct
31
+ check_result(@handler.process(@test[:good]))
32
+ end
33
+
34
+ def test_unexpected
35
+ assert_raise(ModSpox::Exceptions::GeneralException) do
36
+ @handler.process(@test[:bad])
37
+ end
38
+ end
39
+
40
+ def check_result(result)
41
+ assert_kind_of(ModSpox::Messages::Incoming::Pong, result)
18
42
  assert_equal(@test[:good], result.raw_content)
19
43
  assert_kind_of(String, result.string)
20
44
  assert_kind_of(String, result.server)
21
45
  assert_equal('swiftco.wa.us.dal.net', result.server)
22
46
  assert_equal('FOO', result.string)
23
47
  end
24
-
25
- def test_unexpected
26
- assert_raise(ModSpox::Exceptions::GeneralException){@bot.factory.handlers[@bot.factory.find_key(@test[:bad])].process(@test[:bad])}
27
- end
28
48
  end
@@ -11,11 +11,39 @@ class TestPrivmsgHandler < Test::Unit::TestCase
11
11
  :nick_to_channel_addressed => ':spox!~spox@host PRIVMSG #m :mod_spox: foobar',
12
12
  :bad => ':fubared PRIVMSG fail whale'
13
13
  }
14
+ @queue = Queue.new
15
+ @bot.pipeline.hook(self, :gather, 'ModSpox::Messages::Incoming::Privmsg')
16
+ require 'mod_spox/handlers/Privmsg'
17
+ @handler = ModSpox::Handlers::Privmsg.new({})
14
18
  end
15
19
 
16
- def test_nick_to_channel
17
- assert_equal(:PRIVMSG, @bot.factory.find_key(@test[:nick_to_channel]))
18
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:nick_to_channel])].process(@test[:nick_to_channel])
20
+ def gather(m)
21
+ @queue << m
22
+ end
23
+
24
+ def test_indirect
25
+ @bot.factory << @test[:nick_to_channel]
26
+ sleep(0.1)
27
+ assert_equal(1, @queue.size)
28
+ m = @queue.pop
29
+ check_nick2chan(m)
30
+ end
31
+
32
+ def test_nick
33
+ check_nick2nick(@handler.process(@test[:nick_to_nick]))
34
+ end
35
+
36
+ def test_chan_ad
37
+ check_nick2chanad(@handler.process(@test[:nick_to_channel_addressed]))
38
+ end
39
+
40
+ def test_unexpected
41
+ assert_raise(ModSpox::Exceptions::GeneralException) do
42
+ @handler.process(@test[:bad])
43
+ end
44
+ end
45
+
46
+ def check_nick2chan(result)
19
47
  assert_kind_of(ModSpox::Messages::Incoming::Privmsg, result)
20
48
  assert_equal(@test[:nick_to_channel], result.raw_content)
21
49
  assert_kind_of(ModSpox::Models::Channel, result.target)
@@ -35,9 +63,7 @@ class TestPrivmsgHandler < Test::Unit::TestCase
35
63
  assert_equal(result.target, result.replyto)
36
64
  end
37
65
 
38
- def test_nick_to_channel_addressed
39
- assert_equal(:PRIVMSG, @bot.factory.find_key(@test[:nick_to_channel_addressed]))
40
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:nick_to_channel_addressed])].process(@test[:nick_to_channel_addressed])
66
+ def check_nick2chanad(result)
41
67
  assert_kind_of(ModSpox::Messages::Incoming::Privmsg, result)
42
68
  assert_equal(@test[:nick_to_channel_addressed], result.raw_content)
43
69
  assert_kind_of(ModSpox::Models::Channel, result.target)
@@ -57,9 +83,7 @@ class TestPrivmsgHandler < Test::Unit::TestCase
57
83
  assert_equal(result.target, result.replyto)
58
84
  end
59
85
 
60
- def test_nick_to_nick
61
- assert_equal(:PRIVMSG, @bot.factory.find_key(@test[:nick_to_nick]))
62
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:nick_to_nick])].process(@test[:nick_to_nick])
86
+ def check_nick2nick(result)
63
87
  assert_kind_of(ModSpox::Messages::Incoming::Privmsg, result)
64
88
  assert_equal(@test[:nick_to_nick], result.raw_content)
65
89
  assert_kind_of(ModSpox::Models::Nick, result.target)
@@ -78,8 +102,4 @@ class TestPrivmsgHandler < Test::Unit::TestCase
78
102
  assert_equal(result.message_nocolor, result.message)
79
103
  assert_equal(result.source, result.replyto)
80
104
  end
81
-
82
- def test_unexpected
83
- assert_raise(ModSpox::Exceptions::GeneralException){@bot.factory.handlers[@bot.factory.find_key(@test[:bad])].process(@test[:bad])}
84
- end
85
105
  end
@@ -10,31 +10,44 @@ class TestQuitHandler < Test::Unit::TestCase
10
10
  :wo_message => ':spox!~spox@host QUIT :',
11
11
  :bad => ':not.configured QUIT fail whale'
12
12
  }
13
+ @queue = Queue.new
14
+ @bot.pipeline.hook(self, :gather, 'ModSpox::Messages::Incoming::Quit')
15
+ require 'mod_spox/handlers/Quit'
16
+ @handler = ModSpox::Handlers::Quit.new({})
13
17
  end
14
18
 
15
- def test_wo_message
16
- assert_equal(:QUIT, @bot.factory.find_key(@test[:wo_message]))
17
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:wo_message])].process(@test[:wo_message])
18
- assert_kind_of(ModSpox::Messages::Incoming::Quit, result)
19
- assert_equal(@test[:wo_message], result.raw_content)
20
- assert_kind_of(ModSpox::Models::Nick, result.nick)
21
- assert_equal('spox', result.nick.nick)
22
- assert_kind_of(String, result.message)
23
- assert_equal('', result.message)
19
+ def gather(m)
20
+ @queue << m
21
+ end
22
+
23
+ def test_indirect
24
+ @bot.factory << @test[:wo_message]
25
+ sleep(0.1)
26
+ assert_equal(1, @queue.size)
27
+ m = @queue.pop
28
+ check_result(m)
29
+ assert_equal(@test[:wo_message], m.raw_content)
30
+ assert_equal('', m.message)
31
+ end
32
+
33
+ def test_direct
34
+ m = @handler.process(@test[:w_message])
35
+ check_result(m)
36
+ assert_equal('Ping timeout', m.message)
37
+ assert_equal(@test[:w_message], m.raw_content)
38
+ end
39
+
40
+ def test_unexpected
41
+ assert_raise(ModSpox::Exceptions::GeneralException) do
42
+ @handler.process(@test[:bad])
43
+ end
24
44
  end
25
45
 
26
- def test_w_message
27
- assert_equal(:QUIT, @bot.factory.find_key(@test[:w_message]))
28
- result = @bot.factory.handlers[@bot.factory.find_key(@test[:w_message])].process(@test[:w_message])
46
+ def check_result(result)
29
47
  assert_kind_of(ModSpox::Messages::Incoming::Quit, result)
30
- assert_equal(@test[:w_message], result.raw_content)
31
48
  assert_kind_of(ModSpox::Models::Nick, result.nick)
32
49
  assert_equal('spox', result.nick.nick)
33
50
  assert_kind_of(String, result.message)
34
- assert_equal('Ping timeout', result.message)
35
51
  end
36
52
 
37
- def test_unexpected
38
- assert_raise(ModSpox::Exceptions::GeneralException){@bot.factory.handlers[@bot.factory.find_key(@test[:bad])].process(@test[:bad])}
39
- end
40
53
  end
@@ -15,9 +15,11 @@ class TestWhoHandler < Test::Unit::TestCase
15
15
  @test[:good] << ':host 352 spox #mod_spox ~spox host.4 swiftco.wa.us.dal.net spox H@ :0 spox'
16
16
  @test[:good] << ':host 315 spox #mod_spox :End of /WHO list.'
17
17
  @queue = Queue.new
18
- @bot.pipeline.hook(self, :gather, :Incoming_Who)
18
+ @bot.pipeline.hook(self, :gather, 'ModSpox::Messages::Incoming::Who')
19
19
  @nicks = ['pizza_', 'pizza__', 'spox', 'mod_spox']
20
20
  @ops = ['pizza_', 'spox', 'mod_spox']
21
+ require 'mod_spox/handlers/Who'
22
+ @handler = ModSpox::Handlers::Who.new({})
21
23
  end
22
24
 
23
25
  def gather(m)
@@ -43,8 +45,11 @@ class TestWhoHandler < Test::Unit::TestCase
43
45
  assert_equal(@ops.include?(nick.nick), nick.is_op?(m.location))
44
46
  end
45
47
  end
46
-
48
+
47
49
  def test_unexpected
48
- assert_raise(ModSpox::Exceptions::GeneralException){@bot.factory.handlers[@bot.factory.find_key(@test[:bad].dup)].process(@test[:bad].dup)}
50
+ assert_raise(ModSpox::Exceptions::GeneralException) do
51
+ @handler.process(@test[:bad])
52
+ end
49
53
  end
54
+
50
55
  end
@@ -16,13 +16,15 @@ class TestWhoHandler < Test::Unit::TestCase
16
16
  @test[:good] << ':swiftco.wa.us.dal.net 317 spox spox 176 1242140666 :seconds idle, signon time'
17
17
  @test[:good] << ':swiftco.wa.us.dal.net 318 spox spox :End of /WHOIS list.'
18
18
  @queue = Queue.new
19
- @bot.pipeline.hook(self, :gather, :Incoming_Whois)
19
+ @bot.pipeline.hook(self, :gather, 'ModSpox::Messages::Incoming::Whois')
20
20
  @voice = ['#ruby', '#!php']
21
21
  @ops = ['#php', '#mod_spox']
22
22
  nick = ModSpox::Models::Nick.find_or_create(:nick => 'spox')
23
23
  nick.auth.services = true
24
24
  nick.auth.save
25
25
  @con = Time.at(1242140666)
26
+ require 'mod_spox/handlers/Whois'
27
+ @handler = ModSpox::Handlers::Whois.new({})
26
28
  end
27
29
 
28
30
  def gather(m)
@@ -54,8 +56,10 @@ class TestWhoHandler < Test::Unit::TestCase
54
56
  assert_equal('myhost.com', m.nick.host)
55
57
  assert_equal('spox', m.nick.real_name)
56
58
  end
57
-
59
+
58
60
  def test_unexpected
59
- assert_raise(ModSpox::Exceptions::GeneralException){@bot.factory.handlers[@bot.factory.find_key(@test[:bad].dup)].process(@test[:bad].dup)}
61
+ assert_raise(ModSpox::Exceptions::GeneralException) do
62
+ @handler.process(@test[:bad])
63
+ end
60
64
  end
61
65
  end
@@ -0,0 +1,35 @@
1
+ require "#{File.dirname(__FILE__)}/../BotHolder.rb"
2
+
3
+ class TestBotConfig < Test::Unit::TestCase
4
+
5
+ def setup
6
+ h = BotHolder.instance
7
+ @testdir = '/home/testing'
8
+ ModSpox.mod_spox_path = @testdir
9
+ ModSpox::BotConfig.populate(false) # don't try and create directory
10
+ end
11
+
12
+ def test_valid
13
+ gemname, gem = Gem.source_index.find{|name, spec| spec.name == 'mod_spox' && spec.version.version = ModSpox.botversion}
14
+ path = gem.full_gem_path
15
+ assert_equal(path, ModSpox::BotConfig[:basepath])
16
+ assert_equal("#{path}/lib/mod_spox", ModSpox::BotConfig[:libpath])
17
+ assert_equal("#{path}/data/mod_spox", ModSpox::BotConfig[:datapath])
18
+ assert_equal("#{path}/data/mod_spox/plugins", ModSpox::BotConfig[:pluginpath])
19
+ assert_equal("#{path}/data/mod_spox/extras", ModSpox::BotConfig[:pluginextraspath])
20
+ assert_equal("#{@testdir}/.mod_spox", ModSpox::BotConfig[:userpath])
21
+ assert_equal("#{@testdir}/.mod_spox/plugins", ModSpox::BotConfig[:userpluginpath])
22
+ assert_equal("#{@testdir}/.mod_spox/config", ModSpox::BotConfig[:userconfigpath])
23
+ end
24
+
25
+ def test_invalid
26
+ assert_raise(ModSpox::Exceptions::UnknownKey){ ModSpox::BotConfig[:fubar] }
27
+ assert_raise(ModSpox::Exceptions::UnknownKey){ ModSpox::BotConfig[0] }
28
+ assert_raise(ModSpox::Exceptions::UnknownKey){ ModSpox::BotConfig['fail'] }
29
+ # assert_raise(ArgumentError){ ModSpox::BotConfig[nil] } i don't even know how to remove the to_s method
30
+ end
31
+
32
+ def test_configured
33
+ assert(!ModSpox::BotConfig.configured?)
34
+ end
35
+ end
@@ -0,0 +1,139 @@
1
+ require "#{File.dirname(__FILE__)}/../BotHolder.rb"
2
+
3
+ class TestHelpers < Test::Unit::TestCase
4
+ def setup
5
+ h = BotHolder.instance
6
+ ModSpox::Models::Server.find_or_create(:host => 'some.irc.server.com')
7
+ end
8
+ def test_format_seconds
9
+ inc = {:year => 60 * 60 * 24 * 365,
10
+ :month => 60 * 60 * 24 * 31,
11
+ :week => 60 * 60 * 24 * 7,
12
+ :day => 60 * 60 * 24,
13
+ :hour => 60 * 60,
14
+ :minute => 60,
15
+ :second => 1}
16
+ 100.times do |i|
17
+ time = rand(i)
18
+ otime = time
19
+ formatted = []
20
+ inc.each_pair do |name, value|
21
+ val = (time / value).to_i
22
+ if(val > 0)
23
+ time = time - (val * value)
24
+ formatted << "#{val} #{val == 1 ? name : "#{name}s"}"
25
+ end
26
+ end
27
+ formatted = formatted.empty? ? '0 seconds' : formatted.join(' ')
28
+ assert_equal(ModSpox::Helpers.format_seconds(otime), formatted)
29
+ end
30
+ end
31
+
32
+ def test_format_size
33
+ inc = {"byte" => 1024**0, # 1024^0
34
+ "Kilobyte" => 1024**1, # 1024^1
35
+ "Megabyte" => 1024**2, # 1024^2
36
+ "Gigabyte" => 1024**3, # 1024^3
37
+ "Terabyte" => 1024**4, # 1024^4
38
+ "Petabyte" => 1024**5, # 1024^5
39
+ "Exabyte" => 1024**6, # 1024^6
40
+ "Zettabyte" => 1024**7, # 1024^7
41
+ "Yottabyte" => 1024**8 # 1024^8
42
+ }
43
+ 100.times do |i|
44
+ val = i**rand(i)
45
+ formatted = nil
46
+ inc.each_pair do |name, value|
47
+ v = val / value.to_f
48
+ if(v.to_i > 0)
49
+ formatted = ("%.3f" % v) + " #{name}#{v == 1 ? '' : 's'}"
50
+ end
51
+ end
52
+ formatted = '0 bytes' if formatted.nil?
53
+ assert_equal(formatted, ModSpox::Helpers.format_size(val))
54
+ end
55
+ end
56
+
57
+ def test_safe_exec
58
+ assert_raise(IOError) do
59
+ ModSpox::Helpers.safe_exec('echo test', 10, 1)
60
+ end
61
+ assert_raise(Timeout::Error) do
62
+ ModSpox::Helpers.safe_exec('while [ true ]; do true; done;', 1)
63
+ end
64
+ assert_equal("test\n", ModSpox::Helpers.safe_exec('echo test'))
65
+ end
66
+
67
+ def test_tinyurl
68
+ assert_equal('http://tinyurl.com/1c2', ModSpox::Helpers.tinyurl('http://www.google.com'))
69
+ assert_equal('Error', ModSpox::Helpers.tinyurl(''))
70
+ end
71
+
72
+ def test_find_model
73
+ assert_kind_of(ModSpox::Models::Nick, ModSpox::Helpers.find_model('nick'))
74
+ assert_kind_of(ModSpox::Models::Channel, ModSpox::Helpers.find_model('#channel'))
75
+ assert_kind_of(ModSpox::Models::Server, ModSpox::Helpers.find_model('some.irc.server.com'))
76
+ assert_equal('not.a.real.server', ModSpox::Helpers.find_model('not.a.real.server'))
77
+ assert_kind_of(String, '*!*@some.host')
78
+ end
79
+
80
+ def test_convert_entities
81
+ assert_equal('<p>', ModSpox::Helpers.convert_entities('&lt;p&gt;'))
82
+ assert_equal('hi there', ModSpox::Helpers.convert_entities('hi there'))
83
+ end
84
+
85
+ def test_load_message
86
+ ModSpox::Helpers.load_message(:internal, :TimerAdd)
87
+ assert(ModSpox::Messages::Internal::TimerAdd)
88
+ assert_raise(ArgumentError){ ModSpox::Helpers.load_message(:foobar, :fee) }
89
+ assert_raise(LoadError){ ModSpox::Helpers.load_message(:internal, :fail) }
90
+ end
91
+
92
+ def test_type_of?
93
+ ModSpox::Helpers.load_message(:internal, :HaltBot)
94
+ m = ModSpox::Messages::Internal::HaltBot.new
95
+ assert(ModSpox::Helpers.type_of?(m, Object))
96
+ assert(ModSpox::Helpers.type_of?(m, ModSpox))
97
+ assert(ModSpox::Helpers.type_of?(m, ModSpox::Messages))
98
+ assert(ModSpox::Helpers.type_of?(m, ModSpox::Messages::Internal))
99
+ assert(ModSpox::Helpers.type_of?(m, ModSpox::Messages::Internal::HaltBot))
100
+ assert(ModSpox::Helpers.type_of?(m, :ModSpox_Messages_Internal_HaltBot, true))
101
+ assert(ModSpox::Helpers.type_of?(m, :Internal_HaltBot, true))
102
+ assert(ModSpox::Helpers.type_of?(m, :Internal, true))
103
+ assert(!ModSpox::Helpers.type_of?(m, String))
104
+ end
105
+
106
+ def test_find_const
107
+ ModSpox::Helpers.load_message(:internal, :HaltBot)
108
+ assert(ModSpox::Messages::Internal::HaltBot)
109
+ assert_equal(ModSpox::Messages::Internal::HaltBot, ModSpox::Helpers.find_const('ModSpox::Messages::Internal::HaltBot'))
110
+ assert_equal(ModSpox::Messages::Internal::HaltBot, ModSpox::Helpers.find_const('Internal::HaltBot'))
111
+ assert_equal('Incoming::UnknownType', ModSpox::Helpers.find_const('Incoming::UnknownType'))
112
+ end
113
+
114
+ # test partially lifted from the unit tests in the source
115
+ # provided by Ryan "pizza_" Flynn
116
+ # Class and tests found in: http://github.com/pizza/algodict
117
+
118
+ def test_random
119
+ assert_raise(ModSpox::Exceptions::GeneralException) do
120
+ 100.times do
121
+ results = []
122
+ pool = (0..rand(1000)).to_a
123
+ ideal = ModSpox::Helpers::IdealHumanRandomIterator.new(pool)
124
+ (pool.size / 2).times do
125
+ n = ideal.next()
126
+ if(results.include?(n))
127
+ raise Exception.new("Duplicated result")
128
+ else
129
+ results << n
130
+ end
131
+ end
132
+ end
133
+ raise ModSpox::Exceptions::GeneralException.new("OK")
134
+ end
135
+ assert_raise(ArgumentError){ ModSpox::Helpers::IdealHumanRandomIterator.new(1) }
136
+ assert_nil(ModSpox::Helpers::IdealHumanRandomIterator.new([]).next)
137
+ assert_equal(0, ModSpox::Helpers::IdealHumanRandomIterator.new([0]).next)
138
+ end
139
+ end