Sha256: 5f8b9c279bf5b9a9dfb0378662d55d9e750e9d047800de24664bb4c702502e94

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Sinclair::MethodDefinition do
  describe 'yard' do
    describe '#build' do
      describe 'using string method with no options' do
        subject(:method_definition) do
          described_class.from(name, code)
        end

        let(:klass)    { Class.new }
        let(:instance) { klass.new }
        let(:code)     { '@x = @x.to_i ** 2 + 1' }
        let(:name)     { :sequence }

        it 'adds a dynamic method' do
          expect { method_definition.build(klass) }.to add_method(name).to(instance)
          expect { instance.sequence }
            .to change { instance.instance_variable_get(:@x) }.from(nil).to 1
          expect(instance.sequence).to eq(2)
          expect(instance.sequence).to eq(5)
        end

        it 'changes instance variable' do
          method_definition.build(klass)

          expect { instance.sequence }
            .to change { instance.instance_variable_get(:@x) }
            .from(nil).to 1
        end
      end

      describe 'using block with cache option' do
        subject(:method_definition) do
          described_class.from(name, cached: true) do
            @x = @x.to_i**2 + 1
          end
        end

        let(:klass)    { Class.new }
        let(:instance) { klass.new }
        let(:name)     { :sequence }

        it 'adds a dynamic method' do
          expect { method_definition.build(klass) }.to add_method(name).to(instance)
          expect { instance.sequence }
            .to change { instance.instance_variable_get(:@x) }.from(nil).to 1
          expect { instance.sequence }.not_to change(instance, :sequence)
          expect(instance.instance_variable_get(:@sequence)).to eq(1)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinclair-1.4.1 spec/integration/yard/sinclair/method_definition_spec.rb
sinclair-1.4.0 spec/integration/yard/sinclair/method_definition_spec.rb