diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-03-25 02:40:05 +0200 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2019-05-02 00:10:37 +0300 |
commit | e5556a2be8627ec3fe594c738f10422d5a1f5d43 (patch) | |
tree | a1758a1f39915a4eb076793a36d485cb7145d638 /spec/config | |
parent | 03afca9eafe4f2981edad1cfe4346a643fb64d72 (diff) | |
download | pry-e5556a2be8627ec3fe594c738f10422d5a1f5d43.tar.gz |
Refactor Config
Fixes #1843 (Rework the Pry config)
There are a few breaking changes. They are mostly minor, so I decided not to
indroduce deprecations because it will considerable slow things down.
Key changes:
* `Pry.lazy` was replaced with `Pry::Configuration::LazyValue`
The config accepts three values now `LazyValue`, `MemoizedValue` and simply
`Value`. The main difference is that:
- `Value` is any value, including procs (so that an option returns a raw
proc)
- `LazyValue` is a proc that is call on every invocation of an option
- `MemoizedValue` is a value that is called only once (and then the option
always returns the return value of the )
* `Pry.config.history` was a meta-option that held suboptions. However, the new
config doesn't permit that (unless you know what you do)
Instead, we introduce a few options. For example:
- `Pry.config.history.histignore` becomes `Pry.config.history_ignorelist`
- `Pry.config.history.file` becomes `Pry.config.history_file`
- and so on
This was done so we can simplify configuration merging. Inlining option makes
configuration implementation simpler, without losing much. The rule is that
you want to keep your options under your prefix (if you are a
plugin). Therefore, say, `Pry.config.pry_rescue.*` should be
`Pry.config.pry_rescue_*` if you need merging.
The rest should behave in a similar fashion (and I rely heavily on our test
suite to claim so).
Diffstat (limited to 'spec/config')
-rw-r--r-- | spec/config/attributable_spec.rb | 27 | ||||
-rw-r--r-- | spec/config/behavior_spec.rb | 21 | ||||
-rw-r--r-- | spec/config/lazy_value_spec.rb | 9 | ||||
-rw-r--r-- | spec/config/memoized_value_spec.rb | 9 | ||||
-rw-r--r-- | spec/config/value_spec.rb | 37 |
5 files changed, 82 insertions, 21 deletions
diff --git a/spec/config/attributable_spec.rb b/spec/config/attributable_spec.rb new file mode 100644 index 00000000..c0c2b167 --- /dev/null +++ b/spec/config/attributable_spec.rb @@ -0,0 +1,27 @@ +RSpec.describe Pry::Config::Attributable do + subject { klass.new } + + describe "#attribute" do + let(:klass) do + Class.new do + extend Pry::Config::Attributable + attribute :foo + end + end + + it "creates a reader attribute for the given name" do + expect(klass.instance_method(:foo)).to be_a(UnboundMethod) + end + + it "creates a writer attribute for the given name" do + expect(klass.instance_method(:foo=)).to be_a(UnboundMethod) + end + + context "and when the attribute is invoked" do + it "sends the 'call' message to the value" do + expect_any_instance_of(Pry::Config::Value).to receive(:call) + subject.foo + end + end + end +end diff --git a/spec/config/behavior_spec.rb b/spec/config/behavior_spec.rb deleted file mode 100644 index 9a789a16..00000000 --- a/spec/config/behavior_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -RSpec.describe Pry::Config::Behavior do - let(:behavior) do - Class.new do - include Pry::Config::Behavior - end - end - - describe "#last_default" do - it "returns the last default" do - last = behavior.from_hash({}, nil) - middle = behavior.from_hash({}, last) - expect(behavior.from_hash({}, middle).last_default).to be(last) - end - end - - describe "#eager_load!" do - it "returns nil when the default is nil" do - expect(behavior.from_hash({}, nil).eager_load!).to be(nil) - end - end -end diff --git a/spec/config/lazy_value_spec.rb b/spec/config/lazy_value_spec.rb new file mode 100644 index 00000000..dc2aaa1e --- /dev/null +++ b/spec/config/lazy_value_spec.rb @@ -0,0 +1,9 @@ +RSpec.describe Pry::Config::LazyValue do + describe "#call" do + subject { described_class.new { rand } } + + it "doesn't memoize the result of call" do + expect(subject.call).not_to eq(subject.call) + end + end +end diff --git a/spec/config/memoized_value_spec.rb b/spec/config/memoized_value_spec.rb new file mode 100644 index 00000000..1601bff2 --- /dev/null +++ b/spec/config/memoized_value_spec.rb @@ -0,0 +1,9 @@ +RSpec.describe Pry::Config::MemoizedValue do + describe "#call" do + subject { described_class.new { rand } } + + it "memoizes the result of call" do + expect(subject.call).to eq(subject.call) + end + end +end diff --git a/spec/config/value_spec.rb b/spec/config/value_spec.rb new file mode 100644 index 00000000..efb8eca4 --- /dev/null +++ b/spec/config/value_spec.rb @@ -0,0 +1,37 @@ +RSpec.describe Pry::Config::Value do + describe "#call" do + context "when given value is a MemoizedValue" do + subject { described_class.new(Pry::Config::MemoizedValue.new { 123 }) } + + it "calls the MemoizedLazy object" do + expect(subject.call).to eq(123) + end + end + + context "when given value is a LazyValue" do + subject { described_class.new(Pry::Config::LazyValue.new { 123 }) } + + it "calls the LazyValue object" do + expect(subject.call).to eq(123) + end + end + + context "when given value is a Proc" do + let(:callable) { proc {} } + + subject { described_class.new(callable) } + + it "returns the value as is" do + expect(subject.call).to eq(callable) + end + end + + context "when given value is a non-callable object" do + subject { described_class.new('test') } + + it "returns the value as is" do + expect(subject.call).to eq('test') + end + end + end +end |