Sha256: 2408ba57032bc4fe95b90480fcf942632d3ec3fd0a00369e63c48b8e8ff9ba18

Contents?: true

Size: 1.85 KB

Versions: 14

Compression:

Stored size: 1.85 KB

Contents

describe "hash" do
  describe "slice" do
    it "should return a new hash with only the given keys" do
      original = { :a => 'x', :b => 'y', :c => 10 }
      expected = { :a => 'x', :b => 'y' }

      original.slice(:a, :b).should == expected
      original.should.not == expected
    end
    
    it "should replace the hash with only the given keys" do
      original = { :a => 'x', :b => 'y', :c => 10 }
      expected = { :c => 10 }

      original.slice!(:a, :b).should == expected
    end
    
    it "should return a new hash with only the given keys when given an array key" do
      original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
      expected = { [:a, :b] => "an array key", :c => 10 }

      original.slice([:a, :b], :c).should == expected
      original.should.not == expected
    end

    it "should replace the hash with only the given keys when given an array key" do
      original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
      expected = { :a => 'x', :b => 'y' }

      original.slice!([:a, :b], :c).should == expected
    end

    it "should grab each of the splatted keys" do
      original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
      expected = { :a => 'x', :b => "y" }

      original.slice(*[:a, :b]).should == expected
    end
  end
  
  describe "extract!" do
    it "should extract subhash" do
      original = {:a => 1, :b => 2, :c => 3, :d => 4}
      expected = {:a => 1, :b => 2}
      remaining = {:c => 3, :d => 4}

      original.extract!(:a, :b, :x).should == expected
      original.should == remaining
    end

    it "should extract nils" do
      original = {:a => nil, :b => nil}
      expected = {:a => nil}
      extracted = original.extract!(:a, :x)

      extracted.should == expected
      extracted[:a].should == nil
      extracted[:x].should == nil
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
motion-support-1.2.1 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-1.1.1 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-1.2.0 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-1.1.0 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-1.0.0 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.3.0 spec/motion-support/core_ext/hash/slice_spec.rb
motion_blender-support-0.2.8 spec/motion-support/core_ext/hash/slice_spec.rb
motion_blender-support-0.2.7 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.2.6 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.2.5 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.2.4 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.2.3 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.2.2 spec/motion-support/core_ext/hash/slice_spec.rb
motion-support-0.2.0 spec/motion-support/core_ext/hash/slice_spec.rb