jabbot 0.1.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.
@@ -0,0 +1,191 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Jabbot)
2
+
3
+ class TestHandler < Test::Unit::TestCase
4
+ context "pattern writer" do
5
+ should "abort on empty values" do
6
+ handler = Jabbot::Handler.new
7
+
8
+ handler.pattern = nil
9
+ assert_nil handler.instance_eval { @options[:pattern] }
10
+ assert_nil handler.instance_eval { @options[:tokens] }
11
+
12
+ handler.pattern = ""
13
+ assert_nil handler.instance_eval { @options[:pattern] }
14
+ assert_nil handler.instance_eval { @options[:tokens] }
15
+ end
16
+
17
+ should "turn regular pattern into regex" do
18
+ handler = Jabbot::Handler.new
19
+ handler.pattern = "command"
20
+
21
+ assert_equal(/command(\s.+)?/, handler.instance_eval { @options[:pattern] })
22
+ assert_equal 0, handler.instance_eval { @options[:tokens] }.length
23
+ end
24
+
25
+ should "convert single named switch to regex" do
26
+ handler = Jabbot::Handler.new
27
+ handler.pattern = ":command"
28
+
29
+ assert_equal(/([^\s]+)(\s.+)?/, handler.instance_eval { @options[:pattern] })
30
+ assert_equal 1, handler.instance_eval { @options[:tokens] }.length
31
+ assert_equal :command, handler.instance_eval { @options[:tokens].first }
32
+ end
33
+
34
+ should "convert several named switches to regexen" do
35
+ handler = Jabbot::Handler.new
36
+ handler.pattern = ":command fixed_word :subcommand"
37
+
38
+ assert_equal(/([^\s]+) fixed_word ([^\s]+)(\s.+)?/, handler.instance_eval { @options[:pattern] })
39
+ assert_equal 2, handler.instance_eval { @options[:tokens] }.length
40
+ assert_equal :command, handler.instance_eval { @options[:tokens].first }
41
+ assert_equal :subcommand, handler.instance_eval { @options[:tokens][1] }
42
+ end
43
+
44
+ should "convert several named switches to regexen specified by options" do
45
+ handler = Jabbot::Handler.new(":time :hour", :hour => /\d\d/)
46
+
47
+ assert_equal(/([^\s]+) ((?-mix:\d\d))(\s.+)?/, handler.instance_eval { @options[:pattern] })
48
+ assert_equal 2, handler.instance_eval { @options[:tokens] }.length
49
+ assert_equal :time, handler.instance_eval { @options[:tokens].first }
50
+ assert_equal :hour, handler.instance_eval { @options[:tokens][1] }
51
+ end
52
+ end
53
+
54
+ should "recognize empty pattern" do
55
+ handler = Jabbot::Handler.new
56
+ message = message "cjno", "A jabber message"
57
+
58
+ assert handler.recognize?(message)
59
+ end
60
+
61
+ should "recognize empty pattern and allowed user" do
62
+ handler = Jabbot::Handler.new "", :from => "cjno"
63
+ message = message "cjno", "A jabber message"
64
+ assert handler.recognize?(message)
65
+
66
+ handler = Jabbot::Handler.new "", :from => ["cjno", "irbno"]
67
+ assert handler.recognize?(message)
68
+ end
69
+
70
+ should "not recognize empty pattern and disallowed user" do
71
+ handler = Jabbot::Handler.new "", :from => "irbno"
72
+ message = message "cjno", "A jabber message"
73
+ assert !handler.recognize?(message)
74
+
75
+ handler = Jabbot::Handler.new "", :from => ["irbno", "satan"]
76
+ assert !handler.recognize?(message)
77
+ end
78
+
79
+ should "recognize fixed pattern and no user" do
80
+ handler = Jabbot::Handler.new "time"
81
+ message = message "cjno", "time oslo norway"
82
+ assert handler.recognize?(message)
83
+ end
84
+
85
+ should "recognize dynamic pattern and no user" do
86
+ handler = Jabbot::Handler.new "time :city :country"
87
+ message = message "cjno", "time oslo norway"
88
+ assert handler.recognize?(message)
89
+ end
90
+
91
+ should "not recognize dynamic pattern and no user" do
92
+ handler = Jabbot::Handler.new "time :city :country"
93
+ message = message "cjno", "oslo norway what is the time?"
94
+ assert !handler.recognize?(message)
95
+ end
96
+
97
+ should "recognize fixed pattern and user" do
98
+ handler = Jabbot::Handler.new "time", :from => ["cjno", "irbno"]
99
+ message = message "cjno", "time oslo norway"
100
+ assert handler.recognize?(message)
101
+ end
102
+
103
+ should "recognize dynamic pattern and user" do
104
+ handler = Jabbot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
105
+ message = message "cjno", "time oslo norway"
106
+ assert handler.recognize?(message)
107
+ end
108
+
109
+ should "not recognize dynamic pattern and user" do
110
+ handler = Jabbot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
111
+ message = message "dude", "time oslo norway"
112
+ assert !handler.recognize?(message)
113
+ end
114
+
115
+ should "recognize symbol users" do
116
+ handler = Jabbot::Handler.new "time :city :country", :from => [:cjno, :irbno]
117
+ message = message "dude", "time oslo norway"
118
+ assert !handler.recognize?(message)
119
+
120
+ message = message("cjno", "time oslo norway")
121
+ assert handler.recognize?(message)
122
+ end
123
+
124
+ should "recognize messages from allowed users" do
125
+ handler = Jabbot::Handler.new :from => [:cjno, :irbno]
126
+ message = message "cjno", "time oslo norway"
127
+ assert handler.recognize?(message)
128
+ end
129
+
130
+ should "not recognize messages from unallowed users with capital screen names" do
131
+ handler = Jabbot::Handler.new :from => [:cjno, :irbno]
132
+ message = message "Cjno", "time oslo norway"
133
+ assert !handler.recognize?(message)
134
+ end
135
+
136
+ should "accept options as only argument" do
137
+ handler = Jabbot::Handler.new :from => :cjno
138
+ assert_equal(['cjno'], handler.instance_eval { @options[:from] })
139
+ assert_nil handler.instance_eval { @options[:pattern] }
140
+ end
141
+
142
+ should "provide parameters in params hash" do
143
+ handler = Jabbot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
144
+ assert_equal "oslo", params[:city]
145
+ assert_equal "norway", params[:country]
146
+ end
147
+
148
+ message = message "cjno", "time oslo norway"
149
+ assert handler.recognize?(message)
150
+ handler.dispatch(message)
151
+ end
152
+
153
+ should "call constructor block from handle" do
154
+ handler = Jabbot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
155
+ raise "Boom!"
156
+ end
157
+
158
+ assert_raise(RuntimeError) do
159
+ handler.handle(nil, nil)
160
+ end
161
+ end
162
+
163
+ should "recognize regular expressions" do
164
+ handler = Jabbot::Handler.new /(?:what|where) is (.*)/i
165
+ message = message "dude", "Where is this shit?"
166
+ assert handler.recognize?(message)
167
+
168
+ message = message "dude", "How is this shit?"
169
+ assert !handler.recognize?(message)
170
+ end
171
+
172
+ should "recognize regular expressions from specific users" do
173
+ handler = Jabbot::Handler.new /(?:what|where) is (.*)/i, :from => "cjno"
174
+ message = message "dude", "Where is this shit?"
175
+ assert !handler.recognize?(message)
176
+
177
+ message = message "cjno", "Where is this shit?"
178
+ assert handler.recognize?(message)
179
+ end
180
+
181
+ should "provide parameters as arrays when matching regular expressions" do
182
+ handler = Jabbot::Handler.new(/time ([^\s]*) ([^\s]*)/) do |message, params|
183
+ assert_equal "oslo", params[0]
184
+ assert_equal "norway", params[1]
185
+ end
186
+
187
+ message = message "cjno", "time oslo norway"
188
+ assert handler.recognize?(message)
189
+ handler.dispatch(message)
190
+ end
191
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Jabbot)
2
+
3
+ class TestHash < Test::Unit::TestCase
4
+ should "convert string keys to symbols" do
5
+ hash = { "one" => 1, "two" => 2 }
6
+ hash.symbolize_keys!
7
+
8
+ assert_equal 1, hash[:one]
9
+ assert_equal 2, hash[:two]
10
+ assert_nil hash["one"]
11
+ assert_nil hash["two"]
12
+ end
13
+
14
+ should "convert string keys and preserve symbol keys" do
15
+ hash = { "one" => 1, :two => 2 }
16
+ hash.symbolize_keys!
17
+
18
+ assert_equal 1, hash[:one]
19
+ assert_equal 2, hash[:two]
20
+ assert_nil hash["one"]
21
+ assert_nil hash["two"]
22
+ end
23
+
24
+ should "convert hashes recursively" do
25
+ hash = { "one" => 1, :two => { "three" => 3, "four" => 4 } }
26
+ hash.symbolize_keys!
27
+
28
+ assert_equal 1, hash[:one]
29
+ assert_equal 3, hash[:two][:three]
30
+ assert_equal 4, hash[:two][:four]
31
+ assert_nil hash["one"]
32
+ assert_nil hash["two"]
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ if RUBY_VERSION =~ /1\.9/
3
+ Gem.all_load_paths
4
+ gem "thoughtbot-shoulda"
5
+ gem "xmpp4r"
6
+ end
7
+ require 'test/unit'
8
+ require 'shoulda'
9
+ require File.join(File.dirname(__FILE__), '../lib/jabbot')
10
+
11
+ module Test::Unit::Assertions
12
+ def assert_hashes_equal(expected, actual, message = nil)
13
+ full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
14
+ <?> expected but was
15
+ <?>.
16
+ EOT
17
+ assert_block(full_message) do
18
+ break false if expected.keys.length != actual.keys.length
19
+ expected.keys.all? { |k| expected[k] == actual[k] }
20
+ end
21
+ end
22
+
23
+ def assert_hashes_not_equal(expected, actual, message = nil)
24
+ full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
25
+ <?> expected but was
26
+ <?>.
27
+ EOT
28
+ assert_block(full_message) do
29
+ break false if expected.keys.length != actual.keys.length
30
+ expected.keys.any? { |k| expected[k] != actual[k] }
31
+ end
32
+ end
33
+ end
34
+
35
+ Message = Struct.new(:user, :text, :time)
36
+ def message(user, text)
37
+ Message.new(user, text, Time.now)
38
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jabbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - BadBoy_
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-27 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: xmpp4r
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.4"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: thoughtbot-shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.10.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: jeweler
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.10.2
44
+ version:
45
+ description:
46
+ email: badboy@archlinux.us
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.rdoc
53
+ files:
54
+ - README.rdoc
55
+ - Rakefile
56
+ - VERSION.yml
57
+ - lib/hash.rb
58
+ - lib/jabbot.rb
59
+ - lib/jabbot/bot.rb
60
+ - lib/jabbot/config.rb
61
+ - lib/jabbot/handlers.rb
62
+ - lib/jabbot/macros.rb
63
+ - lib/jabbot/message.rb
64
+ - test/test_bot.rb
65
+ - test/test_config.rb
66
+ - test/test_handler.rb
67
+ - test/test_hash.rb
68
+ - test/test_helper.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/badboy/jabbot
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project: jabbot
93
+ rubygems_version: 1.3.2
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Simple framework for creating Jabber/MUC bots, inspired by Sinatra and Twibot
97
+ test_files:
98
+ - test/test_helper.rb
99
+ - test/test_bot.rb
100
+ - test/test_hash.rb
101
+ - test/test_config.rb
102
+ - test/test_handler.rb