Sha256: 25d3eb06df38e12e4beb98c6852fb9b1ec3e341b29c31969e809dfd6a7b529e6

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

require 'spec_helper'

describe Monolens, "core.dig" do
  let(:lens) do
    Monolens.lens('core.dig' => { defn: ['hobbies', 1, 'name'] })
  end

  it 'works' do
    input = {
      hobbies: [
        { name: 'programming' },
        { name: 'music' }
      ]
    }
    expected = 'music'
    expect(lens.call(input)).to eql(expected)
  end

  describe 'error handling' do
    let(:lens) do
      Monolens.lens({
        'array.map' => {
          lenses: {
            'core.dig' => {
              on_missing: on_missing,
              defn: ['hobbies', 1, 'name']
            }.compact
          }
        }
      })
    end

    subject do
      begin
        lens.call(input)
      rescue Monolens::LensError => ex
        ex
      end
    end

    context 'default behavior' do
      let(:on_missing) do
        nil
      end

      let(:input) do
        [{
          hobbies: [
            { name: 'programming' }
          ]
        }]
      end

      it 'fails as expected' do
        expect(subject).to be_a(Monolens::LensError)
        expect(subject.location).to eql([0])
      end
    end

    context 'on_missing: null' do
      let(:on_missing) do
        :null
      end

      let(:input) do
        [{
          hobbies: [
            { name: 'programming' }
          ]
        }]
      end

      it 'works' do
        expect(subject).to eql([nil])
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
monolens-0.6.4 spec/monolens/stdlib/core/test_dig.rb
monolens-0.6.3 spec/monolens/stdlib/core/test_dig.rb
monolens-0.6.2 spec/monolens/stdlib/core/test_dig.rb
monolens-0.6.1 spec/monolens/stdlib/core/test_dig.rb
monolens-0.6.0 spec/monolens/stdlib/core/test_dig.rb
monolens-0.5.3 spec/monolens/core/test_dig.rb
monolens-0.5.2 spec/monolens/core/test_dig.rb
monolens-0.5.1 spec/monolens/core/test_dig.rb
monolens-0.5.0 spec/monolens/core/test_dig.rb
monolens-0.4.0 spec/monolens/core/test_dig.rb