Sha256: 94c9fb79acc3f30ee6ecf07bd316368eef13d47e8051597e9fbf42013020b363

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Arstotzka::Crawler do
  describe 'yard' do
    subject do
      described_class.new(path: path, **options)
    end

    let(:options) { {} }
    let(:path)    { %w[person information first_name] }
    let(:hash) do
      {
        person: {
          'information' => {
            'firstName' => 'John'
          }
        }
      }
    end

    it 'crawls to find the value' do
      expect(subject.value(hash)).to eq('John')
    end

    describe '#value' do
      context 'when hash contains the path' do
        it 'crawls to find the value' do
          expect(subject.value(hash)).to eq('John')
        end
      end

      context 'when we have an array of arrays' do
        let(:path)    { %w[companies games hero_name] }
        let(:options) { { compact: true, case_type: :snake } }
        let(:hash) do
          {
            'companies' => [{
              name: 'Lucas Pope',
              games: [{
                'name' => 'papers, please'
              }, {
                'name' => 'TheNextBigThing',
                hero_name: 'Rakhar'
              }]
            }, {
              name: 'Old Company'
            }]
          }
        end

        it 'crawls to find the value' do
          expect(subject.value(hash)).to eq([['Rakhar']])
        end

        context 'and we set a default value' do
          let(:options) { { compact: true, case_type: :snake, default: 'NO HERO' } }

          it 'return default value for missed keys' do
            expect(subject.value(hash)).to eq([['NO HERO', 'Rakhar'], 'NO HERO'])
          end
        end

        context 'and we give a block' do
          subject do
            described_class.new(path: path, **options) { |value| value&.to_sym }
          end

          it 'returns the post processed values' do
            expect(subject.value(hash)).to eq([[:Rakhar]])
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arstotzka-1.0.3 spec/integration/yard/arstotzka/crawler_spec.rb
arstotzka-1.0.2 spec/integration/yard/arstotzka/crawler_spec.rb