Sha256: af65b88c409a8d2dbf7ee736cf7e5560991e725ec921d25b9abac99c3f0a9c9b

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'spec_helper'

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

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

  describe 'error handling' do
    let(:lens) do
      Monolens.lens({
        'array.map' => {
          lenses: {
            'core.digs' => {
              on_missing: on_missing,
              defn: [
                ['foo'],
                ['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, nil]])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
monolens-0.6.4 spec/monolens/stdlib/core/test_digs.rb