Sha256: bfcdab9941ebd7c4a302b0d7e159a4645fc4a1c86e59441ffc4c705684262355

Contents?: true

Size: 862 Bytes

Versions: 2

Compression:

Stored size: 862 Bytes

Contents

require "spec_helper"
require "hamster/hash"

describe Hamster::Hash do
  describe "#dig" do
    let(:h) { H[:a => 9, :b => H[:c => 'a', :d => 4], :e => nil] }
    it "returns the value with one argument to dig" do
      expect(h.dig(:a)).to eq(9)
    end

    it "returns the value in nested hashes" do
      expect(h.dig(:b, :c)).to eq('a')
    end

    it "returns nil if the key is not present" do
      expect(h.dig(:f, :foo)).to eq(nil)
    end

    it "returns nil if you dig out the end of the hash" do
      expect(h.dig(:f, :foo, :bar)).to eq(nil)
    end

    it "returns nil if a value does not support dig" do
      expect(h.dig(:a, :foo)).to eq(nil)
    end

    it "returns the correct value when there is a default proc" do
      default_hash = H.new { |k| "#{k}-default" }
      expect(default_hash.dig(:a)).to eq("a-default")
    end
  end
end

Version data entries

2 entries across 2 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/dig_spec.rb
hamster-3.0.0 spec/lib/hamster/hash/dig_spec.rb