2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
describe "Hash#compact" do
|
|
|
|
before :each do
|
|
|
|
@hash = { truthy: true, false: false, nil: nil, nil => true }
|
|
|
|
@initial_pairs = @hash.dup
|
|
|
|
@compact = { truthy: true, false: false, nil => true }
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "returns new object that rejects pair has nil value" do
|
|
|
|
ret = @hash.compact
|
|
|
|
ret.should_not equal(@hash)
|
|
|
|
ret.should == @compact
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "keeps own pairs" do
|
|
|
|
@hash.compact
|
|
|
|
@hash.should == @initial_pairs
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
describe "Hash#compact!" do
|
|
|
|
before :each do
|
|
|
|
@hash = { truthy: true, false: false, nil: nil, nil => true }
|
|
|
|
@initial_pairs = @hash.dup
|
|
|
|
@compact = { truthy: true, false: false, nil => true }
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "returns self" do
|
|
|
|
@hash.compact!.should equal(@hash)
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "rejects own pair has nil value" do
|
|
|
|
@hash.compact!
|
|
|
|
@hash.should == @compact
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when each pair does not have nil value" do
|
|
|
|
before :each do
|
2017-05-07 08:04:49 -04:00
|
|
|
@hash.compact!
|
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "returns nil" do
|
|
|
|
@hash.compact!.should be_nil
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
describe "on frozen instance" do
|
|
|
|
before :each do
|
|
|
|
@hash.freeze
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2020-02-08 21:07:01 -05:00
|
|
|
it "keeps pairs and raises a FrozenError" do
|
|
|
|
->{ @hash.compact! }.should raise_error(FrozenError)
|
2019-04-27 12:53:23 -04:00
|
|
|
@hash.should == @initial_pairs
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|