summaryrefslogtreecommitdiff
path: root/spec/config_spec.rb
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2019-05-04 18:09:57 +0300
committerKyrylo Silin <silin@kyrylo.org>2019-05-04 23:09:10 +0300
commit1d04fbd646a21dec380410bab2e8af610c5b6a24 (patch)
tree4ec0a3d50c4cc14a528f28089f75d3351af8ba36 /spec/config_spec.rb
parent8f6c792ae39d130fe132ef1d50ac9dd4a8469a6f (diff)
downloadpry-control-d-ruby-3-friendly.tar.gz
control_d_handler: don't mutate eval_string within the handlercontrol-d-ruby-3-friendly
This is a preparational step for #1824 (Enabling `# frozen_string_literal: true` in `~/.pryc` crashes most operations) Alternative to https://github.com/pry/pry/pull/2030 (config: delete the `control_d_handler` option) We had to jump a few hoops to change how the handler works. The problem is that mutation is the default expected behaviour. Therefore, we had to change its API. There's no need to pass `eval_string` because `pry_instance` already has it as an attribute. `config.control_d_handler` is a proxy proc, to preserve backwards compatibility with users of old signature (one known user is Pry Byebug). The handler will emit a warning if the old signature is used.
Diffstat (limited to 'spec/config_spec.rb')
-rw-r--r--spec/config_spec.rb72
1 files changed, 71 insertions, 1 deletions
diff --git a/spec/config_spec.rb b/spec/config_spec.rb
index a3d38043..0c0e680e 100644
--- a/spec/config_spec.rb
+++ b/spec/config_spec.rb
@@ -28,7 +28,7 @@ RSpec.describe Pry::Config do
specify { expect(subject.should_load_requires).to be(true).or be(false) }
specify { expect(subject.should_load_plugins).to be(true).or be(false) }
specify { expect(subject.windows_console_warning).to be(true).or be(false) }
- specify { expect(subject.control_d_handler).to be_a(Method) }
+ specify { expect(subject.control_d_handler).to respond_to(:call) }
specify { expect(subject.memory_size).to be_a(Numeric) }
specify { expect(subject.extra_sticky_locals).to be_a(Hash) }
specify { expect(subject.command_completions).to be_a(Proc) }
@@ -171,4 +171,74 @@ RSpec.describe Pry::Config do
expect(subject[:foo]).to eq(1)
end
end
+
+ describe "#control_d_handler=" do
+ context "when the handler expects multiple arguments" do
+ it "prints a warning" do
+ expect(Pry::Warning).to receive(:warn).with(
+ "control_d_handler's arity of 2 parameters was deprecated. Now it " \
+ 'gets passed just 1 parameter (pry_instance). Please update your ' \
+ 'handler'
+ )
+ subject.control_d_handler = proc { |_arg1, _arg2| }
+ end
+ end
+
+ context "when the handler expects just one argument" do
+ it "doesn't print a warning" do
+ expect(Pry::Warning).not_to receive(:warn)
+ subject.control_d_handler = proc { |_arg1| }
+ end
+ end
+ end
+
+ describe "#control_d_handler" do
+ let(:pry_instance) { Pry.new }
+
+ context "when it returns a callable accepting one argument" do
+ context "and when it is called with one argument" do
+ it "calls the handler with a pry instance" do
+ subject.control_d_handler = proc do |arg|
+ expect(arg).to eql(pry_instance)
+ end
+ subject.control_d_handler.call(pry_instance)
+ end
+ end
+
+ context "and when it is called with multiple arguments" do
+ before { allow(Pry::Warning).to receive(:warn) }
+
+ it "calls the handler with a pry instance" do
+ subject.control_d_handler = proc do |arg|
+ expect(arg).to eql(pry_instance)
+ end
+ subject.control_d_handler.call('', pry_instance)
+ end
+ end
+ end
+
+ context "when it returns a callabale with two arguments" do
+ before { allow(Pry::Warning).to receive(:warn) }
+
+ context "and when it's called with one argument" do
+ it "calls the handler with a eval_string and a pry instance" do
+ subject.control_d_handler = proc do |arg1, arg2|
+ expect(arg1).to eq('')
+ expect(arg2).to eql(pry_instance)
+ end
+ subject.control_d_handler.call(pry_instance)
+ end
+ end
+
+ context "and when it's called with multiple arguments" do
+ it "calls the handler with a eval_string and a pry instance" do
+ subject.control_d_handler = proc do |arg1, arg2|
+ expect(arg1).to eq('')
+ expect(arg2).to eql(pry_instance)
+ end
+ subject.control_d_handler.call('', pry_instance)
+ end
+ end
+ end
+ end
end