2011-11-12 08:03:02 -05:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
describe Pry::Hooks do
|
|
|
|
before do
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks = Pry::Hooks.new
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "adding a new hook" do
|
|
|
|
it 'should not execute hook while adding it' do
|
2011-11-23 00:51:54 -05:00
|
|
|
run = false
|
|
|
|
@hooks.add_hook(:test_hook, :my_name) { run = true }
|
|
|
|
run.should == false
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
2011-11-23 00:51:54 -05:00
|
|
|
it 'should not allow adding of a hook with a duplicate name' do
|
|
|
|
@hooks.add_hook(:test_hook, :my_name) {}
|
|
|
|
|
|
|
|
lambda { @hooks.add_hook(:test_hook, :my_name) {} }.should.raise ArgumentError
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a new hook with a block' do
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name) { }
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.hook_count(:test_hook).should == 1
|
|
|
|
end
|
|
|
|
|
2011-11-23 00:51:54 -05:00
|
|
|
it 'should create a new hook with a callable' do
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name, proc { })
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.hook_count(:test_hook).should == 1
|
|
|
|
end
|
|
|
|
|
2011-11-23 00:51:54 -05:00
|
|
|
it 'should use block if given both block and callable' do
|
2011-11-18 20:54:15 -05:00
|
|
|
run = false
|
|
|
|
foo = false
|
|
|
|
@hooks.add_hook(:test_hook, :my_name, proc { foo = true }) { run = true }
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.hook_count(:test_hook).should == 1
|
2011-11-18 20:54:15 -05:00
|
|
|
@hooks.exec_hook(:test_hook)
|
|
|
|
run.should == true
|
|
|
|
foo.should == false
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise if not given a block or any other object' do
|
2011-11-15 08:49:11 -05:00
|
|
|
lambda { @hooks.add_hook(:test_hook, :my_name) }.should.raise ArgumentError
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
2011-11-18 20:54:15 -05:00
|
|
|
it 'should create multiple hooks for an event' do
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name) {}
|
|
|
|
@hooks.add_hook(:test_hook, :my_name2) {}
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.hook_count(:test_hook).should == 2
|
|
|
|
end
|
2011-11-23 00:51:54 -05:00
|
|
|
|
|
|
|
it 'should return a count of 0 for an empty hook' do
|
|
|
|
@hooks.hook_count(:test_hook).should == 0
|
|
|
|
end
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
2011-11-23 00:51:54 -05:00
|
|
|
describe "getting hooks" do
|
|
|
|
describe "get_hook" do
|
|
|
|
it 'should return the correct requested hook' do
|
|
|
|
run = false
|
|
|
|
fun = false
|
|
|
|
@hooks.add_hook(:test_hook, :my_name) { run = true }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name2) { fun = true }
|
|
|
|
@hooks.get_hook(:test_hook, :my_name).call
|
|
|
|
run.should == true
|
|
|
|
fun.should == false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return nil if hook does not exist' do
|
|
|
|
@hooks.get_hook(:test_hook, :my_name).should == nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "get_hooks" do
|
|
|
|
it 'should return a hash of hook names/hook functions for an event' do
|
|
|
|
hook1 = proc { 1 }
|
|
|
|
hook2 = proc { 2 }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name1, hook1)
|
|
|
|
@hooks.add_hook(:test_hook, :my_name2, hook2)
|
|
|
|
hash = @hooks.get_hooks(:test_hook)
|
|
|
|
hash.size.should == 2
|
|
|
|
hash[:my_name1].should == hook1
|
|
|
|
hash[:my_name2].should == hook2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return an empty hash if no hooks defined' do
|
|
|
|
@hooks.get_hooks(:test_hook).should == {}
|
|
|
|
end
|
2011-11-17 05:20:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "clearing all hooks for an event" do
|
|
|
|
it 'should clear all hooks' do
|
|
|
|
@hooks.add_hook(:test_hook, :my_name) { }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name2) { }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name3) { }
|
|
|
|
@hooks.clear(:test_hook)
|
|
|
|
@hooks.hook_count(:test_hook).should == 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:49:11 -05:00
|
|
|
describe "deleting a hook" do
|
2011-11-18 20:54:15 -05:00
|
|
|
it 'should successfully delete a hook' do
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name) {}
|
|
|
|
@hooks.delete_hook(:test_hook, :my_name)
|
|
|
|
@hooks.hook_count(:test_hook).should == 0
|
|
|
|
end
|
|
|
|
|
2011-11-18 20:54:15 -05:00
|
|
|
it 'should return the deleted hook' do
|
2011-11-17 05:20:07 -05:00
|
|
|
run = false
|
|
|
|
@hooks.add_hook(:test_hook, :my_name) { run = true }
|
|
|
|
@hooks.delete_hook(:test_hook, :my_name).call
|
|
|
|
run.should == true
|
2011-11-15 08:49:11 -05:00
|
|
|
end
|
2011-11-23 00:51:54 -05:00
|
|
|
|
|
|
|
it 'should return nil if hook does not exist' do
|
|
|
|
@hooks.delete_hook(:test_hook, :my_name).should == nil
|
|
|
|
end
|
2011-11-15 08:49:11 -05:00
|
|
|
end
|
|
|
|
|
2011-11-12 08:03:02 -05:00
|
|
|
describe "executing a hook" do
|
|
|
|
it 'should execute block hook' do
|
2011-11-18 20:54:15 -05:00
|
|
|
run = false
|
|
|
|
@hooks.add_hook(:test_hook, :my_name) { run = true }
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.exec_hook(:test_hook)
|
2011-11-18 20:54:15 -05:00
|
|
|
run.should == true
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should execute proc hook' do
|
2011-11-18 20:54:15 -05:00
|
|
|
run = false
|
|
|
|
@hooks.add_hook(:test_hook, :my_name, proc { run = true })
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.exec_hook(:test_hook)
|
2011-11-18 20:54:15 -05:00
|
|
|
run.should == true
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should execute a general callable hook' do
|
|
|
|
callable = Object.new.tap do |obj|
|
|
|
|
obj.instance_variable_set(:@test_var, nil)
|
|
|
|
class << obj
|
|
|
|
attr_accessor :test_var
|
|
|
|
def call() @test_var = true; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name, callable)
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.exec_hook(:test_hook)
|
|
|
|
callable.test_var.should == true
|
|
|
|
end
|
|
|
|
|
2011-11-18 20:54:15 -05:00
|
|
|
it 'should execute all hooks for an event if more than one is defined' do
|
2011-11-12 08:03:02 -05:00
|
|
|
x = nil
|
|
|
|
y = nil
|
2011-11-23 00:51:54 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name1) { y = true }
|
2011-11-15 08:49:11 -05:00
|
|
|
@hooks.add_hook(:test_hook, :my_name2) { x = true }
|
2011-11-12 08:03:02 -05:00
|
|
|
@hooks.exec_hook(:test_hook)
|
|
|
|
x.should == true
|
|
|
|
y.should == true
|
|
|
|
end
|
2011-11-23 00:51:54 -05:00
|
|
|
|
|
|
|
it 'should execute hooks in order' do
|
|
|
|
array = []
|
|
|
|
@hooks.add_hook(:test_hook, :my_name1) { array << 1 }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name2) { array << 2 }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name3) { array << 3 }
|
|
|
|
@hooks.exec_hook(:test_hook)
|
|
|
|
array.should == [1, 2, 3]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'return value of exec_hook should be that of last executed hook' do
|
|
|
|
@hooks.add_hook(:test_hook, :my_name1) { 1 }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name2) { 2 }
|
|
|
|
@hooks.add_hook(:test_hook, :my_name3) { 3 }
|
|
|
|
@hooks.exec_hook(:test_hook).should == 3
|
|
|
|
end
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
end
|