Skip to content

Commit

Permalink
Remove auto-detection of Rails / test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Feb 9, 2015
1 parent 19c38a6 commit 1900071
Show file tree
Hide file tree
Showing 37 changed files with 718 additions and 163 deletions.
30 changes: 12 additions & 18 deletions lib/shoulda/matchers.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
require 'shoulda/matchers/assertion_error'
require 'shoulda/matchers/configuration'
require 'shoulda/matchers/doublespeak'
require 'shoulda/matchers/error'
require 'shoulda/matchers/independent'
require 'shoulda/matchers/integrations'
require 'shoulda/matchers/matcher_context'
require 'shoulda/matchers/rails_shim'
require 'shoulda/matchers/util'
require 'shoulda/matchers/version'
require 'shoulda/matchers/warn'

require 'shoulda/matchers/independent'
require 'shoulda/matchers/action_controller'
require 'shoulda/matchers/active_model'
require 'shoulda/matchers/active_record'

if defined?(ActiveModel)
require 'shoulda/matchers/active_model'
module Shoulda
module Matchers
class << self
attr_accessor :assertion_exception_class
end
end
end

if defined?(ActiveRecord)
require 'shoulda/matchers/active_record'
end

if defined?(ActionController)
require 'shoulda/matchers/action_controller'
end

if defined?(RSpec)
require 'shoulda/matchers/integrations/rspec'
end

require 'shoulda/matchers/integrations/test_unit'
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def redirects_to_url?
@context.__send__(:assert_redirected_to, url)
@failure_message_when_negated = "Didn't expect to redirect to #{url}"
true
rescue Shoulda::Matchers::AssertionError => error
rescue Shoulda::Matchers.assertion_exception_class => error
@failure_message = error.message
false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def renders_template?
@context.__send__(:assert_template, @options, @message)
@failure_message_when_negated = "Didn't expect to render #{@template}"
true
rescue Shoulda::Matchers::AssertionError => error
rescue Shoulda::Matchers.assertion_exception_class => error
@failure_message = error.message
false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/action_controller/route_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def route_recognized?
rescue ::ActionController::RoutingError => error
@failure_message = error.message
false
rescue Shoulda::Matchers::AssertionError => error
rescue Shoulda::Matchers.assertion_exception_class => error
@failure_message = error.message
false
end
Expand Down
27 changes: 0 additions & 27 deletions lib/shoulda/matchers/assertion_error.rb

This file was deleted.

20 changes: 20 additions & 0 deletions lib/shoulda/matchers/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Shoulda
module Matchers
# @private
def self.configure
yield configuration
end

# @private
def self.configuration
@_configuration ||= Configuration.new
end

# @private
class Configuration
def integrate(&block)
Integrations::Configuration.apply(self, &block)
end
end
end
end
42 changes: 42 additions & 0 deletions lib/shoulda/matchers/integrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Shoulda
module Matchers
module Integrations
class << self
def register_library(klass, name)
library_registry.register(klass, name)
end

def find_library!(name)
library_registry.find!(name)
end

def register_test_framework(klass, name)
test_framework_registry.register(klass, name)
end

def find_test_framework!(name)
test_framework_registry.find!(name)
end

private

def library_registry
@_library_registry ||= Registry.new
end

def test_framework_registry
@_test_framework_registry ||= Registry.new
end
end
end
end
end

require 'shoulda/matchers/integrations/configuration'
require 'shoulda/matchers/integrations/configuration_error'
require 'shoulda/matchers/integrations/inclusion'
require 'shoulda/matchers/integrations/rails'
require 'shoulda/matchers/integrations/registry'

require 'shoulda/matchers/integrations/libraries'
require 'shoulda/matchers/integrations/test_frameworks'
67 changes: 67 additions & 0 deletions lib/shoulda/matchers/integrations/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'set'

module Shoulda
module Matchers
module Integrations
# @private
class Configuration
def self.apply(configuration, &block)
new(configuration, &block).apply
end

def initialize(configuration, &block)
@test_frameworks = Set.new

test_framework :missing_test_framework
library :missing_library

block.call(self)
end

def test_framework(name)
clear_default_test_framework
@test_frameworks << Integrations.find_test_framework!(name)
end

def library(name)
@library = Integrations.find_library!(name)
end

def apply
if no_test_frameworks_added? && library_not_set?
raise ConfigurationError, <<EOT
shoulda-matchers is not configured correctly. You need to specify a test
framework and/or library. For example:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
EOT
end

@test_frameworks.each do |test_framework|
test_framework.include(Shoulda::Matchers::Independent)
@library.integrate_with(test_framework)
end
end

private

def clear_default_test_framework
@test_frameworks.select!(&:present?)
end

def no_test_frameworks_added?
@test_frameworks.empty? || !@test_frameworks.any?(&:present?)
end

def library_not_set?
@library.nil?
end
end
end
end
end
8 changes: 8 additions & 0 deletions lib/shoulda/matchers/integrations/configuration_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Shoulda
module Matchers
module Integrations
class ConfigurationError < StandardError
end
end
end
end
19 changes: 19 additions & 0 deletions lib/shoulda/matchers/integrations/inclusion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Shoulda
module Matchers
module Integrations
module Inclusion
def include_into(mod, *other_mods, &block)
mods_to_include = other_mods.dup
mods_to_extend = other_mods.dup

if block
mods_to_include << Module.new(&block)
end

mod.__send__(:include, *mods_to_include)
mod.extend(*mods_to_extend)
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/shoulda/matchers/integrations/libraries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'shoulda/matchers/integrations/libraries/action_controller'
require 'shoulda/matchers/integrations/libraries/active_model'
require 'shoulda/matchers/integrations/libraries/active_record'
require 'shoulda/matchers/integrations/libraries/missing_library'
require 'shoulda/matchers/integrations/libraries/rails'
30 changes: 30 additions & 0 deletions lib/shoulda/matchers/integrations/libraries/action_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Shoulda
module Matchers
module Integrations
module Libraries
class ActionController
Integrations.register_library(self, :action_controller)

include Integrations::Inclusion
include Integrations::Rails

def integrate_with(test_framework)
test_framework.include(matchers_module)

include_into(::ActionController::TestCase, matchers_module) do
def subject
@controller
end
end
end

private

def matchers_module
Shoulda::Matchers::ActionController
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/shoulda/matchers/integrations/libraries/active_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Shoulda
module Matchers
module Integrations
module Libraries
class ActiveModel
Integrations.register_library(self, :active_model)

include Integrations::Inclusion
include Integrations::Rails

def integrate_with(test_framework)
test_framework.include(matchers_module)
include_into(ActiveSupport::TestCase, matchers_module)
end

private

def matchers_module
Shoulda::Matchers::ActiveModel
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/shoulda/matchers/integrations/libraries/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Shoulda
module Matchers
module Integrations
module Libraries
class ActiveRecord
Integrations.register_library(self, :active_record)

include Integrations::Inclusion
include Integrations::Rails

def integrate_with(test_framework)
test_framework.include(matchers_module)
include_into(ActiveSupport::TestCase, matchers_module)
end

private

def matchers_module
Shoulda::Matchers::ActiveRecord
end
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/shoulda/matchers/integrations/libraries/missing_library.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Shoulda
module Matchers
module Integrations
module Libraries
class MissingLibrary
Integrations.register_library(self, :missing_library)

def integrate_with(test_framework)
end

def rails?
false
end
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/shoulda/matchers/integrations/libraries/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Shoulda
module Matchers
module Integrations
module Libraries
class Rails
Integrations.register_library(self, :rails)

include Integrations::Rails

SUB_LIBRARIES = [
:active_model,
:active_record,
:action_controller
]

def integrate_with(test_framework)
Shoulda::Matchers.assertion_exception_class =
ActiveSupport::TestCase::Assertion

SUB_LIBRARIES.each do |name|
library = Integrations.find_library!(name)
library.integrate_with(test_framework)
end
end
end
end
end
end
end
Loading

0 comments on commit 1900071

Please sign in to comment.