Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.bundle
Gemfile.lock
pkg/*
log
tmp
12 changes: 11 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
source "http://rubygems.org"

# Specify your gem's dependencies in rspec_caching_test.gemspec
gemspec
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
9 changes: 9 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -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

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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
17 changes: 16 additions & 1 deletion lib/rspec_caching_test.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# coding: utf-8

require "rspec_caching_test/version"
require "rspec_caching_test/cache_test"
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
6 changes: 6 additions & 0 deletions lib/rspec_caching_test/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# coding: utf-8

module RspecCachingTest
module Matchers
end
end
93 changes: 93 additions & 0 deletions lib/rspec_caching_test/test_store.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions rspec_caching_test.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ 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")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
# s.add_runtime_dependency "rest-client"
end
44 changes: 44 additions & 0 deletions spec/rspec_caching_test/test_store_spec.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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