jabbot 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Jabbot
2
+ VERSION = '0.2.0'
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'fileutils'
5
+
6
+ dir = File.dirname(File.expand_path(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(dir, '..', 'lib'))
8
+ $LOAD_PATH.unshift(dir)
9
+
10
+ require 'jabbot'
11
+
12
+ module Test::Unit::Assertions
13
+ def assert_hashes_equal(expected, actual, message = nil)
14
+ full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
15
+ <?> expected but was
16
+ <?>.
17
+ EOT
18
+ assert_block(full_message) do
19
+ break false if expected.keys.length != actual.keys.length
20
+ expected.keys.all? { |k| expected[k] == actual[k] }
21
+ end
22
+ end
23
+
24
+ def assert_hashes_not_equal(expected, actual, message = nil)
25
+ full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
26
+ <?> expected but was
27
+ <?>.
28
+ EOT
29
+ assert_block(full_message) do
30
+ break false if expected.keys.length != actual.keys.length
31
+ expected.keys.any? { |k| expected[k] != actual[k] }
32
+ end
33
+ end
34
+ end
35
+
36
+ TEST_DIR = File.join(File.dirname(__FILE__), *%w[.])
37
+
38
+ def testpath(path)
39
+ File.join(TEST_DIR, path)
40
+ end
41
+
42
+ # test/spec/mini 3
43
+ # http://gist.github.com/25455
44
+ # chris@ozmm.org
45
+ # file:lib/test/spec/mini.rb
46
+ def context(*args, &block)
47
+ return super unless (name = args.first) && block
48
+ require 'test/unit'
49
+ klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
50
+ def self.test(name, &block)
51
+ define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
52
+ end
53
+ def self.xtest(*args) end
54
+ def self.setup(&block) define_method(:setup, &block) end
55
+ def self.teardown(&block) define_method(:teardown, &block) end
56
+ end
57
+ (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
58
+ klass.class_eval &block
59
+ end
60
+
61
+ Message = Struct.new(:user, :text, :time)
62
+ def mock_message(user, text)
63
+ Message.new(user, text, Time.now)
64
+ end
@@ -1,20 +1,20 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Jabbot)
1
+ require 'helper'
2
2
  require 'fileutils'
3
3
 
4
- class TestBot < Test::Unit::TestCase
5
- should "not raise errors when initialized" do
4
+ context "Bot" do
5
+ test "raise no exceptions when initialized" do
6
6
  assert_nothing_raised do
7
7
  Jabbot::Bot.new Jabbot::Config.new
8
8
  end
9
9
  end
10
10
 
11
- should "raise errors when initialized without config file" do
11
+ test "raise exception when initialized without config file" do
12
12
  assert_raise SystemExit do
13
13
  Jabbot::Bot.new
14
14
  end
15
15
  end
16
16
 
17
- should "not raise error on initialize when config file exists" do
17
+ test "raise no exception on initialize when config file exists" do
18
18
  if File.exists?("config")
19
19
  FileUtils.rm("config/bot.yml")
20
20
  else
@@ -30,17 +30,17 @@ class TestBot < Test::Unit::TestCase
30
30
  FileUtils.rm_rf("config")
31
31
  end
32
32
 
33
- should "provide configuration settings as methods" do
33
+ test "provide configuration settings as methods" do
34
34
  bot = Jabbot::Bot.new Jabbot::Config.new(:login => "jabbot")
35
35
  assert_equal "jabbot", bot.login
36
36
  end
37
37
 
38
- should "return logger instance" do
38
+ test "return logger instance" do
39
39
  bot = Jabbot::Bot.new(Jabbot::Config.default << Jabbot::Config.new)
40
40
  assert bot.log.is_a?(Logger)
41
41
  end
42
42
 
43
- should "respect configured log level" do
43
+ test "respect configured log level" do
44
44
  bot = Jabbot::Bot.new(Jabbot::Config.new(:log_level => "info"))
45
45
  assert_equal Logger::INFO, bot.log.level
46
46
 
@@ -49,38 +49,8 @@ class TestBot < Test::Unit::TestCase
49
49
  end
50
50
  end
51
51
 
52
- class TestBotMacros < Test::Unit::TestCase
53
- should "provide configure macro" do
54
- assert respond_to?(:configure)
55
- end
56
-
57
- should "yield configuration" do
58
- Jabbot::Macros.bot = Jabbot::Bot.new Jabbot::Config.default
59
-
60
- conf = nil
61
- assert_nothing_raised { configure { |c| conf = c } }
62
- assert conf.is_a?(Jabbot::Config)
63
- end
64
-
65
- should "add handler" do
66
- Jabbot::Macros.bot = Jabbot::Bot.new Jabbot::Config.default
67
-
68
- handler = add_handler(:message, ":command", :from => :cjno)
69
- assert handler.is_a?(Jabbot::Handler), handler.class
70
- end
71
-
72
- should "provide client macro" do
73
- assert respond_to?(:client)
74
- end
75
-
76
- should "provide user macro" do
77
- assert respond_to?(:user)
78
- end
79
- end
80
-
81
- class TestBotHandlers < Test::Unit::TestCase
82
-
83
- should "include handlers" do
52
+ context "Handler DSL" do
53
+ test "include handlers" do
84
54
  bot = Jabbot::Bot.new(Jabbot::Config.new)
85
55
 
86
56
  assert_not_nil bot.handlers
@@ -91,7 +61,7 @@ class TestBotHandlers < Test::Unit::TestCase
91
61
  assert_not_nil bot.handlers[:subject]
92
62
  end
93
63
 
94
- should "add handler" do
64
+ test "add handler" do
95
65
  bot = Jabbot::Bot.new(Jabbot::Config.new)
96
66
  bot.add_handler :message, Jabbot::Handler.new
97
67
  assert_equal 1, bot.handlers[:message].length
@@ -1,44 +1,44 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Jabbot)
1
+ require 'helper'
2
2
  require 'stringio'
3
3
 
4
- class TestConfig < Test::Unit::TestCase
5
- should "default configuration be a hash" do
4
+ context "Config" do
5
+ test "default configuration be a hash" do
6
6
  assert_not_nil Jabbot::Config::DEFAULT
7
7
  assert Jabbot::Config::DEFAULT.is_a?(Hash)
8
8
  end
9
9
 
10
- should "initialize with no options" do
10
+ test "initialize with no options" do
11
11
  assert_hashes_equal({}, Jabbot::Config.new.settings)
12
12
  end
13
13
 
14
- should "return config from add" do
14
+ test "return config from add" do
15
15
  config = Jabbot::Config.new
16
16
  assert_equal config, config.add(Jabbot::Config.new)
17
17
  end
18
18
 
19
- should "alias add to <<" do
19
+ test "alias add to <<" do
20
20
  config = Jabbot::Config.new
21
21
  assert config.respond_to?(:<<)
22
22
  assert config << Jabbot::Config.new
23
23
  end
24
24
 
25
- should "mirror method_missing as config getters" do
25
+ test "mirror method_missing as config getters" do
26
26
  config = Jabbot::Config.default << Jabbot::Config.new
27
27
  assert_equal Jabbot::Config::DEFAULT[:password], config.password
28
28
  assert_equal Jabbot::Config::DEFAULT[:login], config.login
29
29
  end
30
30
 
31
- should "mirror missing methods as config setters" do
31
+ test "mirror missing methods as config setters" do
32
32
  config = Jabbot::Config.default << Jabbot::Config.new
33
33
  assert_equal Jabbot::Config::DEFAULT[:login], config.login
34
34
 
35
35
  val = "jabbot"
36
- config.login = val+'!'
36
+ config.login = val+'!'
37
37
  assert_not_equal Jabbot::Config::DEFAULT[:login], config.login
38
38
  assert_equal val+'!', config.login
39
39
  end
40
40
 
41
- should "not override default hash" do
41
+ test "not override default hash" do
42
42
  config = Jabbot::Config.default
43
43
  hash = Jabbot::Config::DEFAULT
44
44
 
@@ -49,7 +49,7 @@ class TestConfig < Test::Unit::TestCase
49
49
  assert_hashes_equal hash, Jabbot::Config::DEFAULT
50
50
  end
51
51
 
52
- should "return merged configuration from to_hash" do
52
+ test "return merged configuration from to_hash" do
53
53
  config = Jabbot::Config.new
54
54
  config.login = "jabbot"
55
55
  config.password = "secret"
@@ -64,16 +64,16 @@ class TestConfig < Test::Unit::TestCase
64
64
  end
65
65
  end
66
66
 
67
- class TestFileConfig < Test::Unit::TestCase
68
- should "subclass config for file config" do
67
+ context "FileConfig" do
68
+ test "subclass config for file config" do
69
69
  assert Jabbot::FileConfig.new(StringIO.new).is_a?(Jabbot::Config)
70
70
  end
71
71
 
72
- should "read settings from stream" do
72
+ test "read settings from stream" do
73
73
  config = Jabbot::FileConfig.new(StringIO.new <<-YAML)
74
- login: jabbot
75
- password: secret
76
- YAML
74
+ login: jabbot
75
+ password: secret
76
+ YAML
77
77
 
78
78
  assert_equal "jabbot", config.login
79
79
  assert_equal "secret", config.password
@@ -1,156 +1,154 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Jabbot)
1
+ require 'helper'
2
2
 
3
- class TestHandler < Test::Unit::TestCase
4
- context "pattern writer" do
5
- should "abort on empty values" do
6
- handler = Jabbot::Handler.new
3
+ context "Handler" do
4
+ test "abort on empty values" do
5
+ handler = Jabbot::Handler.new
7
6
 
8
- handler.pattern = nil
9
- assert_nil handler.instance_eval { @options[:pattern] }
10
- assert_nil handler.instance_eval { @options[:tokens] }
7
+ handler.pattern = nil
8
+ assert_nil handler.instance_eval { @options[:pattern] }
9
+ assert_nil handler.instance_eval { @options[:tokens] }
11
10
 
12
- handler.pattern = ""
13
- assert_nil handler.instance_eval { @options[:pattern] }
14
- assert_nil handler.instance_eval { @options[:tokens] }
15
- end
11
+ handler.pattern = ""
12
+ assert_nil handler.instance_eval { @options[:pattern] }
13
+ assert_nil handler.instance_eval { @options[:tokens] }
14
+ end
16
15
 
17
- should "turn regular pattern into regex" do
18
- handler = Jabbot::Handler.new
19
- handler.pattern = "command"
16
+ test "turn regular pattern into regex" do
17
+ handler = Jabbot::Handler.new
18
+ handler.pattern = "command"
20
19
 
21
- assert_equal(/command(\s.+)?/, handler.instance_eval { @options[:pattern] })
22
- assert_equal 0, handler.instance_eval { @options[:tokens] }.length
23
- end
20
+ assert_equal(/command(\s.+)?/, handler.instance_eval { @options[:pattern] })
21
+ assert_equal 0, handler.instance_eval { @options[:tokens] }.length
22
+ end
24
23
 
25
- should "convert single named switch to regex" do
26
- handler = Jabbot::Handler.new
27
- handler.pattern = ":command"
24
+ test "convert single named switch to regex" do
25
+ handler = Jabbot::Handler.new
26
+ handler.pattern = ":command"
28
27
 
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
28
+ assert_equal(/([^\s]+)(\s.+)?/, handler.instance_eval { @options[:pattern] })
29
+ assert_equal 1, handler.instance_eval { @options[:tokens] }.length
30
+ assert_equal :command, handler.instance_eval { @options[:tokens].first }
31
+ end
33
32
 
34
- should "convert several named switches to regexen" do
35
- handler = Jabbot::Handler.new
36
- handler.pattern = ":command fixed_word :subcommand"
33
+ test "convert several named switches to regexen" do
34
+ handler = Jabbot::Handler.new
35
+ handler.pattern = ":command fixed_word :subcommand"
37
36
 
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
37
+ assert_equal(/([^\s]+) fixed_word ([^\s]+)(\s.+)?/, handler.instance_eval { @options[:pattern] })
38
+ assert_equal 2, handler.instance_eval { @options[:tokens] }.length
39
+ assert_equal :command, handler.instance_eval { @options[:tokens].first }
40
+ assert_equal :subcommand, handler.instance_eval { @options[:tokens][1] }
41
+ end
43
42
 
44
- should "convert several named switches to regexen specified by options" do
45
- handler = Jabbot::Handler.new(":time :hour", :hour => /\d\d/)
43
+ test "convert several named switches to regexen specified by options" do
44
+ handler = Jabbot::Handler.new(":time :hour", :hour => /\d\d/)
46
45
 
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
46
+ assert_equal(/([^\s]+) ((?-mix:\d\d))(\s.+)?/, handler.instance_eval { @options[:pattern] })
47
+ assert_equal 2, handler.instance_eval { @options[:tokens] }.length
48
+ assert_equal :time, handler.instance_eval { @options[:tokens].first }
49
+ assert_equal :hour, handler.instance_eval { @options[:tokens][1] }
52
50
  end
53
51
 
54
- should "recognize empty pattern" do
52
+ test "recognize empty pattern" do
55
53
  handler = Jabbot::Handler.new
56
- message = message "cjno", "A jabber message"
54
+ message = mock_message "cjno", "A jabber message"
57
55
 
58
56
  assert handler.recognize?(message)
59
57
  end
60
58
 
61
- should "recognize empty pattern and allowed user" do
59
+ test "recognize empty pattern and allowed user" do
62
60
  handler = Jabbot::Handler.new "", :from => "cjno"
63
- message = message "cjno", "A jabber message"
61
+ message = mock_message "cjno", "A jabber message"
64
62
  assert handler.recognize?(message)
65
63
 
66
64
  handler = Jabbot::Handler.new "", :from => ["cjno", "irbno"]
67
65
  assert handler.recognize?(message)
68
66
  end
69
67
 
70
- should "not recognize empty pattern and disallowed user" do
68
+ test "not recognize empty pattern and disallowed user" do
71
69
  handler = Jabbot::Handler.new "", :from => "irbno"
72
- message = message "cjno", "A jabber message"
70
+ message = mock_message "cjno", "A jabber message"
73
71
  assert !handler.recognize?(message)
74
72
 
75
73
  handler = Jabbot::Handler.new "", :from => ["irbno", "satan"]
76
74
  assert !handler.recognize?(message)
77
75
  end
78
76
 
79
- should "recognize fixed pattern and no user" do
77
+ test "recognize fixed pattern and no user" do
80
78
  handler = Jabbot::Handler.new "time"
81
- message = message "cjno", "time oslo norway"
79
+ message = mock_message "cjno", "time oslo norway"
82
80
  assert handler.recognize?(message)
83
81
  end
84
82
 
85
- should "recognize dynamic pattern and no user" do
83
+ test "recognize dynamic pattern and no user" do
86
84
  handler = Jabbot::Handler.new "time :city :country"
87
- message = message "cjno", "time oslo norway"
85
+ message = mock_message "cjno", "time oslo norway"
88
86
  assert handler.recognize?(message)
89
87
  end
90
88
 
91
- should "not recognize dynamic pattern and no user" do
89
+ test "not recognize dynamic pattern and no user" do
92
90
  handler = Jabbot::Handler.new "time :city :country"
93
- message = message "cjno", "oslo norway what is the time?"
91
+ message = mock_message "cjno", "oslo norway what is the time?"
94
92
  assert !handler.recognize?(message)
95
93
  end
96
94
 
97
- should "recognize fixed pattern and user" do
95
+ test "recognize fixed pattern and user" do
98
96
  handler = Jabbot::Handler.new "time", :from => ["cjno", "irbno"]
99
- message = message "cjno", "time oslo norway"
97
+ message = mock_message "cjno", "time oslo norway"
100
98
  assert handler.recognize?(message)
101
99
  end
102
100
 
103
- should "recognize dynamic pattern and user" do
101
+ test "recognize dynamic pattern and user" do
104
102
  handler = Jabbot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
105
- message = message "cjno", "time oslo norway"
103
+ message = mock_message "cjno", "time oslo norway"
106
104
  assert handler.recognize?(message)
107
105
  end
108
106
 
109
- should "not recognize dynamic pattern and user" do
107
+ test "not recognize dynamic pattern and user" do
110
108
  handler = Jabbot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
111
- message = message "dude", "time oslo norway"
109
+ message = mock_message "dude", "time oslo norway"
112
110
  assert !handler.recognize?(message)
113
111
  end
114
112
 
115
- should "recognize symbol users" do
113
+ test "recognize symbol users" do
116
114
  handler = Jabbot::Handler.new "time :city :country", :from => [:cjno, :irbno]
117
- message = message "dude", "time oslo norway"
115
+ message = mock_message "dude", "time oslo norway"
118
116
  assert !handler.recognize?(message)
119
117
 
120
- message = message("cjno", "time oslo norway")
118
+ message = mock_message("cjno", "time oslo norway")
121
119
  assert handler.recognize?(message)
122
120
  end
123
121
 
124
- should "recognize messages from allowed users" do
122
+ test "recognize messages from allowed users" do
125
123
  handler = Jabbot::Handler.new :from => [:cjno, :irbno]
126
- message = message "cjno", "time oslo norway"
124
+ message = mock_message "cjno", "time oslo norway"
127
125
  assert handler.recognize?(message)
128
126
  end
129
-
130
- should "not recognize messages from unallowed users with capital screen names" do
127
+
128
+ test "not recognize messages from unallowed users with capital screen names" do
131
129
  handler = Jabbot::Handler.new :from => [:cjno, :irbno]
132
- message = message "Cjno", "time oslo norway"
130
+ message = mock_message "Cjno", "time oslo norway"
133
131
  assert !handler.recognize?(message)
134
132
  end
135
133
 
136
- should "accept options as only argument" do
134
+ test "accept options as only argument" do
137
135
  handler = Jabbot::Handler.new :from => :cjno
138
136
  assert_equal(['cjno'], handler.instance_eval { @options[:from] })
139
137
  assert_nil handler.instance_eval { @options[:pattern] }
140
138
  end
141
139
 
142
- should "provide parameters in params hash" do
140
+ test "provide parameters in params hash" do
143
141
  handler = Jabbot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
144
142
  assert_equal "oslo", params[:city]
145
143
  assert_equal "norway", params[:country]
146
144
  end
147
145
 
148
- message = message "cjno", "time oslo norway"
146
+ message = mock_message "cjno", "time oslo norway"
149
147
  assert handler.recognize?(message)
150
148
  handler.dispatch(message)
151
149
  end
152
150
 
153
- should "call constructor block from handle" do
151
+ test "call constructor block from handle" do
154
152
  handler = Jabbot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
155
153
  raise "Boom!"
156
154
  end
@@ -160,31 +158,31 @@ class TestHandler < Test::Unit::TestCase
160
158
  end
161
159
  end
162
160
 
163
- should "recognize regular expressions" do
161
+ test "recognize regular expressions" do
164
162
  handler = Jabbot::Handler.new /(?:what|where) is (.*)/i
165
- message = message "dude", "Where is this shit?"
163
+ message = mock_message "dude", "Where is this shit?"
166
164
  assert handler.recognize?(message)
167
165
 
168
- message = message "dude", "How is this shit?"
166
+ message = mock_message "dude", "How is this shit?"
169
167
  assert !handler.recognize?(message)
170
168
  end
171
169
 
172
- should "recognize regular expressions from specific users" do
170
+ test "recognize regular expressions from specific users" do
173
171
  handler = Jabbot::Handler.new /(?:what|where) is (.*)/i, :from => "cjno"
174
- message = message "dude", "Where is this shit?"
172
+ message = mock_message "dude", "Where is this shit?"
175
173
  assert !handler.recognize?(message)
176
174
 
177
- message = message "cjno", "Where is this shit?"
175
+ message = mock_message "cjno", "Where is this shit?"
178
176
  assert handler.recognize?(message)
179
177
  end
180
178
 
181
- should "provide parameters as arrays when matching regular expressions" do
179
+ test "provide parameters as arrays when matching regular expressions" do
182
180
  handler = Jabbot::Handler.new(/time ([^\s]*) ([^\s]*)/) do |message, params|
183
181
  assert_equal "oslo", params[0]
184
182
  assert_equal "norway", params[1]
185
183
  end
186
184
 
187
- message = message "cjno", "time oslo norway"
185
+ message = mock_message "cjno", "time oslo norway"
188
186
  assert handler.recognize?(message)
189
187
  handler.dispatch(message)
190
188
  end