Sha256: 152c9212b35617ad0f8f5565aa530b4441289fea1e384ad9ac7d0f166ba995f2

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'
require 'method_decorators/decorators/memoize'

describe Memoize do
  describe "#call" do
    let(:method) { double(:method, :call => :calculation) }
    let(:this) { Object.new }
    subject { Memoize.new }

    it "calculates the value the first time the arguments are supplied" do
      method.should_receive(:call)
      subject.call(method, this, 10)
    end

    it "stores the value of the method call" do
      method.stub(:call).and_return(:foo, :bar)
      subject.call(method, this, 10).should == :foo
      subject.call(method, this, 10).should == :foo
    end

    it "memoizes the return value and skips the call the second time" do
      subject.call(method, this, 10)
      method.should_not_receive(:call)
      subject.call(method, this, 10)
    end

    it "memoizes different values for different arguments" do
      method.stub(:call).with(10).and_return(:foo, :bar)
      method.stub(:call).with(20).and_return(:bar, :foo)
      subject.call(method, this, 10).should == :foo
      subject.call(method, this, 10).should == :foo
      subject.call(method, this, 20).should == :bar
      subject.call(method, this, 20).should == :bar
    end
  end

  describe "acceptance" do
    let(:klass) do
      Class.new Base do
        +Memoize
        def count
          @count ||= 0
          @count += 1
          rand
        end
      end
    end
    subject { klass.new }

    it "memoizes calls to the method" do
      x = subject.count
      subject.count.should == x
      #subject.count.should == 1
      #subject.count.should == 1
    end

    context "with two instances of the decorated class" do
      let(:o1) { subject }
      let(:o2) { klass.new }
      it "cache does not interact with that of other instances" do
        o1.count.should_not == o2.count
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
method_decorators-0.9.2 spec/decorators/memoize_spec.rb