Sha256: c76450426613f94bb52096cdeb40add06b76e8b8e0d55a35d9876a7ca37acfd5

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

require "spec_helper"

describe Object, ".vattr_initialize" do
  it "creates initializer, value readers and value object identity" do
    klass = Class.new do
      vattr_initialize :foo, :bar
    end

    example1 = klass.new("Foo", "Bar")
    example2 = klass.new("Foo", "Bar")

    _(example1.foo).must_equal "Foo"
    _(example1).must_equal example2
  end

  it "works with hash ivars" do
    klass = Class.new do
      vattr_initialize :foo, [:bar, :baz!]
    end

    example1 = klass.new("Foo", bar: "Bar", baz: "Baz")
    example2 = klass.new("Foo", bar: "Bar", baz: "Baz")
    _(example1.baz).must_equal "Baz"
    _(example1).must_equal example2
  end

  it "works with hash ivars and default values" do
    klass = Class.new do
      vattr_initialize :foo, [ bar: "Bar", baz!: "Baz" ]
    end

    example1 = klass.new("Foo")
    example2 = klass.new("Foo")
    _(example1.baz).must_equal "Baz"
    _(example1).must_equal example2
  end

  it "can accept an initializer block" do
    called = false
    klass = Class.new do
      vattr_initialize :value do
        called = true
      end
    end

    klass.new("expected")

    _(called).must_equal true
  end

  it "accepts the alias attr_value_initialize" do
    klass = Class.new do
      attr_value_initialize :foo, :bar
    end

    example1 = klass.new("Foo", "Bar")
    example2 = klass.new("Foo", "Bar")

    _(example1.foo).must_equal "Foo"
    _(example1).must_equal example2
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
attr_extras-6.2.5 spec/attr_extras/vattr_initialize_spec.rb
attr_extras-6.2.4 spec/attr_extras/vattr_initialize_spec.rb
attr_extras-6.2.3 spec/attr_extras/vattr_initialize_spec.rb
attr_extras-6.2.2 spec/attr_extras/vattr_initialize_spec.rb