2011-04-16 21:02:13 +00:00
|
|
|
require 'spec_helper'
|
2011-05-02 13:34:33 +00:00
|
|
|
require 'guard/guard'
|
2011-04-10 23:08:43 +00:00
|
|
|
|
|
|
|
describe Guard::Hook do
|
2011-04-16 21:02:13 +00:00
|
|
|
|
2011-04-18 00:06:45 +00:00
|
|
|
class Guard::Dummy < Guard::Guard; end
|
2011-04-10 23:08:43 +00:00
|
|
|
|
|
|
|
let(:guard_class) { ::Guard::Dummy }
|
|
|
|
let(:listener) { double('listener').as_null_object }
|
|
|
|
|
2011-09-22 22:22:44 +00:00
|
|
|
after { described_class.reset_callbacks! }
|
2011-04-18 00:06:45 +00:00
|
|
|
|
2011-09-22 22:22:44 +00:00
|
|
|
describe "--module methods--" do
|
|
|
|
before { described_class.add_callback(listener, guard_class, :start_begin) }
|
2011-04-10 23:08:43 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
describe ".add_callback" do
|
2011-04-10 23:08:43 +00:00
|
|
|
it "can add a single callback" do
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.has_callback?(listener, guard_class, :start_begin).should be_true
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can add multiple callbacks" do
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.add_callback(listener, guard_class, [:event1, :event2])
|
|
|
|
described_class.has_callback?(listener, guard_class, :event1).should be_true
|
|
|
|
described_class.has_callback?(listener, guard_class, :event2).should be_true
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
describe ".notify" do
|
2011-04-10 23:08:43 +00:00
|
|
|
it "sends :call to the given Guard class's callbacks" do
|
2011-04-18 00:06:45 +00:00
|
|
|
listener.should_receive(:call).with(guard_class, :start_begin, "args")
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.notify(guard_class, :start_begin, "args")
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
it "runs only the given callbacks" do
|
2011-04-10 23:08:43 +00:00
|
|
|
listener2 = double('listener2')
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.add_callback(listener2, guard_class, :start_end)
|
2011-04-10 23:08:43 +00:00
|
|
|
listener2.should_not_receive(:call).with(guard_class, :start_end)
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.notify(guard_class, :start_begin)
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
it "runs callbacks only for the guard given" do
|
2011-04-10 23:08:43 +00:00
|
|
|
guard2_class = double('Guard::Dummy2').class
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.add_callback(listener, guard2_class, :start_begin)
|
2011-04-10 23:08:43 +00:00
|
|
|
listener.should_not_receive(:call).with(guard2_class, :start_begin)
|
2011-09-22 22:22:44 +00:00
|
|
|
described_class.notify(guard_class, :start_begin)
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
describe "#hook" do
|
2011-04-18 00:06:45 +00:00
|
|
|
before(:all) do
|
|
|
|
guard_class.class_eval do
|
|
|
|
def start
|
|
|
|
hook "my_hook"
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_all
|
|
|
|
hook :begin
|
|
|
|
hook :end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
hook :begin, 'args'
|
|
|
|
hook 'special_sauce', 'first_arg', 'second_arg'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@guard = guard_class.new
|
|
|
|
end
|
|
|
|
|
2011-04-10 23:08:43 +00:00
|
|
|
it "calls Guard::Hook.notify" do
|
2011-04-16 21:02:13 +00:00
|
|
|
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_begin)
|
|
|
|
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_end)
|
2011-04-18 00:06:45 +00:00
|
|
|
@guard.run_all
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "if passed a string parameter, will use that for the hook name" do
|
2011-04-18 00:06:45 +00:00
|
|
|
Guard::Hook.should_receive(:notify).with(guard_class, :my_hook)
|
|
|
|
@guard.start
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts extra args" do
|
|
|
|
Guard::Hook.should_receive(:notify).with(guard_class, :stop_begin, 'args')
|
|
|
|
Guard::Hook.should_receive(:notify).with(guard_class, :special_sauce, 'first_arg', 'second_arg')
|
|
|
|
@guard.stop
|
|
|
|
end
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|
2011-09-04 16:44:42 +00:00
|
|
|
|
2011-04-10 23:08:43 +00:00
|
|
|
end
|