Sha256: 705ecb13f4f38d4d5fa2ce066a114bbf42b8fe9a44b3d3c3e6b0188d91b4f103

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require 'spec_helper'

module RenderWithView
  RSpec.describe HalfOpenStruct do
    it "can be initialized with a hash, and values read and written" do
      s = HalfOpenStruct.new(name: "Joe")
      expect(s.name).to eq "Joe"

      s.age = 42
      expect(s.age).to eq 42
    end

    it "raises trying to read an unknown value" do
      s = HalfOpenStruct.new

      expect { s.age }.to raise_error("Unknown key: age")

      s.age = 42
      expect(s.age).to eq 42
    end

    it "raises for unknown methods" do
      s = HalfOpenStruct.new

      expect { s.foo("bar") }.to raise_error(NoMethodError)
    end

    # Considered an API like "s.age?" but that's ambiguous with undefined vs. falsy values.
    it "lets you query for values being defined" do
      s = HalfOpenStruct.new
      expect(s.include?(:age)).to be false

      s.age = 42
      expect(s.include?(:age)).to be true
    end

    it "lets you fetch values with a fallback" do
      s = HalfOpenStruct.new
      expect(s.fetch(:age, :fallback)).to eq :fallback

      s.age = 42
      expect(s.fetch(:age, :fallback)).to eq 42
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
render_with_view-0.1.3 spec/render_with_view/half_open_struct_spec.rb
render_with_view-0.1.2 spec/render_with_view/half_open_struct_spec.rb
render_with_view-0.1.1 spec/render_with_view/half_open_struct_spec.rb