Skip to content

Commit

Permalink
Fix reaction matching behaviour
Browse files Browse the repository at this point in the history
Some unit tests were failing silently because all exceptions were
being rescued (fixed in ab0dcac).

Fix matching behaviour: invoke all matching reactions, or the
first defined default reaction, if no matches.
  • Loading branch information
pewniak747 committed Nov 19, 2017
1 parent ab0dcac commit 1313d44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lib/hipbot/matchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ def reactables
plugins.map(&:class)
end

def presence_reaction_sets
reactables.map(&:presence_reactions)
def reaction_sets
defined_reaction_sets + default_reaction_sets
end

def reaction_sets
reactables.each_with_object([]) do |reactable, array|
array.unshift(reactable.reactions)
array.push(reactable.default_reactions)
end
def defined_reaction_sets
reactables.map(&:reactions)
end

def default_reaction_sets
# Each default reaction is alone in its own reaction set
reactables.flat_map(&:default_reactions).map { |r| [r] }
end

def presence_reaction_sets
reactables.map(&:presence_reactions)
end

def set_matches sets, message
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
subject.react(sender, room, '@robot hello robot!')
end

it "should choose first option when multiple options match" do
it "should choose all matching options" do
described_class.on /hello there/ do reply('hello there') end
described_class.on /hello (.*)/ do reply('hello') end
subject.should_receive(:send_to_room).with(room, 'hello there')
subject.should_receive(:send_to_room).with(room, 'hello')
subject.react(sender, room, '@robot hello there')
end

Expand Down

0 comments on commit 1313d44

Please sign in to comment.