2014-03-14 00:31:24 -04:00
|
|
|
require_relative 'helper'
|
2011-11-12 08:03:02 -05:00
|
|
|
|
|
|
|
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 }
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(run).to eq 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) {}
|
|
|
|
|
2015-06-20 19:56:04 -04:00
|
|
|
expect { @hooks.add_hook(:test_hook, :my_name) {} }.to raise_error 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) { }
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 1
|
2011-11-12 08:03:02 -05:00
|
|
|
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 { })
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 1
|
2011-11-12 08:03:02 -05:00
|
|
|
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 }
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 1
|
2011-11-18 20:54:15 -05:00
|
|
|
@hooks.exec_hook(:test_hook)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(run).to eq true
|
|
|
|
expect(foo).to eq false
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise if not given a block or any other object' do
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
expect { @hooks.add_hook(:test_hook, :my_name) }.to raise_error 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) {}
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 2
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
2011-11-23 00:51:54 -05:00
|
|
|
|
|
|
|
it 'should return a count of 0 for an empty hook' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 0
|
2011-11-23 00:51:54 -05:00
|
|
|
end
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|
|
|
|
|
2012-01-11 22:54:48 -05:00
|
|
|
describe "Pry::Hooks#merge" do
|
|
|
|
describe "merge!" do
|
|
|
|
it 'should merge in the Pry::Hooks' do
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
|
|
|
|
h2 = Pry::Hooks.new
|
|
|
|
|
|
|
|
h2.merge!(h1)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h2.get_hook(:test_hook, :testing)).to eq h1.get_hook(:test_hook, :testing)
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not share merged elements with original' do
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
|
|
|
|
h2 = Pry::Hooks.new
|
|
|
|
|
|
|
|
h2.merge!(h1)
|
|
|
|
h2.add_hook(:test_hook, :testing2) {}
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h2.get_hook(:test_hook, :testing2)).not_to eq h1.get_hook(:test_hook, :testing2)
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should NOT overwrite hooks belonging to shared event in receiver' do
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
|
|
|
|
callable = proc {}
|
|
|
|
h2 = Pry::Hooks.new.add_hook(:test_hook, :testing2, callable)
|
|
|
|
|
|
|
|
h2.merge!(h1)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h2.get_hook(:test_hook, :testing2)).to eq callable
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should overwrite identical hook in receiver' do
|
|
|
|
callable1 = proc { :one }
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing, callable1)
|
|
|
|
callable2 = proc { :two }
|
|
|
|
h2 = Pry::Hooks.new.add_hook(:test_hook, :testing, callable2)
|
|
|
|
|
|
|
|
h2.merge!(h1)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h2.get_hook(:test_hook, :testing)).to eq callable1
|
|
|
|
expect(h2.hook_count(:test_hook)).to eq 1
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should preserve hook order' do
|
|
|
|
name = ""
|
|
|
|
h1 = Pry::Hooks.new
|
|
|
|
h1.add_hook(:test_hook, :testing3) { name << "h" }
|
|
|
|
h1.add_hook(:test_hook, :testing4) { name << "n" }
|
|
|
|
|
|
|
|
h2 = Pry::Hooks.new
|
|
|
|
h2.add_hook(:test_hook, :testing1) { name << "j" }
|
|
|
|
h2.add_hook(:test_hook, :testing2) { name << "o" }
|
|
|
|
|
|
|
|
h2.merge!(h1)
|
|
|
|
h2.exec_hook(:test_hook)
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(name).to eq "john"
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "merge" do
|
|
|
|
it 'should return a fresh, independent instance' do
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
|
|
|
|
h2 = Pry::Hooks.new
|
|
|
|
|
|
|
|
h3 = h2.merge(h1)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h3).not_to eq h1
|
|
|
|
expect(h3).not_to eq h2
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should contain hooks from original instance' do
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
|
|
|
|
h2 = Pry::Hooks.new.add_hook(:test_hook2, :testing) {}
|
|
|
|
|
|
|
|
h3 = h2.merge(h1)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h3.get_hook(:test_hook, :testing)).to eq h1.get_hook(:test_hook, :testing)
|
|
|
|
expect(h3.get_hook(:test_hook2, :testing)).to eq h2.get_hook(:test_hook2, :testing)
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not affect original instances when new hooks are added' do
|
|
|
|
h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
|
|
|
|
h2 = Pry::Hooks.new.add_hook(:test_hook2, :testing) {}
|
|
|
|
|
|
|
|
h3 = h2.merge(h1)
|
|
|
|
h3.add_hook(:test_hook3, :testing) {}
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(h1.get_hook(:test_hook3, :testing)).to eq nil
|
|
|
|
expect(h2.get_hook(:test_hook3, :testing)).to eq nil
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "dupping a Pry::Hooks instance" do
|
|
|
|
it 'should share hooks with original' do
|
|
|
|
@hooks.add_hook(:test_hook, :testing) do
|
|
|
|
:none_such
|
|
|
|
end
|
|
|
|
|
|
|
|
hooks_dup = @hooks.dup
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(hooks_dup.get_hook(:test_hook, :testing)).to eq @hooks.get_hook(:test_hook, :testing)
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
2012-01-24 03:44:21 -05:00
|
|
|
it 'adding a new event to dupped instance should not affect original' do
|
2012-01-11 22:54:48 -05:00
|
|
|
@hooks.add_hook(:test_hook, :testing) { :none_such }
|
|
|
|
hooks_dup = @hooks.dup
|
|
|
|
|
|
|
|
hooks_dup.add_hook(:other_test_hook, :testing) { :okay_man }
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(hooks_dup.get_hook(:other_test_hook, :testing)).not_to eq @hooks.get_hook(:other_test_hook, :testing)
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
2012-01-24 03:44:21 -05:00
|
|
|
it 'adding a new hook to dupped instance should not affect original' do
|
2012-01-11 22:54:48 -05:00
|
|
|
@hooks.add_hook(:test_hook, :testing) { :none_such }
|
|
|
|
hooks_dup = @hooks.dup
|
|
|
|
|
|
|
|
hooks_dup.add_hook(:test_hook, :testing2) { :okay_man }
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(hooks_dup.get_hook(:test_hook, :testing2)).not_to eq @hooks.get_hook(:test_hook, :testing2)
|
2012-01-11 22:54:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(run).to eq true
|
|
|
|
expect(fun).to eq false
|
2011-11-23 00:51:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return nil if hook does not exist' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.get_hook(:test_hook, :my_name)).to eq nil
|
2011-11-23 00:51:54 -05:00
|
|
|
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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(hash.size).to eq 2
|
|
|
|
expect(hash[:my_name1]).to eq hook1
|
|
|
|
expect(hash[:my_name2]).to eq hook2
|
2011-11-23 00:51:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return an empty hash if no hooks defined' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.get_hooks(:test_hook)).to eq({})
|
2011-11-23 00:51:54 -05:00
|
|
|
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) { }
|
2015-03-14 04:08:16 -04:00
|
|
|
@hooks.clear_event_hooks(:test_hook)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 0
|
2011-11-17 05:20:07 -05:00
|
|
|
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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 0
|
2011-11-15 08:49:11 -05:00
|
|
|
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
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(run).to eq 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
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.delete_hook(:test_hook, :my_name)).to eq nil
|
2011-11-23 00:51:54 -05:00
|
|
|
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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(run).to eq 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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(run).to eq 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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(callable.test_var).to eq true
|
2011-11-12 08:03:02 -05:00
|
|
|
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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(x).to eq true
|
|
|
|
expect(y).to eq true
|
2011-11-12 08:03:02 -05:00
|
|
|
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)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(array).to eq [1, 2, 3]
|
2011-11-23 00:51:54 -05:00
|
|
|
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 }
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.exec_hook(:test_hook)).to eq 3
|
2011-11-23 00:51:54 -05:00
|
|
|
end
|
2012-01-14 03:33:36 -05:00
|
|
|
|
|
|
|
it 'should add exceptions to the errors array' do
|
|
|
|
@hooks.add_hook(:test_hook, :foo1) { raise 'one' }
|
|
|
|
@hooks.add_hook(:test_hook, :foo2) { raise 'two' }
|
|
|
|
@hooks.add_hook(:test_hook, :foo3) { raise 'three' }
|
|
|
|
@hooks.exec_hook(:test_hook)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.errors.map(&:message)).to eq ['one', 'two', 'three']
|
2012-01-14 03:33:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the last exception raised as the return value' do
|
|
|
|
@hooks.add_hook(:test_hook, :foo1) { raise 'one' }
|
|
|
|
@hooks.add_hook(:test_hook, :foo2) { raise 'two' }
|
|
|
|
@hooks.add_hook(:test_hook, :foo3) { raise 'three' }
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.exec_hook(:test_hook)).to eq @hooks.errors.last
|
2012-01-14 03:33:36 -05:00
|
|
|
end
|
2012-01-03 01:45:26 -05:00
|
|
|
end
|
2012-01-02 14:36:39 -05:00
|
|
|
|
2012-01-03 01:45:26 -05:00
|
|
|
describe "integration tests" do
|
|
|
|
describe "when_started hook" do
|
|
|
|
it 'should yield options to the hook' do
|
|
|
|
options = nil
|
2012-01-19 23:39:25 -05:00
|
|
|
Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _| options = opt }
|
2012-01-02 14:36:39 -05:00
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
redirect_pry_io(StringIO.new("exit"), StringIO.new) do
|
2012-01-03 01:45:26 -05:00
|
|
|
Pry.start binding, :hello => :baby
|
|
|
|
end
|
2012-07-12 07:13:13 -04:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(options[:hello]).to eq :baby
|
2012-01-02 14:36:39 -05:00
|
|
|
|
2012-01-03 01:45:26 -05:00
|
|
|
Pry.config.hooks.delete_hook(:when_started, :test_hook)
|
|
|
|
end
|
2012-01-17 23:20:13 -05:00
|
|
|
|
2012-01-19 23:39:25 -05:00
|
|
|
describe "target" do
|
2012-01-17 23:20:13 -05:00
|
|
|
|
2012-01-19 23:39:25 -05:00
|
|
|
it 'should yield the target, as a binding ' do
|
|
|
|
b = nil
|
|
|
|
Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _| b = target }
|
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
redirect_pry_io(StringIO.new("exit"), StringIO.new) do
|
2012-01-19 23:39:25 -05:00
|
|
|
Pry.start 5, :hello => :baby
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(b.is_a?(Binding)).to eq true
|
2012-01-19 23:39:25 -05:00
|
|
|
Pry.config.hooks.delete_hook(:when_started, :test_hook)
|
2012-01-17 23:20:13 -05:00
|
|
|
end
|
|
|
|
|
2012-01-19 23:39:25 -05:00
|
|
|
it 'should yield the target to the hook' do
|
|
|
|
b = nil
|
|
|
|
Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _| b = target }
|
2012-01-17 23:20:13 -05:00
|
|
|
|
2015-01-22 16:52:20 -05:00
|
|
|
redirect_pry_io(StringIO.new("exit"), StringIO.new) do
|
2012-01-19 23:39:25 -05:00
|
|
|
Pry.start 5, :hello => :baby
|
|
|
|
end
|
2012-01-17 23:20:13 -05:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(b.eval('self')).to eq 5
|
2012-01-19 23:39:25 -05:00
|
|
|
Pry.config.hooks.delete_hook(:when_started, :test_hook)
|
2012-01-17 23:20:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-19 23:39:25 -05:00
|
|
|
it 'should allow overriding of target (and binding_stack)' do
|
2012-01-17 23:20:13 -05:00
|
|
|
o = Object.new
|
|
|
|
class << o; attr_accessor :value; end
|
2012-01-19 23:39:25 -05:00
|
|
|
|
|
|
|
Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _pry_| _pry_.binding_stack = [Pry.binding_for(o)] }
|
2012-01-17 23:20:13 -05:00
|
|
|
|
|
|
|
redirect_pry_io(InputTester.new("@value = true","exit-all")) do
|
|
|
|
Pry.start binding, :hello => :baby
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.value).to eq true
|
2012-01-17 23:20:13 -05:00
|
|
|
Pry.config.hooks.delete_hook(:when_started, :test_hook)
|
|
|
|
end
|
2012-01-19 23:39:25 -05:00
|
|
|
|
2012-01-02 14:36:39 -05:00
|
|
|
end
|
2012-01-13 08:22:30 -05:00
|
|
|
|
|
|
|
describe "after_session hook" do
|
|
|
|
it 'should always run, even if uncaught exception bubbles out of repl' do
|
2014-12-09 22:44:02 -05:00
|
|
|
o = Pry::Config.new
|
2012-01-13 08:22:30 -05:00
|
|
|
o.great_escape = Class.new(StandardError)
|
|
|
|
|
2012-01-13 08:29:00 -05:00
|
|
|
old_ew = Pry.config.exception_whitelist
|
2012-01-13 08:22:30 -05:00
|
|
|
Pry.config.exception_whitelist << o.great_escape
|
|
|
|
|
|
|
|
array = [1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
begin
|
2015-01-22 16:52:20 -05:00
|
|
|
redirect_pry_io(StringIO.new("raise great_escape"), StringIO.new) do
|
2012-01-13 08:22:30 -05:00
|
|
|
Pry.start o, :hooks => Pry::Hooks.new.add_hook(:after_session, :cleanup) { array = nil }
|
|
|
|
end
|
|
|
|
rescue => ex
|
|
|
|
exception = ex
|
|
|
|
end
|
|
|
|
|
|
|
|
# ensure that an exception really was raised and it broke out
|
|
|
|
# of the repl
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(exception.is_a?(o.great_escape)).to eq true
|
2012-01-13 08:22:30 -05:00
|
|
|
|
|
|
|
# check that after_session hook ran
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(array).to eq nil
|
2012-01-13 08:29:00 -05:00
|
|
|
|
|
|
|
# cleanup after test
|
|
|
|
Pry.config.exception_whitelist = old_ew
|
2012-01-13 08:22:30 -05:00
|
|
|
end
|
2012-01-14 03:33:36 -05:00
|
|
|
|
2012-06-23 09:48:11 -04:00
|
|
|
describe "before_eval hook" do
|
|
|
|
describe "modifying input code" do
|
|
|
|
it 'should replace input code with code determined by hook' do
|
|
|
|
hooks = Pry::Hooks.new.add_hook(:before_eval, :quirk) { |code, pry| code.replace(":little_duck") }
|
|
|
|
redirect_pry_io(InputTester.new(":jemima", "exit-all"), out = StringIO.new) do
|
|
|
|
Pry.start(self, :hooks => hooks)
|
|
|
|
end
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(out.string).to match(/little_duck/)
|
|
|
|
expect(out.string).not_to match(/jemima/)
|
2012-06-23 09:48:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not interfere with command processing when replacing input code' do
|
|
|
|
commands = Pry::CommandSet.new do
|
|
|
|
import_from Pry::Commands, "exit-all"
|
|
|
|
|
|
|
|
command "how-do-you-like-your-blue-eyed-boy-now-mister-death" do
|
|
|
|
output.puts "in hours of bitterness i imagine balls of sapphire, of metal"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
hooks = Pry::Hooks.new.add_hook(:before_eval, :quirk) { |code, pry| code.replace(":little_duck") }
|
2015-03-14 05:48:09 -04:00
|
|
|
|
2012-06-23 09:48:11 -04:00
|
|
|
redirect_pry_io(InputTester.new("how-do-you-like-your-blue-eyed-boy-now-mister-death", "exit-all"), out = StringIO.new) do
|
|
|
|
Pry.start(self, :hooks => hooks, :commands => commands)
|
|
|
|
end
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(out.string).to match(/in hours of bitterness i imagine balls of sapphire, of metal/)
|
|
|
|
expect(out.string).not_to match(/little_duck/)
|
2012-06-23 09:48:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-01-14 03:33:36 -05:00
|
|
|
describe "exceptions" do
|
|
|
|
before do
|
|
|
|
Pry.config.hooks.add_hook(:after_eval, :baddums){ raise "Baddums" }
|
|
|
|
Pry.config.hooks.add_hook(:after_eval, :simbads){ raise "Simbads" }
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Pry.config.hooks.delete_hook(:after_eval, :baddums)
|
|
|
|
Pry.config.hooks.delete_hook(:after_eval, :simbads)
|
|
|
|
end
|
|
|
|
it "should not raise exceptions" do
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
expect { mock_pry("1", "2", "3") }.to_not raise_error
|
2012-01-14 03:33:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should print out a notice for each exception raised" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("1")).to match(/after_eval hook failed: RuntimeError: Baddums\n.*after_eval hook failed: RuntimeError: Simbads/m)
|
2012-01-14 03:33:36 -05:00
|
|
|
end
|
|
|
|
end
|
2012-01-13 08:22:30 -05:00
|
|
|
end
|
2012-01-23 07:10:51 -05:00
|
|
|
end
|
|
|
|
|
2012-01-24 03:44:21 -05:00
|
|
|
describe "anonymous hooks" do
|
|
|
|
it 'should allow adding of hook without a name' do
|
|
|
|
@hooks.add_hook(:test_hook, nil) {}
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 1
|
2012-01-24 03:44:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should only allow one anonymous hook to exist' do
|
|
|
|
@hooks.add_hook(:test_hook, nil) { }
|
|
|
|
@hooks.add_hook(:test_hook, nil) { }
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@hooks.hook_count(:test_hook)).to eq 1
|
2012-01-24 03:44:21 -05:00
|
|
|
end
|
2012-01-23 07:10:51 -05:00
|
|
|
|
2012-01-24 03:44:21 -05:00
|
|
|
it 'should execute most recently added anonymous hook' do
|
|
|
|
x = nil
|
|
|
|
y = nil
|
|
|
|
@hooks.add_hook(:test_hook, nil) { y = 1 }
|
|
|
|
@hooks.add_hook(:test_hook, nil) { x = 2 }
|
|
|
|
@hooks.exec_hook(:test_hook)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(y).to eq nil
|
|
|
|
expect(x).to eq 2
|
2012-01-24 03:44:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-12 08:03:02 -05:00
|
|
|
end
|