Sha256: cbdbe26dfce0654ba31c7172633b46088f47903d5f4d170b8e9828969f1eeff1

Contents?: true

Size: 1.76 KB

Versions: 31

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Sinclair::MethodDefinition::BlockDefinition do
  subject(:definition) do
    described_class.new(method_name, **options, &block)
  end

  let(:method_name) { :the_method }
  let(:block)       { proc { RandomGenerator.rand } }
  let(:options)     { {} }

  describe '#method_block' do
    let(:klass)     { Class.new }
    let(:instance)  { klass.new }
    let(:new_block) { definition.method_block }

    it 'returns the block' do
      expect(definition.method_block).to eq(block)
    end

    it 'returns a block with no cache' do
      expect(instance.instance_eval(&new_block))
        .not_to eq(instance.instance_eval(&new_block))
    end

    context 'when cache true is given' do
      let(:options) { { cached: true } }

      it 'returns the a new block' do
        expect(definition.method_block).to be_a(Proc)
      end

      it 'returns a block with cache' do
        expect(instance.instance_eval(&new_block))
          .to eq(instance.instance_eval(&new_block))
      end

      it 'returns a block that does not cache nil' do
        instance.instance_variable_set("@#{method_name}", nil)

        expect(instance.instance_eval(&new_block)).not_to be_nil
      end
    end

    context 'when cache full is given' do
      let(:options) { { cached: :full } }

      it 'returns the a new block' do
        expect(definition.method_block).to be_a(Proc)
      end

      it 'returns a block with cache' do
        expect(instance.instance_eval(&new_block))
          .to eq(instance.instance_eval(&new_block))
      end

      it 'returns a block that does caches nil' do
        instance.instance_variable_set("@#{method_name}", nil)

        expect(instance.instance_eval(&new_block)).to be_nil
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
sinclair-2.1.1 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-2.1.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-2.0.1 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-2.0.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.16.3 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.16.2 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.16.1 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.16.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.15.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.14.2 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.14.1 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.14.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.13.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.12.1 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.12.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.11.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.10.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.9.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.8.0 spec/lib/sinclair/method_definition/block_definition_spec.rb
sinclair-1.7.0 spec/lib/sinclair/method_definition/block_definition_spec.rb