Sha256: c95e34b10e7351acff73129b9db974993d4a65f833222d9dffff1713900aec5f

Contents?: true

Size: 1.4 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
dry-initializer-3.0.3 spec/optional_spec.rb