Sha256: 450c35febd9979f8957aba828273b1efae699300e15aa03d63d1a8ee28b0ad2b

Contents?: true

Size: 971 Bytes

Versions: 1

Compression:

Stored size: 971 Bytes

Contents

require "spec_helper"

RSpec.describe "Rubber Duck examples" do
  it "casts objects as duck types" do
    # standard:disable Lint/ConstantDefinitionInBlock
    Person = Plumbing::RubberDuck.define :first_name, :last_name, :email
    LikesFood = Plumbing::RubberDuck.define :favourite_food

    PersonData = Struct.new(:first_name, :last_name, :email, :favourite_food)
    CarData = Struct.new(:make, :model, :colour)
    # standard:enable Lint/ConstantDefinitionInBlock

    @porsche_911 = CarData.new "Porsche", "911", "black"
    expect { @porsche_911.as Person }.to raise_error(TypeError)

    @alice = PersonData.new "Alice", "Aardvark", "alice@example.com", "Ice cream"

    @person = @alice.as Person
    expect(@person.first_name).to eq "Alice"
    expect(@person.email).to eq "alice@example.com"
    expect { @person.favourite_food }.to raise_error(NoMethodError)

    @hungry = @person.as LikesFood
    expect(@hungry.favourite_food).to eq "Ice cream"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
standard-procedure-plumbing-0.3.2 spec/examples/rubber_duck_spec.rb