forked from joshwand/campfire-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathour_quotes_spec.rb
186 lines (165 loc) · 4.96 KB
/
our_quotes_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'spec'
BOT_ROOT = File.join(File.dirname(__FILE__), '..')
BOT_ENVIRONMENT = 'test'
require File.join(File.dirname(__FILE__), '../lib/bot.rb')
bot = CampfireBot::Bot.instance
require "#{BOT_ROOT}/plugins/our_quotes.rb"
describe "OurQuotes plugin" do
it "should remember added quotes" do
when_saying "!addquote Someone: Hello world."
bot_should_reply "Added quote #1."
quotes[0].should == "Someone: Hello world."
end
it "should complain when !addquote is given without a quote" do
when_saying "!addquote"
bot_should_reply "Please include the text of the quote and attribution."
quotes.size.should == 0
end
it "should regurgitate random quotes" do
with_quote_log {
add "Someone: Hello world."
}
when_saying "!quote"
# How to show attribution, index, room, and timestamp?
#bot_should_reply "Someone spake thusly: Hello world"
# "Foo quoth thither
bot_should_reply "#1 Someone: Hello world."
end
it "should find matching quotes" do
with_quote_log {
add "Anyone: Hello world."
add "Someone: Hello world."
del 1
add "Someone: It's too cold."
}
when_saying "!quote o w"
bot_should_reply "#2 Someone: Hello world. (1 match)"
end
it "should use proper grammer when showing number of matches" do
with_quote_log {
add "Anyone: hi!"
add "Anyone: bye."
}
when_saying "!quote Anyone"
bot_should_reply %r/ \(2 matches\)$/
end
it "should retrieve quotes by ID when the query is numeric" do
with_quote_log {
add "Anyone: 2+2"
add "Anyone: hi!"
}
when_saying "!quote 2"
bot_should_reply "#2 Anyone: hi!"
end
it "should display an explanation when !quote is given an invalid ID" do
when_saying "!quote 1"
bot_should_reply "No quote #1."
end
# Implementation idea: save array of matches in a hash
# keyed by query and perhaps user. Should expire after
# a few minutes, so don't bother persisting it even to tmp/.
it "should iterate through results on repeat command" do
pending
with_quote_log {
add "Someone: Foo"
add "Someone: Bar"
add "Someone: Baz"
}
when_saying "!quote Someone"
bot_should_reply %r/#\d Someone: ... \(3 matches\)$/
when_saying "!quote Someone"
bot_should_reply %r/#\d Someone: ... \(2 of 3 matches\)$/
when_saying "!quote Someone"
bot_should_reply %r/#\d Someone: ... \(3 of 3 matches\)$/
# Or maybe (3 matches, none left) ?
end
it "should remove last quote upon request" do
with_quote_log {
add "Hello world."
}
when_saying "!rmquote 1"
bot_should_reply "Deleted quote #1."
@quote.quotes.size.should == 0
end
it "should free trailing IDs when removing quotes" do
with_quote_log {
add "One"
add "Two"
add "Three"
add "Four"
del 3
del 4
add "III"
}
quotes[3 - 1].should == "III"
end
it "should preserve IDs of quotes after the removed one" do
with_quote_log {
add "One"
add "Two"
add "Three"
del 2
}
quotes[3 - 1].should == "Three"
end
it "should display an explanation when !rmquote is given an invalid ID" do
when_saying "!rmquote 1"
bot_should_reply "No quote #1."
end
it "should save quotes between runs" do
with_quote_log {
add "One"
add "Two"
}
init_plugin # Should re-read the .yml file
quotes[2 - 1].should == "Two"
end
it "should handle angle brackets appropriately" do
# One convention for attribution is to place the speaker in brackets.
# And this is how Campfire+Tinder+CampfireBot send us the message.
when_saying "!addquote \\u0026lt;Someone\\u0026gt; Hello"
bot_should_reply "Added quote #1."
quotes[1 - 1].should == "<Someone> Hello"
end
before :each do
@bot = CampfireBot::Bot.instance
@bot.stub!(:config).and_return('nickname' => 'Bot',
'our_quotes_recall_command' => 'quote'
#,'our_quotes_debug_enabled' => true
)
init_plugin
end
after :each do
if File.exist? @quote.data_file
File.unlink(@quote.data_file)
end
end
def init_plugin
@quote = OurQuotes.new
CampfireBot::Plugin.registered_plugins['OurQuotes'] = @quote
end
def when_saying(msg)
@message = CampfireBot::Message.new(:room => Tinder::Room.new(42, "Room1"),
:message => msg,
:person => 'Josh')
end
def bot_should_reply(msg)
@message.should_receive(:speak).with(msg)
@bot.send(:handle_message, @message)
end
# Group log setup calls into a block
def with_quote_log
yield
end
# Add a quote to the log
def add(quote)
@quote.append_add("Someoneelse", "Room1", quote)
end
# Remove a quote from the log by id
def del(quote_id)
@quote.append_del("Someoneelse", "Room1", quote_id)
end
def quotes
@quote.quotes
end
end