From 0db9e8b03ffa8e52d36d2f57384e5a09a6d10790 Mon Sep 17 00:00:00 2001 From: kengos Date: Wed, 9 Jan 2013 23:34:00 +0900 Subject: [PATCH 1/9] Update gemspec --- rspec_caching_test.gemspec | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rspec_caching_test.gemspec b/rspec_caching_test.gemspec index cb6ef36..759567e 100644 --- a/rspec_caching_test.gemspec +++ b/rspec_caching_test.gemspec @@ -9,9 +9,7 @@ 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 "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 +17,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 From e623ed258279ea0f951a5b80d95e2b86d824039a Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 00:43:05 +0900 Subject: [PATCH 2/9] wip caching_test_store --- Gemfile | 11 ++- Guardfile | 9 ++ LICENSE | 22 +++++ lib/rspec_caching_test/caching_test_store.rb | 93 ++++++++++++++++++++ spec/spec_helper.rb | 0 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 Guardfile create mode 100644 LICENSE create mode 100644 lib/rspec_caching_test/caching_test_store.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index 3ecb7fe..86d38ff 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,13 @@ 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 '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..973e1ea --- /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 do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{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/caching_test_store.rb b/lib/rspec_caching_test/caching_test_store.rb new file mode 100644 index 0000000..3d47367 --- /dev/null +++ b/lib/rspec_caching_test/caching_test_store.rb @@ -0,0 +1,93 @@ +# coding: utf-8 + +module RspecCachingTest + class CachingTestStore < ::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/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e69de29 From dd446c989c49f9848ae0ffaa5db3c432aae6a5d4 Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 01:11:12 +0900 Subject: [PATCH 3/9] do not work --- Guardfile | 2 +- lib/rspec_caching_test.rb | 17 ++++++++++- lib/rspec_caching_test/matchers.rb | 6 ++++ .../{caching_test_store.rb => test_store.rb} | 2 +- log/development.log | 0 rspec_caching_test.gemspec | 1 + spec/rspec_caching_test/test_store_spec.rb | 9 ++++++ spec/spec_helper.rb | 29 +++++++++++++++++++ tmp/rspec_guard_result | 1 + 9 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 lib/rspec_caching_test/matchers.rb rename lib/rspec_caching_test/{caching_test_store.rb => test_store.rb} (97%) create mode 100644 log/development.log create mode 100644 spec/rspec_caching_test/test_store_spec.rb create mode 100644 tmp/rspec_guard_result diff --git a/Guardfile b/Guardfile index 973e1ea..4df3a3a 100644 --- a/Guardfile +++ b/Guardfile @@ -1,7 +1,7 @@ # 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 do +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/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } diff --git a/lib/rspec_caching_test.rb b/lib/rspec_caching_test.rb index 5f80def..da98a74 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/machers" + +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/caching_test_store.rb b/lib/rspec_caching_test/test_store.rb similarity index 97% rename from lib/rspec_caching_test/caching_test_store.rb rename to lib/rspec_caching_test/test_store.rb index 3d47367..4bbc39d 100644 --- a/lib/rspec_caching_test/caching_test_store.rb +++ b/lib/rspec_caching_test/test_store.rb @@ -1,7 +1,7 @@ # coding: utf-8 module RspecCachingTest - class CachingTestStore < ::ActiveSupport::Cache::Store + class TestStore < ::ActiveSupport::Cache::Store # Record of what the app tells us to cache attr_accessor :cached diff --git a/log/development.log b/log/development.log new file mode 100644 index 0000000..e69de29 diff --git a/rspec_caching_test.gemspec b/rspec_caching_test.gemspec index 759567e..ac4ee97 100644 --- a/rspec_caching_test.gemspec +++ b/rspec_caching_test.gemspec @@ -9,6 +9,7 @@ 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.add_dependency "activesupport", '>= 3.0.0' s.add_dependency "rspec-rails", '>= 2.0.0' s.files = `git ls-files`.split("\n") 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..d7093a0 --- /dev/null +++ b/spec/rspec_caching_test/test_store_spec.rb @@ -0,0 +1,9 @@ +# coding: utf-8 + +require 'spec_helper' + +describe RspecCachingTest::TestStore do + describe '#initialize' do + it { describe_class.new.hit be_kind_of Hash } + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e69de29..1633b38 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -0,0 +1,29 @@ +# coding: utf-8 + +require 'rails' + +module RspecCachingTest + class Application < Rails::Application + config.active_support.deprecation :notify + end +end +RspecCachingTest::Application.initialize! + +require 'active_support/railtie' +require 'action_controller/railtie' +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.use_transactional_fixtures = false + 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 diff --git a/tmp/rspec_guard_result b/tmp/rspec_guard_result new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tmp/rspec_guard_result @@ -0,0 +1 @@ + From 2ce246e5bddf4dba65c795af9ccbb676fa704374 Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 01:14:34 +0900 Subject: [PATCH 4/9] modified .gitignore --- .gitignore | 2 ++ log/development.log | 0 tmp/rspec_guard_result | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) delete mode 100644 log/development.log delete mode 100644 tmp/rspec_guard_result 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/log/development.log b/log/development.log deleted file mode 100644 index e69de29..0000000 diff --git a/tmp/rspec_guard_result b/tmp/rspec_guard_result deleted file mode 100644 index 8b13789..0000000 --- a/tmp/rspec_guard_result +++ /dev/null @@ -1 +0,0 @@ - From 9fc6e5b952dbf8c31cca142690e7098ce7c0779e Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 01:27:42 +0900 Subject: [PATCH 5/9] poosible to run spec --- Gemfile | 1 + lib/rspec_caching_test.rb | 4 ++-- spec/rspec_caching_test/test_store_spec.rb | 4 ++-- spec/spec_helper.rb | 13 +++++-------- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index 86d38ff..86d841a 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ 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 diff --git a/lib/rspec_caching_test.rb b/lib/rspec_caching_test.rb index da98a74..8de4161 100644 --- a/lib/rspec_caching_test.rb +++ b/lib/rspec_caching_test.rb @@ -2,7 +2,7 @@ require "rspec_caching_test/version" require "rspec_caching_test/test_store" -require "rspec_caching_test/machers" +require "rspec_caching_test/matchers" module RspecCachingTest class << self @@ -10,7 +10,7 @@ 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 + include ::RspecCachingTest::Matchers end end end diff --git a/spec/rspec_caching_test/test_store_spec.rb b/spec/rspec_caching_test/test_store_spec.rb index d7093a0..226afa0 100644 --- a/spec/rspec_caching_test/test_store_spec.rb +++ b/spec/rspec_caching_test/test_store_spec.rb @@ -2,8 +2,8 @@ require 'spec_helper' -describe RspecCachingTest::TestStore do +describe ::RspecCachingTest::TestStore do describe '#initialize' do - it { describe_class.new.hit be_kind_of Hash } + it { described_class.new.hit.should be_kind_of Hash } end end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1633b38..657e617 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,16 +1,14 @@ # coding: utf-8 +require 'tzinfo' +require 'action_controller/railtie' -require 'rails' - -module RspecCachingTest +module RspecCachingApplication class Application < Rails::Application - config.active_support.deprecation :notify + config.active_support.deprecation :log end end -RspecCachingTest::Application.initialize! +RspecCachingApplication::Application.initialize! -require 'active_support/railtie' -require 'action_controller/railtie' require 'rspec' require 'rspec/rails' @@ -18,7 +16,6 @@ class Application < Rails::Application RSpec.configure do |config| config.mock_with :rspec - config.use_transactional_fixtures = false config.treat_symbols_as_metadata_keys_with_true_values = true config.filter_run focus: true config.run_all_when_everything_filtered = true From 84a646708a5d2f47d52ad3580735f520b7aed876 Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 01:32:36 +0900 Subject: [PATCH 6/9] fix --- Guardfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guardfile b/Guardfile index 4df3a3a..73a3899 100644 --- a/Guardfile +++ b/Guardfile @@ -3,7 +3,7 @@ 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/lib/#{m[1]}_spec.rb" } + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end From 23ecd21cd6ea86ce2f353d8cc27b76c3556f51db Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 01:32:59 +0900 Subject: [PATCH 7/9] add active_support.deprecation = :notify --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 657e617..407b6e1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,7 +4,7 @@ module RspecCachingApplication class Application < Rails::Application - config.active_support.deprecation :log + config.active_support.deprecation = :notify end end RspecCachingApplication::Application.initialize! From 991f15e80488407939210293ce0c8d9e0c6fcc9b Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 10 Jan 2013 01:34:02 +0900 Subject: [PATCH 8/9] RspecCachingApplication to RspecCachingTest --- spec/spec_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 407b6e1..8b4ae3e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,12 +2,12 @@ require 'tzinfo' require 'action_controller/railtie' -module RspecCachingApplication +module RspecCachingTest class Application < Rails::Application config.active_support.deprecation = :notify end end -RspecCachingApplication::Application.initialize! +RspecCachingTest::Application.initialize! require 'rspec' require 'rspec/rails' From b88eec205d3e77c905b2466098f9ae51cc5fce3b Mon Sep 17 00:00:00 2001 From: kengos Date: Fri, 11 Jan 2013 00:03:18 +0900 Subject: [PATCH 9/9] add spec --- spec/rspec_caching_test/test_store_spec.rb | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spec/rspec_caching_test/test_store_spec.rb b/spec/rspec_caching_test/test_store_spec.rb index 226afa0..149abf5 100644 --- a/spec/rspec_caching_test/test_store_spec.rb +++ b/spec/rspec_caching_test/test_store_spec.rb @@ -6,4 +6,39 @@ 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