twibot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,148 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
2
+
3
+ class TestHandler < Test::Unit::TestCase
4
+ test "pattern writer should abort on empty values" do
5
+ handler = Twibot::Handler.new
6
+
7
+ handler.pattern = nil
8
+ assert_nil handler.instance_eval { @options[:pattern] }
9
+ assert_nil handler.instance_eval { @options[:tokens] }
10
+
11
+ handler.pattern = ""
12
+ assert_nil handler.instance_eval { @options[:pattern] }
13
+ assert_nil handler.instance_eval { @options[:tokens] }
14
+ end
15
+
16
+ test "pattern writer should turn regular pattern into regex" do
17
+ handler = Twibot::Handler.new
18
+ handler.pattern = "command"
19
+
20
+ assert_equal(/command(\s.+)?/, handler.instance_eval { @options[:pattern] })
21
+ assert_equal 0, handler.instance_eval { @options[:tokens] }.length
22
+ end
23
+
24
+ test "pattern writer should convert single named switch to regex" do
25
+ handler = Twibot::Handler.new
26
+ handler.pattern = ":command"
27
+
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
32
+
33
+ test "pattern writer should convert several named switches to regexen" do
34
+ handler = Twibot::Handler.new
35
+ handler.pattern = ":command fixed_word :subcommand"
36
+
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
42
+
43
+ test "pattern writer should convert several named switches to regexen specified by options" do
44
+ handler = Twibot::Handler.new(":time :hour", :hour => /\d\d/)
45
+
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] }
50
+ end
51
+
52
+ test "should recognize empty pattern" do
53
+ handler = Twibot::Handler.new
54
+ message = message "cjno", "A twitter direct message"
55
+
56
+ assert handler.recognize?(message)
57
+ end
58
+
59
+ test "should recognize empty pattern and allowed user" do
60
+ handler = Twibot::Handler.new "", :from => "cjno"
61
+ message = message "cjno", "A twitter direct message"
62
+ assert handler.recognize?(message)
63
+
64
+ handler = Twibot::Handler.new "", :from => ["cjno", "irbno"]
65
+ assert handler.recognize?(message)
66
+ end
67
+
68
+ test "should not recognize empty pattern and disallowed user" do
69
+ handler = Twibot::Handler.new "", :from => "irbno"
70
+ message = message "cjno", "A twitter direct message"
71
+ assert !handler.recognize?(message)
72
+
73
+ handler = Twibot::Handler.new "", :from => ["irbno", "satan"]
74
+ assert !handler.recognize?(message)
75
+ end
76
+
77
+ test "should recognize fixed pattern and no user" do
78
+ handler = Twibot::Handler.new "time"
79
+ message = message "cjno", "time oslo norway"
80
+ assert handler.recognize?(message)
81
+ end
82
+
83
+ test "should recognize dynamic pattern and no user" do
84
+ handler = Twibot::Handler.new "time :city :country"
85
+ message = message "cjno", "time oslo norway"
86
+ assert handler.recognize?(message)
87
+ end
88
+
89
+ test "should not recognize dynamic pattern and no user" do
90
+ handler = Twibot::Handler.new "time :city :country"
91
+ message = message "cjno", "oslo norway what is the time?"
92
+ assert !handler.recognize?(message)
93
+ end
94
+
95
+ test "should recognize fixed pattern and user" do
96
+ handler = Twibot::Handler.new "time", :from => ["cjno", "irbno"]
97
+ message = message "cjno", "time oslo norway"
98
+ assert handler.recognize?(message)
99
+ end
100
+
101
+ test "should recognize dynamic pattern and user" do
102
+ handler = Twibot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
103
+ message = message "cjno", "time oslo norway"
104
+ assert handler.recognize?(message)
105
+ end
106
+
107
+ test "should not recognize dynamic pattern and user" do
108
+ handler = Twibot::Handler.new "time :city :country", :from => ["cjno", "irbno"]
109
+ message = message "dude", "time oslo norway"
110
+ assert !handler.recognize?(message)
111
+ end
112
+
113
+ test "should recognize symbol users" do
114
+ handler = Twibot::Handler.new "time :city :country", :from => [:cjno, :irbno]
115
+ message = message "dude", "time oslo norway"
116
+ assert !handler.recognize?(message)
117
+
118
+ message = message("cjno", "time oslo norway")
119
+ assert handler.recognize?(message)
120
+ end
121
+
122
+ test "should accept options as only argument" do
123
+ handler = Twibot::Handler.new :from => :cjno
124
+ assert_equal(:cjno, handler.instance_eval { @options[:from] })
125
+ assert_nil handler.instance_eval { @options[:pattern] }
126
+ end
127
+
128
+ test "should provide parameters in params hash" do
129
+ handler = Twibot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
130
+ assert_equal "oslo", params[:city]
131
+ assert_equal "norway", params[:country]
132
+ end
133
+
134
+ message = message "cjno", "time oslo norway"
135
+ assert handler.recognize?(message)
136
+ handler.dispatch(message)
137
+ end
138
+
139
+ test "handle should call constructor block" do
140
+ handler = Twibot::Handler.new("time :city :country", :from => ["cjno", "irbno"]) do |message, params|
141
+ raise "Boom!"
142
+ end
143
+
144
+ assert_raise(RuntimeError) do
145
+ handler.handle(nil, nil)
146
+ end
147
+ end
148
+ end
data/test/test_hash.rb ADDED
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
2
+
3
+ class TestHash < Test::Unit::TestCase
4
+ test "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
+ test "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
+ test "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,43 @@
1
+ require 'test/unit'
2
+ require 'context'
3
+ require 'mocha'
4
+ require File.join(File.dirname(__FILE__), '../lib/twibot')
5
+
6
+ module Test::Unit::Assertions
7
+ def assert_hashes_equal(expected, actual, message = nil)
8
+ full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
9
+ <?> expected but was
10
+ <?>.
11
+ EOT
12
+ assert_block(full_message) do
13
+ break false if expected.keys.length != actual.keys.length
14
+ expected.keys.all? { |k| expected[k] == actual[k] }
15
+ end
16
+ end
17
+
18
+ def assert_hashes_not_equal(expected, actual, message = nil)
19
+ full_message = build_message(message, <<EOT, expected.inspect, actual.inspect)
20
+ <?> expected but was
21
+ <?>.
22
+ EOT
23
+ assert_block(full_message) do
24
+ break false if expected.keys.length != actual.keys.length
25
+ expected.keys.any? { |k| expected[k] != actual[k] }
26
+ end
27
+ end
28
+ end
29
+
30
+ def message(from, text)
31
+ Twitter::Message.new(:id => 1,
32
+ :sender => Twitter::User.new(:screen_name => from),
33
+ :text => text,
34
+ :recipient => "twibot",
35
+ :created_at => Time.now)
36
+ end
37
+
38
+ def tweet(from, text)
39
+ Twitter::Status.new(:id => 1,
40
+ :text => text,
41
+ :user => Twitter::User.new(:screen_name => from),
42
+ :created_at => Time.now)
43
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) unless defined?(Twibot)
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twibot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-15 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: twitter4r
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: bones
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.4.0
34
+ version:
35
+ description: Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra.
36
+ email: christian@cjohansen.no
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - History.txt
43
+ - Readme.rdoc
44
+ files:
45
+ - History.txt
46
+ - Rakefile
47
+ - Readme.rdoc
48
+ - lib/hash.rb
49
+ - lib/twibot.rb
50
+ - lib/twibot/bot.rb
51
+ - lib/twibot/config.rb
52
+ - lib/twibot/handlers.rb
53
+ - lib/twibot/macros.rb
54
+ - lib/twibot/tweets.rb
55
+ - tasks/ann.rake
56
+ - tasks/bones.rake
57
+ - tasks/gem.rake
58
+ - tasks/git.rake
59
+ - tasks/notes.rake
60
+ - tasks/post_load.rake
61
+ - tasks/rdoc.rake
62
+ - tasks/rubyforge.rake
63
+ - tasks/setup.rb
64
+ - tasks/spec.rake
65
+ - tasks/svn.rake
66
+ - tasks/test.rake
67
+ - test/test_bot.rb
68
+ - test/test_config.rb
69
+ - test/test_handler.rb
70
+ - test/test_hash.rb
71
+ - test/test_helper.rb
72
+ - test/test_twibot.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/cjohansen/twibot/
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - Readme.rdoc
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ requirements: []
94
+
95
+ rubyforge_project: twibot
96
+ rubygems_version: 1.3.1
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: Twibot (pronounced like "Abbot"), is a Ruby microframework for creating Twitter bots, heavily inspired by Sinatra
100
+ test_files:
101
+ - test/test_helper.rb
102
+ - test/test_config.rb
103
+ - test/test_hash.rb
104
+ - test/test_twibot.rb
105
+ - test/test_bot.rb
106
+ - test/test_handler.rb