Sha256: ac1be4e4a6f7bb0c9f42aadd40d15adf3710a25691597a9b526f4680769c55a0

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require 'minitest_helper'

describe Caching do
  
  class Cachable

    def initialize
      @slow_method = 0
      @slow_method_with_args = 0
      @fast_method = 0
    end

    def slow_method
      @slow_method += 1
    end
    cache_method :slow_method

    def slow_method_with_args(arg)
      @slow_method_with_args += 1
    end
    cache_method :slow_method_with_args

    def fast_method
      @fast_method += 1
    end

  end


  it 'Cache methods' do
    object = Cachable.new

    3.times { object.slow_method.must_equal 1 }
    3.times { object.slow_method_with_args([1]).must_equal 1 }
    3.times { object.slow_method_with_args('text').must_equal 2 }
    object.fast_method.must_equal 1
    object.fast_method.must_equal 2
  end

  it 'Clear cache for all methods' do
    object = Cachable.new

    3.times { object.slow_method.must_equal 1 }
    3.times { object.slow_method_with_args([1]).must_equal 1 }
    3.times { object.slow_method_with_args('text').must_equal 2 }

    object.clear_cache

    object.slow_method.must_equal 2
    object.slow_method_with_args(3).must_equal 3
  end

  it 'Clear cache for specific method' do
    object = Cachable.new

    3.times { object.slow_method.must_equal 1 }
    3.times { object.slow_method_with_args([1]).must_equal 1 }
    3.times { object.slow_method_with_args('text').must_equal 2 }

    object.clear_cache :slow_method

    object.slow_method.must_equal 2
    object.slow_method_with_args([1]).must_equal 1
    object.slow_method_with_args('text').must_equal 2
  end

  it 'Clear cache for specific method with arguments' do
    object = Cachable.new

    3.times { object.slow_method.must_equal 1 }
    3.times { object.slow_method_with_args([1]).must_equal 1 }
    3.times { object.slow_method_with_args('text').must_equal 2 }

    object.clear_cache :slow_method_with_args

    object.slow_method.must_equal 1
    object.slow_method_with_args([1]).must_equal 3
    object.slow_method_with_args('text').must_equal 4
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
caching-0.0.5 spec/caching_spec.rb
caching-0.0.4 spec/caching_spec.rb
caching-0.0.3 spec/caching_spec.rb