diff --git a/.gitignore b/.gitignore index 4040c6c..65134a9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .bundle Gemfile.lock pkg/* +log +tmp diff --git a/Gemfile b/Gemfile index 3ecb7fe..86d841a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,14 @@ source "http://rubygems.org" # Specify your gem's dependencies in rspec_caching_test.gemspec -gemspec \ No newline at end of file +gemspec + +group :development, :test do + gem 'guard-rspec' + gem 'fuubar' + gem 'tzinfo' + gem 'libnotify', require: false + gem 'rb-inotify', require: false + gem 'rb-fsevent', require: false + gem 'growl', require: false +end diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..73a3899 --- /dev/null +++ b/Guardfile @@ -0,0 +1,9 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +guard 'rspec', cli: "--color --profile", keep_failed: false, all_on_start: false, all_after_pass: false do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } +end + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b087474 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2012 kengos + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR diff --git a/lib/rspec_caching_test.rb b/lib/rspec_caching_test.rb index 5f80def..8de4161 100644 --- a/lib/rspec_caching_test.rb +++ b/lib/rspec_caching_test.rb @@ -1,2 +1,17 @@ +# coding: utf-8 + require "rspec_caching_test/version" -require "rspec_caching_test/cache_test" \ No newline at end of file +require "rspec_caching_test/test_store" +require "rspec_caching_test/matchers" + +module RspecCachingTest + class << self + def setup(do_read_cache = false) + Rails.configuration.action_controller.perform_caching = true + Rails.configuration.action_controller.cache_store = ::RspecCachingTest::TestStore.new(do_read_cache) + RSpec::Matchers.class_eval do + include ::RspecCachingTest::Matchers + end + end + end +end \ No newline at end of file diff --git a/lib/rspec_caching_test/matchers.rb b/lib/rspec_caching_test/matchers.rb new file mode 100644 index 0000000..e7003cf --- /dev/null +++ b/lib/rspec_caching_test/matchers.rb @@ -0,0 +1,6 @@ +# coding: utf-8 + +module RspecCachingTest + module Matchers + end +end \ No newline at end of file diff --git a/lib/rspec_caching_test/test_store.rb b/lib/rspec_caching_test/test_store.rb new file mode 100644 index 0000000..4bbc39d --- /dev/null +++ b/lib/rspec_caching_test/test_store.rb @@ -0,0 +1,93 @@ +# coding: utf-8 + +module RspecCachingTest + class TestStore < ::ActiveSupport::Cache::Store + # Record of what the app tells us to cache + attr_accessor :cached + + # Record of what the app tells us to expire + attr_accessor :expired + + # Record of what the app tells us to expire via patterns + attr_accessor :expiration_patterns + + # Cached data that could be returned + attr_accessor :data + + # Setting to enable the returning of cached data. + attr_accessor :read_cache + + attr_accessor :readed + attr_accessor :hit + + def initialize(do_read_cache = false) + @read_cache = do_read_cache + clear + super + end + + def delete_matched(matcher, options = nil) + matcher = key_matcher(matcher, options) + @data.keys.each do |key| + delete_entry(key, options) if key.match(matcher) + end + @expiration_patterns << matcher + end + + def increment(name, amount = 1, options = nil) + if num = read(name, options) + num = num.to_i + amount + write(name, num, options) + num + else + nil + end + end + + def decrement(name, amount = 1, options = nil) + if num = read(name, options) + num = num.to_i - amount + write(name, num, options) + num + else + nil + end + end + + def cleanup(options = nil) + @data.keys.each do |key| + entry = @data[key] + delete_entry(key, options) if entry && entry.expired? + end + end + + def clear(options = nil) + @data = {} + @readed = [] + @cached = [] + @expired = [] + @expiration_patterns = [] + @hit = {} + end + + protected + def read_entry(key, options) + entry = @data[key] + @readed << key + @hit[key] = @hit[key] ? @hit[key] + 1 : 1 if entry + @read_cache ? entry : nil + end + + def write_entry(key, entry, options) + @data[key] = entry + @cached << key + true + end + + def delete_entry(key, options) + entry = @data.delete(key) + @expired << key + !!entry + end + end +end \ No newline at end of file diff --git a/rspec_caching_test.gemspec b/rspec_caching_test.gemspec index cb6ef36..ac4ee97 100644 --- a/rspec_caching_test.gemspec +++ b/rspec_caching_test.gemspec @@ -9,9 +9,8 @@ Gem::Specification.new do |s| s.homepage = "https://github.com/kengos/rspec_caching_test" s.summary = %q{RSpec caching test} s.description = %q{RSpec caching test helper} - - s.rubyforge_project = "rspec_caching_test" - s.add_dependency "rails", ">=3.0.0" + s.add_dependency "activesupport", '>= 3.0.0' + s.add_dependency "rspec-rails", '>= 2.0.0' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") @@ -19,6 +18,5 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] # specify any dependencies here; for example: - # s.add_development_dependency "rspec" # s.add_runtime_dependency "rest-client" end diff --git a/spec/rspec_caching_test/test_store_spec.rb b/spec/rspec_caching_test/test_store_spec.rb new file mode 100644 index 0000000..149abf5 --- /dev/null +++ b/spec/rspec_caching_test/test_store_spec.rb @@ -0,0 +1,44 @@ +# coding: utf-8 + +require 'spec_helper' + +describe ::RspecCachingTest::TestStore do + describe '#initialize' do + it { described_class.new.hit.should be_kind_of Hash } + end + + let(:cache) { described_class.new } + describe '#write' do + before { cache.write('foo', 'bar') } + it { cache.cached.should == %w(foo) } + it { cache.data['foo'].value.should eq 'bar' } + end + + describe '#read' do + before { + cache.write('foo', 'bar') + cache.read('foo') + } + it { cache.readed.should == %w(foo) } + it { cache.hit['foo'].should eq 1 } + context 'more read' do + before { cache.read('foo') } + it { cache.readed.should == %w(foo foo) } + it { cache.hit['foo'].should == 2 } + end + + context 'no cached' do + before { cache.read('bar') } + it { cache.readed.should == %w(foo bar) } + it { cache.hit['bar'].should be_nil } + end + end + + describe '#delete' do + before { + cache.write('foo', 'bar') + cache.delete('foo').should be_true + } + it { cache.expired.should === %w(foo) } + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..8b4ae3e --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,26 @@ +# coding: utf-8 +require 'tzinfo' +require 'action_controller/railtie' + +module RspecCachingTest + class Application < Rails::Application + config.active_support.deprecation = :notify + end +end +RspecCachingTest::Application.initialize! + +require 'rspec' +require 'rspec/rails' + +require File.expand_path(File.dirname(__FILE__) + '/../lib/rspec_caching_test') + +RSpec.configure do |config| + config.mock_with :rspec + config.treat_symbols_as_metadata_keys_with_true_values = true + config.filter_run focus: true + config.run_all_when_everything_filtered = true + + config.before(:each) do + ::RspecCachingTest::setup + end +end \ No newline at end of file