cinch-test 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,3 +13,13 @@
13
13
  3. Make a message with `make_message(bot, 'message text')`
14
14
  4. Stub things out on the message as you like, then collect all the replies
15
15
  with `get_replies(message)`
16
+
17
+ ## Changelog
18
+ * 0.0.3
19
+ * [Bug Fix] Initializing plugins without a config hash works now.
20
+ * [Bug Fix] MockMessage objects now have a real Cinch::User / Cinch::Channel object
21
+ instead of strings.
22
+ * [Bug Fix] MockMessages with a channel set will also be processed as a :channel event.
23
+ * [Bug Fix] MockMessages of :private events will now also be processed as :message
24
+ * [Bug Fix] All MockMessages will now be processed in as a :catchall event type in
25
+ addition to other specified types.
@@ -5,7 +5,7 @@ require 'thread'
5
5
  module Cinch
6
6
  module Test
7
7
  class MockIRC < Cinch::IRC
8
- def initialize(*)
8
+ def initialize(*args)
9
9
  super
10
10
  # the #setup method adds the @network property among
11
11
  # other things
@@ -20,7 +20,7 @@ module Cinch
20
20
  end
21
21
 
22
22
  class MockBot < Cinch::Bot
23
- def initialize(*)
23
+ def initialize(*args)
24
24
  super
25
25
  @irc = MockIRC.new(self)
26
26
 
@@ -34,7 +34,7 @@ module Cinch
34
34
  end
35
35
 
36
36
  class MockMessage < Cinch::Message
37
- def initialize(msg, bot, opts={})
37
+ def initialize(msg, bot, opts = {})
38
38
  # override the message-parsing stuff
39
39
  super(nil, bot)
40
40
  @message = msg
@@ -45,6 +45,8 @@ module Cinch
45
45
  end
46
46
  end
47
47
 
48
+ Reply = Struct.new(:text, :event, :time)
49
+
48
50
  def make_bot(plugin, opts = {}, &b)
49
51
  MockBot.new do
50
52
  configure do |c|
@@ -60,11 +62,62 @@ module Cinch
60
62
  end
61
63
  end
62
64
 
63
- def make_message(bot, text, opts={})
65
+ def make_message(bot, text, opts = {})
64
66
  MockMessage.new(text, bot, opts)
65
67
  end
66
68
 
67
- def send_message(message, event = :message)
69
+ # Process message and return all replies.
70
+ # @parmam [Cinch::Test::MockMessage] message A MockMessage object.
71
+ # @param [Symbol] event The event type of the message.
72
+ def get_replies(message, event = :message)
73
+ mutex = Mutex.new
74
+ replies = []
75
+
76
+ # Catch all m.reply
77
+ (class << message; self; end).class_eval do
78
+ define_method :reply do |msg, prefix = false|
79
+ msg = [self.user.nick, msg].join(': ') if prefix
80
+ r = Reply.new(msg, :message, Time.now)
81
+ mutex.synchronize { replies << r }
82
+ end
83
+ end
84
+
85
+ # Catch all user.(msg|send|privmsg)
86
+ (class << message.user; self; end).class_eval do
87
+ [:send, :msg, :privmsg].each do |method|
88
+ define_method method do |msg, notice = false|
89
+ r = Reply.new(msg, (notice ? :notice : :private), Time.now)
90
+ mutex.synchronize { replies << r }
91
+ end
92
+ end
93
+ end
94
+
95
+ # Catch all channel.send and action
96
+ if message.channel
97
+ (class << message.channel; self; end).class_eval do
98
+ define_method :send do |msg, notice = false|
99
+ r = Reply.new(msg, :channel, Time.now)
100
+ mutex.synchronize { replies << r }
101
+ end
102
+
103
+ define_method :action do |msg, notice = false|
104
+ r = Reply.new(msg, :action, Time.now)
105
+ mutex.synchronize { replies << r }
106
+ end
107
+ end
108
+ end
109
+
110
+ process_message(message, event)
111
+
112
+ replies
113
+ end
114
+
115
+ private
116
+
117
+ # Process message by dispatching it to the handlers
118
+ # @param [Cinch::Test::MockMessage] message A MockMessage object.
119
+ # @param [Symbol] event The event type of the message.
120
+ def process_message(message, event)
68
121
  handlers = message.bot.handlers
69
122
 
70
123
  # Deal with secondary event types
@@ -86,22 +139,5 @@ module Cinch
86
139
  handler.thread_group.list.each(&:join)
87
140
  end
88
141
  end
89
-
90
- def get_replies(message, event=:message)
91
- mutex = Mutex.new
92
-
93
- replies = []
94
-
95
- (class << message; self; end).class_eval do
96
- define_method :reply do |r, prefix = false|
97
- r = [self.user.nick, r].join(': ') if prefix
98
- mutex.synchronize { replies << r }
99
- end
100
- end
101
-
102
- send_message(message, event)
103
-
104
- replies
105
- end
106
142
  end
107
143
  end
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Test
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -10,30 +10,45 @@ describe Cinch::Test do
10
10
  @foo = config[:foo]
11
11
  end
12
12
 
13
- match /foo/, :method => :foo
13
+ match /foo/, method: :foo
14
14
  def foo(m)
15
15
  m.reply "foo: #{@foo}"
16
16
  end
17
17
 
18
- match /bar/, :method => :bar
18
+ match /bar/, method: :bar
19
19
  def bar(m)
20
20
  m.reply 'bar reply'
21
21
  end
22
22
 
23
- match /baz/, :method => :baz
23
+ match /baz/, method: :baz
24
24
  def baz(m)
25
25
  m.reply 'baz reply', true
26
26
  end
27
27
 
28
- listen_to :channel
29
- def listen(m)
30
- m.reply 'I listen'
28
+ listen_to :channel, method: :chan_listen
29
+ def chan_listen(m)
30
+ m.reply 'I listen' if m.message == 'blah'
31
31
  end
32
32
 
33
- match /trout/, :method => :action, :react_on => :action
33
+ match /trout/, method: :action, react_on: :action
34
34
  def action(m)
35
35
  m.reply 'I hate fish'
36
36
  end
37
+
38
+ match /ping/, method: :ping
39
+ def ping(m)
40
+ m.channel.send 'Pong?'
41
+ end
42
+
43
+ match /dance/, method: :dance
44
+ def dance(m)
45
+ m.channel.action 'DANCES'
46
+ end
47
+
48
+ match /message/, method: :message
49
+ def message(m)
50
+ m.user.send 'a message!'
51
+ end
37
52
  end
38
53
 
39
54
  include Cinch::Test
@@ -52,33 +67,50 @@ describe Cinch::Test do
52
67
  it 'makes a bot with config values available to the plugin' do
53
68
  message = make_message(bot, '!foo')
54
69
  replies = get_replies(message)
55
- assert replies == ['foo: foo_value']
70
+ assert replies.first.text == 'foo: foo_value'
56
71
  end
57
72
 
58
- describe '#make_message' do
59
- let(:message) { make_message(bot, '!bar') }
73
+ describe 'message events' do
60
74
 
61
75
  it 'messages a test bot and gets a reply' do
76
+ message = make_message(bot, '!bar')
62
77
  replies = get_replies(message)
63
- assert replies == ['bar reply']
78
+ assert replies.first.text == 'bar reply'
64
79
  end
65
- end
66
80
 
67
- describe '#make_message with reply' do
68
81
  let(:message) { make_message(bot, '!baz') }
69
82
 
70
83
  it 'messages a test bot and gets a prefixed reply' do
71
84
  replies = get_replies(message)
72
- assert replies == ['test: baz reply']
85
+ assert replies.first.text == 'test: baz reply'
73
86
  end
74
87
  end
75
88
 
76
- describe 'listeners' do
77
- let(:message) { make_message(bot, 'blah blah blah', :channel => 'test') }
89
+ describe 'user events' do
90
+ it 'captures messages sent to users' do
91
+ r = get_replies(make_message(bot, '!message', :channel => '#test')).first
92
+ assert r.text == 'a message!'
93
+ assert r.event == :private
94
+ end
95
+ end
78
96
 
79
- it 'messages a test bot and gets a prefixed reply' do
97
+ describe 'channel events' do
98
+ it 'get triggered by channel events' do
99
+ message = make_message(bot, 'blah', :channel => 'test')
80
100
  replies = get_replies(message)
81
- assert replies == ['I listen']
101
+ assert replies.first.text == 'I listen'
102
+ end
103
+
104
+ it 'captures channel messages' do
105
+ r = get_replies(make_message(bot, '!ping', :channel => '#test')).first
106
+ assert r.text == 'Pong?'
107
+ assert r.event == :channel
108
+ end
109
+
110
+ it 'captures a channel action' do
111
+ r = get_replies(make_message(bot, '!dance', :channel => '#test')).first
112
+ assert r.text == 'DANCES'
113
+ assert r.event == :action
82
114
  end
83
115
  end
84
116
  end
@@ -1,5 +1,11 @@
1
1
  require 'coveralls'
2
- Coveralls.wear!
2
+ require 'simplecov'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start 'rails'
3
9
 
4
10
  require 'cinch/test'
5
11
  require 'minitest'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-29 00:00:00.000000000 Z
13
+ date: 2013-07-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake