Sha256: 6316823022c1a890b463f545caf737dd7fa1a6d226fc3a708946e25a7677dd55

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

describe Hash do
  describe "ClassMethods" do
    subject { Hash }

    describe "::from_dotted_path" do
      it "returns a new Hash" do
        subject.from_dotted_path("deep.nested.item").should be_a(Hash)
      end

      it "a hash containing the nested keys" do
        obj = subject.from_dotted_path("deep.nested.item")

        obj.should have_key("deep")
        obj["deep"].should have_key("nested")
        obj["deep"]["nested"].should have_key("item")
      end

      it "sets a nil value for the deepest nested item" do
        obj = subject.from_dotted_path("deep.nested.item")

        obj["deep"]["nested"]["item"].should be_nil
      end

      context "when given a seed value" do
        it "sets the value of the deepest nested item to the seed" do
          obj = subject.from_dotted_path("deep.nested.item", "seeded_value")

          obj["deep"]["nested"]["item"].should eql("seeded_value")
        end
      end
    end
  end

  subject { Hash.new }

  describe "#dig" do
    context "when the Hash contains the nested path" do
      subject do
        {
          "we" => {
            "found" => {
              "something" => true
            }
          }
        }
      end

      it "returns the value at the dotted path" do
        subject.dig("we.found.something").should be_true
      end
    end

    context "when the Hash does not contain the nested path" do
      it "returns a nil value" do
        subject.dig("nothing.is.here").should be_nil
      end
    end

    context "when the Hash contains symbols for keys" do
      subject do
        {
          we: {
            found: {
              something: :symbol_value
            }
          }
        }
      end

      it "returns the value at the dotted path" do
        subject.dig("we.found.something").should eql(:symbol_value)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chozo-0.2.0 spec/unit/chozo/core_ext/hash_spec.rb
chozo-0.1.0 spec/unit/chozo/core_ext/hash_spec.rb
chozo-0.0.3 spec/unit/chozo/core_ext/hash_spec.rb