require 'test/unit' require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment") $LOAD_PATH << File.dirname(__FILE__) + '/../lib/' require 'file_cache' FileCache.config.update(:default_dir => File.dirname(__FILE__) + "/../tmp") class FileCacheTest < Test::Unit::TestCase def setup #FileUtils.rm Dir.glob(File.dirname(__FILE__) + '/../tmp/**') end def create_cache_item(options) text = 'cached item' end def test_get text = FileCache.get {|opt| create_cache_item(opt)} assert_equal 'cached item', text end def test_get_filename filename = FileCache.get_filename {|opt| create_cache_item(opt)} assert File.exists?(filename) end def test_get_filename_custom_filename filename = FileCache.get_filename(:filename => 'my.cache') {|opt| create_cache_item(opt)} assert_equal File.join(FileCache.config[:default_dir], 'my.cache'), filename end def test_get_object arr = [1,2,3] value = FileCache.get(:filename => 'arr.cache') {|opt| arr } assert_equal value, arr value = FileCache.get(:filename => 'arr.cache') {|opt| arr } assert_equal value, arr end def test_get_filename_with_prefix filename = FileCache.get_filename(:prefix => 'prefix_') {|opt| create_cache_item(opt)} assert filename =~ %r{/prefix_} end def test_filename options = {:ext => '.gif'} puts FileCache.send('filename', options) FileCache.file_exists?(options) puts YAML::dump(options) puts FileCache.send('filename', options) end end