2012-03-03 06:51:11 -05:00
|
|
|
describe "Sticky locals (_file_ and friends)" do
|
|
|
|
it 'locals should all exist upon initialization' 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 { pry_eval '_file_', '_dir_', '_ex_', '_pry_', '_' }.to_not raise_error
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'locals should still exist after cd-ing into a new context' 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 { pry_eval 'cd 0', '_file_', '_dir_', '_ex_', '_pry_', '_' }.to_not raise_error
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
|
2012-09-09 01:29:00 -04:00
|
|
|
it 'locals should keep value after cd-ing (_pry_)' do
|
|
|
|
pry_tester.tap do |t|
|
|
|
|
pry = t.eval '_pry_'
|
|
|
|
t.eval 'cd 0'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval('_pry_')).to eq(pry)
|
2012-09-09 01:29:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-09 22:39:01 -04:00
|
|
|
describe '_ex_' do
|
|
|
|
it 'returns the last exception without wrapping it in a LastException' do
|
|
|
|
ReplTester.start do
|
|
|
|
input 'raise "halp"'
|
|
|
|
|
|
|
|
input '_ex_.message == "halp"'
|
|
|
|
output '=> true'
|
|
|
|
|
2014-03-09 22:41:57 -04:00
|
|
|
input 'Kernel.instance_method(:class).bind(_ex_).call'
|
|
|
|
output '=> RuntimeError'
|
2014-03-09 22:39:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'keeps its value after cd-ing' do
|
|
|
|
ReplTester.start do
|
|
|
|
input 'error blah'
|
|
|
|
input '$x = _ex_'
|
|
|
|
input 'cd 0'
|
|
|
|
|
|
|
|
input '_ex_ == $x'
|
|
|
|
output '=> true'
|
|
|
|
end
|
|
|
|
end
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'locals should keep value after cd-ing (_file_ and _dir_)' do
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.command "file-and-dir-test" do
|
2012-03-03 06:51:11 -05:00
|
|
|
set_file_and_dir_locals("/blah/ostrich.rb")
|
|
|
|
end
|
|
|
|
|
2019-02-24 17:11:05 -05:00
|
|
|
expect(pry_eval('file-and-dir-test', 'cd 0', '_file_'))
|
2019-03-02 07:01:01 -05:00
|
|
|
.to match(%r{/blah/ostrich\.rb})
|
2012-09-09 01:29:00 -04:00
|
|
|
|
2019-02-24 17:11:05 -05:00
|
|
|
expect(pry_eval('file-and-dir-test', 'cd 0', '_dir_'))
|
2019-03-02 07:01:01 -05:00
|
|
|
.to match(%r{/blah})
|
2012-09-09 01:29:00 -04:00
|
|
|
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.commands.delete "file-and-dir-test"
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
|
2014-02-03 08:51:27 -05:00
|
|
|
it 'locals should return last result (_)' do
|
|
|
|
pry_tester.tap do |t|
|
2015-01-23 08:01:32 -05:00
|
|
|
lam = t.eval 'lambda { |_foo| }'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval('_')).to eq(lam)
|
2014-02-03 08:51:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'locals should return second last result (__)' do
|
|
|
|
pry_tester.tap do |t|
|
2015-01-23 08:01:32 -05:00
|
|
|
lam = t.eval 'lambda { |_foo| }'
|
2014-02-03 08:51:27 -05:00
|
|
|
t.eval 'num = 1'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval('__')).to eq(lam)
|
2014-02-03 08:51:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-13 22:33:51 -04:00
|
|
|
describe "User defined sticky locals" do
|
|
|
|
describe "setting as Pry.config option" do
|
|
|
|
it 'should define a new sticky local for the session (normal value)' do
|
|
|
|
Pry.config.extra_sticky_locals[:test_local] = :john
|
|
|
|
o = Object.new
|
|
|
|
redirect_pry_io(InputTester.new("@value = test_local",
|
|
|
|
"exit-all")) do
|
|
|
|
Pry.start(o)
|
|
|
|
end
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.instance_variable_get(:@value)).to eq :john
|
2012-03-13 22:33:51 -04:00
|
|
|
Pry.config.extra_sticky_locals = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a new sticky local for the session (proc)' do
|
|
|
|
Pry.config.extra_sticky_locals[:test_local] = proc { :john }
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
redirect_pry_io(InputTester.new("@value = test_local",
|
|
|
|
"exit-all")) do
|
|
|
|
Pry.start(o)
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.instance_variable_get(:@value)).to eq :john
|
2012-03-13 22:33:51 -04:00
|
|
|
Pry.config.extra_sticky_locals = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "passing in as hash option when creating pry instance" do
|
|
|
|
it 'should define a new sticky local for the session (normal value)' do
|
|
|
|
o = Object.new
|
|
|
|
redirect_pry_io(InputTester.new("@value = test_local",
|
|
|
|
"exit-all")) do
|
2019-02-25 17:47:48 -05:00
|
|
|
Pry.start(o, extra_sticky_locals: { test_local: :john })
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.instance_variable_get(:@value)).to eq :john
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define multiple sticky locals' do
|
|
|
|
o = Object.new
|
2019-02-24 13:20:22 -05:00
|
|
|
redirect_pry_io(
|
|
|
|
InputTester.new(
|
|
|
|
"@value1 = test_local1",
|
|
|
|
"@value2 = test_local2",
|
|
|
|
"exit-all"
|
|
|
|
)
|
|
|
|
) do
|
|
|
|
Pry.start(
|
|
|
|
o,
|
|
|
|
extra_sticky_locals: {
|
2019-02-25 17:42:17 -05:00
|
|
|
test_local1: :john,
|
2019-02-24 13:20:22 -05:00
|
|
|
test_local2: :carl
|
|
|
|
}
|
|
|
|
)
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.instance_variable_get(:@value1)).to eq :john
|
|
|
|
expect(o.instance_variable_get(:@value2)).to eq :carl
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a new sticky local for the session (as Proc)' do
|
|
|
|
o = Object.new
|
|
|
|
redirect_pry_io(InputTester.new("@value = test_local",
|
|
|
|
"exit-all")) do
|
2019-02-25 17:47:48 -05:00
|
|
|
Pry.start(o, extra_sticky_locals: { test_local: proc { :john } })
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.instance_variable_get(:@value)).to eq :john
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "hash option value should override config value" do
|
|
|
|
it 'should define a new sticky local for the session (normal value)' do
|
|
|
|
Pry.config.extra_sticky_locals[:test_local] = :john
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
redirect_pry_io(InputTester.new("@value = test_local",
|
|
|
|
"exit-all")) do
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start(o, extra_sticky_locals: { test_local: :carl })
|
2012-03-13 22:33:51 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(o.instance_variable_get(:@value)).to eq :carl
|
2012-03-13 22:33:51 -04:00
|
|
|
Pry.config.extra_sticky_locals = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-03 06:51:11 -05:00
|
|
|
it 'should create a new sticky local' do
|
2012-12-20 04:28:04 -05:00
|
|
|
t = pry_tester
|
|
|
|
t.eval "_pry_.add_sticky_local(:test_local){ :test_value }"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval("test_local")).to eq(:test_value)
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should still exist after cd-ing into new binding' do
|
2012-12-20 04:28:04 -05:00
|
|
|
t = pry_tester
|
|
|
|
t.eval "_pry_.add_sticky_local(:test_local){ :test_value }"
|
|
|
|
t.eval "cd Object.new"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(t.eval("test_local")).to eq(:test_value)
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should provide different values for successive block invocations' do
|
2012-07-23 04:03:05 -04:00
|
|
|
pry = Pry.new
|
2012-12-16 20:12:03 -05:00
|
|
|
pry.push_binding binding
|
2012-07-23 04:03:05 -04:00
|
|
|
pry.add_sticky_local(:test_local) { rand }
|
|
|
|
value1 = pry.evaluate_ruby 'test_local'
|
|
|
|
value2 = pry.evaluate_ruby 'test_local'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(value1).not_to eq(value2)
|
2012-03-03 06:51:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|