1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/spec/config_spec.rb

168 lines
4.2 KiB
Ruby
Raw Normal View History

require_relative 'helper'
2014-01-21 10:32:03 -05:00
describe Pry::Config do
describe "reserved keys" do
it "raises an ArgumentError on assignment of a reserved key" do
local = Pry::Config.new
2014-01-29 19:41:56 -05:00
Pry::Config::RESERVED_KEYS.each do |key|
2014-01-30 20:43:13 -05:00
should.raise(ArgumentError) { local[key] = 1 }
2014-01-29 19:41:56 -05:00
end
end
end
2014-01-30 20:39:23 -05:00
describe "traversal to parent" do
it "traverses back to the parent when a local key is not found" do
2014-01-30 20:43:13 -05:00
local = Pry::Config.new Pry::Config.from_hash(foo: 1)
local.foo.should == 1
2014-01-21 10:32:03 -05:00
end
2014-01-30 20:39:23 -05:00
it "stores a local key and prevents traversal to the parent" do
2014-01-30 20:43:13 -05:00
local = Pry::Config.new Pry::Config.from_hash(foo: 1)
local.foo = 2
local.foo.should == 2
2014-01-21 10:32:03 -05:00
end
2014-01-30 20:39:23 -05:00
it "duplicates a copy on read from the parent" do
ukraine = "i love"
2014-01-30 20:43:13 -05:00
local = Pry::Config.new Pry::Config.from_hash(home: ukraine)
local.home.equal?(ukraine).should == false
2014-01-21 10:32:03 -05:00
end
it "traverses through a chain of parents" do
root = Pry::Config.from_hash({foo: 21})
local1 = Pry::Config.new(root)
local2 = Pry::Config.new(local1)
local3 = Pry::Config.new(local2)
local3.foo.should == 21
end
2014-01-21 10:32:03 -05:00
end
2014-02-02 08:52:55 -05:00
describe ".from_hash" do
2014-03-14 00:40:09 -04:00
it "returns an object without a default" do
2014-02-02 08:52:55 -05:00
local = Pry::Config.from_hash({})
2014-03-14 00:40:09 -04:00
local.default.should == nil
2014-02-02 08:52:55 -05:00
end
2014-03-14 00:40:09 -04:00
it "returns an object with a default" do
2014-02-02 08:52:55 -05:00
default = Pry::Config.new(nil)
local = Pry::Config.from_hash({}, default)
2014-03-14 00:40:09 -04:00
local.default.should == local
2014-02-02 08:52:55 -05:00
end
end
2014-03-14 00:40:09 -04:00
describe "#default" do
it "returns nil" do
local = Pry::Config.new(nil)
local.default.should == nil
end
it "returns the default" do
default = Pry::Config.new(nil)
local = Pry::Config.new(default)
local.default.should == default
end
end
2014-02-02 17:23:23 -05:00
describe "#keys" do
it "returns an array of local keys" do
root = Pry::Config.from_hash({zoo: "boo"}, nil)
local = Pry::Config.from_hash({foo: "bar"}, root)
local.keys.should == ["foo"]
end
end
2014-02-02 08:52:55 -05:00
describe "#==" do
it "compares equality through the underlying lookup table" do
local1 = Pry::Config.new(nil)
local2 = Pry::Config.new(nil)
local1.foo = "hi"
local2.foo = "hi"
local1.should == local2
end
it "compares equality against an object who does not implement #to_hash" do
local1 = Pry::Config.new(nil)
local1.should.not == Object.new
end
2014-02-02 08:52:55 -05:00
end
2014-01-31 08:18:32 -05:00
describe "#forget" do
it "forgets a local key" do
local = Pry::Config.new Pry::Config.from_hash(foo: 1)
local.foo = 2
local.foo.should == 2
local.forget(:foo)
local.foo.should == 1
end
end
describe "#to_hash" do
it "provides a copy of local key & value pairs as a Hash" do
local = Pry::Config.new Pry::Config.from_hash(bar: true)
local.foo = "21"
local.to_hash.should == { "foo" => "21" }
end
it "returns a duplicate of the lookup table" do
local = Pry::Config.new(nil)
local.to_hash.merge!("foo" => 42)
local.foo.should.not == 42
end
2014-01-31 08:18:32 -05:00
end
describe "#merge!" do
before do
@config = Pry::Config.new(nil)
2014-01-31 08:18:32 -05:00
end
it "merges an object who returns a Hash through #to_hash" do
obj = Class.new { def to_hash() {epoch: 1} end }.new
@config.merge!(obj)
@config.epoch.should == 1
end
it "merges an object who returns a Hash through #to_h" do
obj = Class.new { def to_h() {epoch: 2} end }.new
@config.merge!(obj)
@config.epoch.should == 2
end
it "merges a Hash" do
@config.merge!(epoch: 420)
@config.epoch.should == 420
end
it "raises a TypeError for objects who can't become a Hash" do
should.raise(TypeError) { @config.merge!(Object.new) }
2014-01-31 08:18:32 -05:00
end
end
2014-03-14 22:05:19 -04:00
describe "#clear" do
before do
@local = Pry::Config.new(nil)
end
it "returns true" do
@local.clear.should == true
end
it "clears local assignments" do
@local.foo = 1
@local.clear
@local.to_hash.should == {}
end
it "is aliased as #refresh" do
@local.method(:clear).should == @local.method(:refresh)
end
end
2014-01-30 20:39:23 -05:00
describe "#[]=" do
it "stores keys as strings" do
local = Pry::Config.from_hash({})
local[:zoo] = "hello"
local.to_hash.should == { "zoo" => "hello" }
2014-01-21 10:32:03 -05:00
end
end
end