From 54e6ce52f7691779a1c6ee0397b4ae374fee6c37 Mon Sep 17 00:00:00 2001 From: r-obert Date: Wed, 5 Dec 2018 22:18:31 +0100 Subject: [PATCH] Fix a bug where `Pry::Config.new(nil) == nil` would return true. (#1903) --- lib/pry/config/behavior.rb | 2 ++ spec/config_spec.rb | 48 +++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 9d6c936a..8b1a5a3d 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -215,6 +215,8 @@ class Pry # True if self and `other` are considered `eql?`, otherwise false. # def ==(other) + return false if !other + @lookup == __try_convert_to_hash(other) end alias_method :eql?, :== diff --git a/spec/config_spec.rb b/spec/config_spec.rb index bd2b63b0..b3c6f734 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -1,4 +1,24 @@ -describe Pry::Config do +RSpec.describe Pry::Config do + describe ".from_hash" do + it "returns an object without a default" do + local = described_class.from_hash({}) + expect(local.default).to eq(nil) + end + + it "returns an object with a default" do + default = described_class.new(nil) + local = described_class.from_hash({}, default) + expect(local.default).to eq(local) + end + + it "recursively creates Pry::Config objects from a Hash" do + h = {'foo1' => {'foo2' => {'foo3' => 'foobar'}}} + default = described_class.from_hash(h) + expect(default.foo1).to be_instance_of(described_class) + expect(default.foo1.foo2).to be_instance_of(described_class) + end + end + describe "bug #1552" do specify "a local key has precendence over its default when the stored value is false" do local = described_class.from_hash({}, described_class.from_hash('color' => true)) @@ -53,26 +73,6 @@ describe Pry::Config do end end - describe ".from_hash" do - it "returns an object without a default" do - local = described_class.from_hash({}) - expect(local.default).to eq(nil) - end - - it "returns an object with a default" do - default = described_class.new(nil) - local = described_class.from_hash({}, default) - expect(local.default).to eq(local) - end - - it "recursively creates Pry::Config objects from a Hash" do - h = {'foo1' => {'foo2' => {'foo3' => 'foobar'}}} - default = described_class.from_hash(h) - expect(default.foo1).to be_instance_of(described_class) - expect(default.foo1.foo2).to be_instance_of(described_class) - end - end - describe "#respond_to_missing?" do before do @config = described_class.new(nil) @@ -140,6 +140,12 @@ describe Pry::Config do local1 = described_class.new(nil) expect(local1).not_to eq(Object.new) end + + it "returns false when compared against nil" do + # rubocop:disable Style/NilComparison + expect(described_class.new(nil) == nil).to eq(false) + # rubocop:enable Style/NilComparison + end end describe "#forget" do