Sha256: 85493b2590accc6c57ea781e514b2a474504ebfe8b0fcae8d9f07445588184b1

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require "spec_helper"

describe Mem do
  describe "#memoize" do
    let(:object) do
      klass.new
    end

    context "with normal method name" do
      let(:klass) do
        Class.new do
          include Mem

          def a(value, &block)
            b(value, &block)
          end
          memoize :a

          def b(value)
            [value, yield]
          end

          def c
            "c"
          end
          memoize :c
        end
      end

      it "memoizes the method call" do
        expect(object).to receive(:b).once.and_call_original
        expect(object.a(1) { 2 }).to eq [1, 2]
        expect(object.a(3) { 4 }).to eq [1, 2]
      end

      it "defines setter" do
        expect(object.c).to eq "c"
        object.c = "d"
        expect(object.c).to eq "d"
      end
    end

    context "with abnormal method name" do
      let(:klass) do
        Class.new do
          include Mem

          def a!(value, &block)
            b(value, &block)
          end
          memoize :a!

          def b(value)
            [value, yield]
          end
        end
      end

      it "memoizes the method call" do
        expect(object).to receive(:b).once.and_call_original
        expect(object.a!(1) { 2 }).to eq [1, 2]
        expect(object.a!(3) { 4 }).to eq [1, 2]
        object.should have_memoized(:a!)
        object.memoized(:a!).should == [1, 2]
        object.memoized_table.should == { a!: [1, 2] }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mem-0.1.4 spec/mem_spec.rb
mem-0.1.3 spec/mem_spec.rb
mem-0.1.2 spec/mem_spec.rb
mem-0.1.1 spec/mem_spec.rb