Sha256: c595bc5bc3c53fe7ff45719a21b83ff2cd3b69c83fcab808e14256d9b0048b22

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8

require 'spec_helper'
require File.expand_path('../../fixtures/classes', __FILE__)

shared_examples_for 'memoizes method' do
  it 'memoizes the instance method' do
    subject
    instance = object.new
    instance.send(method).should equal(instance.send(method))
  end

  it 'creates a method that returns a frozen value' do
    subject
    instance = object.new
    instance.send(method).should be_frozen
  end

  specification = proc do
    object.send(:define_method, method) do
      caller
    end

    subject

    file, line = object.new.send(method).first.split(':')[0, 2]

    File.expand_path(file).should == File.expand_path('../../../../../../lib/veritas/support/immutable.rb', __FILE__)
    line.to_i.should == 179
  end

  it 'sets the file and line number properly' do
    if RUBY_PLATFORM[/java/]
      pending('Kernel#caller returns the incorrect line number in JRuby', &specification)
    else
      instance_eval(&specification)
    end
  end
end

describe Immutable::ModuleMethods, '#memoize' do
  subject { object.memoize(method) }

  let(:object) { Class.new(ImmutableSpecs::Object) }

  context 'public method' do
    let(:method) { :public_method }

    it { should equal(object) }

    it_should_behave_like 'memoizes method'

    it 'is still a public method' do
      should be_public_method_defined(method)
    end
  end

  context 'protected method' do
    let(:method) { :protected_method }

    it { should equal(object) }

    it_should_behave_like 'memoizes method'

    it 'is still a protected method' do
      should be_protected_method_defined(method)
    end
  end

  context 'private method' do
    let(:method) { :private_method }

    it { should equal(object) }

    it_should_behave_like 'memoizes method'

    it 'is still a private method' do
      should be_private_method_defined(method)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
veritas-0.0.4 spec/unit/veritas/immutable/module_methods/memoize_spec.rb