Sha256: a439147b8699168a13f909498f1d16f875101f6e0ef18c85056f16b6d744414f

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

describe "optional value" do
  context "when has no default value" do
    before do
      class Test::Foo
        extend Dry::Initializer

        param :foo
        param :bar, optional: true
      end
    end

    it "quacks like nil" do
      subject = Test::Foo.new(1)

      expect(subject.bar).to eq nil
    end

    it "keeps info about been UNDEFINED" do
      subject = Test::Foo.new(1)

      expect(subject.instance_variable_get(:@bar))
        .to eq Dry::Initializer::UNDEFINED
    end

    it "can be set explicitly" do
      subject = Test::Foo.new(1, "qux")

      expect(subject.bar).to eq "qux"
    end
  end

  context "with undefined: false" do
    before do
      class Test::Foo
        extend Dry::Initializer[undefined: false]

        param :foo
        param :bar, optional: true
      end
    end

    it "sets undefined values to nil" do
      subject = Test::Foo.new(1)

      expect(subject.instance_variable_get(:@bar)).to be_nil
    end
  end

  context "when has a default value" do
    before do
      class Test::Foo
        extend Dry::Initializer

        param :foo
        param :bar, optional: true, default: proc { "baz" }
      end
    end

    it "is takes default value" do
      subject = Test::Foo.new(1)

      expect(subject.bar).to eq "baz"
    end

    it "can be set explicitly" do
      subject = Test::Foo.new(1, "qux")

      expect(subject.bar).to eq "qux"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-initializer-3.0.2 spec/optional_spec.rb
dry-initializer-3.0.1 spec/optional_spec.rb
dry-initializer-3.0.0 spec/optional_spec.rb
dry-initializer-2.5.0 spec/optional_spec.rb
dry-initializer-2.4.0 spec/optional_spec.rb
dry-initializer-2.3.0 spec/optional_spec.rb
dry-initializer-2.2.0 spec/optional_spec.rb
dry-initializer-2.1.0 spec/optional_spec.rb
dry-initializer-2.0.0 spec/optional_spec.rb