Sha256: 8e28260eb88fd9434f592fdcf34d30f86205e5c67d31daeed4e23a77c7ebc772

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require "spec_helper"
require "hamster/hash"

describe Hamster::Hash do
  describe ".new" do
    it "is amenable to overriding of #initialize" do
      class SnazzyHash < Hamster::Hash
        def initialize
          super({'snazzy?' => 'oh yeah'})
        end
      end

      SnazzyHash.new['snazzy?'].should == 'oh yeah'
    end

    context "from a subclass" do
      it "returns a frozen instance of the subclass" do
        subclass = Class.new(Hamster::Hash)
        instance = subclass.new("some" => "values")
        instance.class.should be(subclass)
        instance.frozen?.should be true
      end
    end

    it "accepts an array as initializer" do
      H.new([['a', 'b'], ['c', 'd']]).should eql(H['a' => 'b', 'c' => 'd'])
    end

    it "returns a Hash which doesn't change even if initializer is mutated" do
      rbhash = {a: 1, b: 2}
      hash = H.new(rbhash)
      rbhash[:a] = 'BAD'
      hash.should eql(H[a: 1, b: 2])
    end
  end

  describe ".[]" do
    it "accepts a Ruby Hash as initializer" do
      hash = H[a: 1, b: 2]
      hash.class.should be(Hamster::Hash)
      hash.size.should == 2
      hash.key?(:a).should == true
      hash.key?(:b).should == true
    end

    it "accepts a Hamster::Hash as initializer" do
      hash = H[H.new(a: 1, b: 2)]
      hash.class.should be(Hamster::Hash)
      hash.size.should == 2
      hash.key?(:a).should == true
      hash.key?(:b).should == true
    end

    it "accepts an array as initializer" do
      hash = H[[[:a, 1], [:b, 2]]]
      hash.class.should be(Hamster::Hash)
      hash.size.should == 2
      hash.key?(:a).should == true
      hash.key?(:b).should == true
    end

    it "can be used with a subclass of Hamster::Hash" do
      subclass = Class.new(Hamster::Hash)
      instance = subclass[a: 1, b: 2]
      instance.class.should be(subclass)
      instance.size.should == 2
      instance.key?(:a).should == true
      instance.key?(:b).should == true
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/hamster-3.0.0/spec/lib/hamster/hash/new_spec.rb
hamster-3.0.0 spec/lib/hamster/hash/new_spec.rb
hamster-2.0.0 spec/lib/hamster/hash/new_spec.rb