Sha256: 99fae671110dbe94494cb6c9427004412026c3cd588f418fe95952c4bb0612b8

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

require 'spec'

dir = File.dirname __FILE__
require "#{dir}/spec_require"
require "#{dir}/../lib/ruby_ext/should"
require "#{dir}/../lib/ruby_ext/declarative_cache"

describe 'DeclarativeCache' do
	class CachedClass
		attr_accessor :value
		def value_get; @value end
		cache! :value_get
		
		attr_accessor :value2
		def value2_get; @value2 end		
		
		attr_accessor :params
		def params_get param; @params[param] end 				
		
		attr_accessor :multiplier
		def *(arg)
		  @multiplier * arg
		end
		cache_with_params! '*'
	end						
	
	DeclarativeCache.cache! CachedClass, :value2_get
	DeclarativeCache.cache_with_params! CachedClass, :params_get	
	
	it "Simple Cache" do
		o = CachedClass.new
		o.value = 0
		o.value2 = 0		
		o.value_get.should == 0
		o.value2_get.should == 0
		
		o.value = 1
		o.value2 = 1
		o.value_get.should == 0
		o.value2_get.should == 0
	end
	
	it "should check for parameters" do
		o = CachedClass.new
		lambda{o.value_get(1)}.should raise_error(AssertionError)
	end
	
	it "Cache With Params" do
		o = CachedClass.new
		o.params = {:a => :b}
		o.params_get(:a).should == :b
		o.params = {:a => :c}
		o.params_get(:a).should == :b
	end
	
	it "should works with operators" do
	  o = CachedClass.new
		o.multiplier = 2
		(o * 2).should == 4
		o.multiplier = 3
		(o * 2).should == 4
	end
	
	class CachedClass2
		class << self
			attr_accessor :value
			def value_get; @value end 								
		end
	end				
	
	DeclarativeCache.cache! CachedClass2.singleton_class, :value_get				
	
	it "Simple Cache" do
		CachedClass2.value = 0
		CachedClass2.value_get.should == 0
		CachedClass2.value = 1
		CachedClass2.value_get.should == 0
	end
	
	module CachedModule
	  attr_accessor :value
		def value_get; @value end
		cache! :value_get
	end
	
	class CachedClass3
	  include CachedModule
	end
	
	it "should also works with modules (from error)" do
		o = CachedClass.new
		o.value = 0
		o.value_get.should == 0
		
		o.value = 1
		o.value_get.should == 0
	end
	
	
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-ext-0.2.16 spec/declarative_cache_spec.rb
ruby-ext-0.2.15 spec/declarative_cache_spec.rb