From 2fc9f3f7f987a1ddb90bcfa797fdc59bbaa78b16 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 19 Aug 2009 23:30:06 -0400 Subject: [PATCH] Move adapters to webrat/adapters, and Rails and Merb integration code into webrat/integrations --- lib/webrat.rb | 6 +++++ lib/webrat/{ => adapters}/mechanize.rb | 0 lib/webrat/adapters/merb.rb | 11 +++++++++ lib/webrat/{ => adapters}/rack.rb | 0 lib/webrat/{ => adapters}/rails.rb | 14 ++--------- lib/webrat/{ => adapters}/sinatra.rb | 2 -- lib/webrat/core/configuration.rb | 12 ++++------ lib/webrat/core/logging.rb | 2 +- lib/webrat/integrations/merb.rb | 10 ++++++++ lib/webrat/integrations/rails.rb | 9 +++++++ lib/webrat/merb_adapter.rb | 24 ------------------- lib/webrat/selenium/selenium_session.rb | 2 ++ spec/private/core/configuration_spec.rb | 18 -------------- .../mechanize/mechanize_adapter_spec.rb | 2 -- spec/private/rails/rails_adapter_spec.rb | 2 -- spec/spec_helper.rb | 2 ++ 16 files changed, 48 insertions(+), 68 deletions(-) rename lib/webrat/{ => adapters}/mechanize.rb (100%) create mode 100644 lib/webrat/adapters/merb.rb rename lib/webrat/{ => adapters}/rack.rb (100%) rename lib/webrat/{ => adapters}/rails.rb (91%) rename lib/webrat/{ => adapters}/sinatra.rb (91%) create mode 100644 lib/webrat/integrations/merb.rb create mode 100644 lib/webrat/integrations/rails.rb delete mode 100644 lib/webrat/merb_adapter.rb diff --git a/lib/webrat.rb b/lib/webrat.rb index bd823de..bc31233 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -2,6 +2,12 @@ require "rack" require "nokogiri" module Webrat + autoload :MechanizeAdapter, "webrat/adapters/mechanize" + autoload :MerbAdapter, "webrat/adapters/merb" + autoload :RackAdapter, "webrat/adapters/rack" + autoload :RailsAdapter, "webrat/adapters/rails" + autoload :SinatraAdapter, "webrat/adapters/sinatra" + # The common base class for all exceptions raised by Webrat. class WebratError < StandardError end diff --git a/lib/webrat/mechanize.rb b/lib/webrat/adapters/mechanize.rb similarity index 100% rename from lib/webrat/mechanize.rb rename to lib/webrat/adapters/mechanize.rb diff --git a/lib/webrat/adapters/merb.rb b/lib/webrat/adapters/merb.rb new file mode 100644 index 0000000..a8af8bb --- /dev/null +++ b/lib/webrat/adapters/merb.rb @@ -0,0 +1,11 @@ +require "webrat/integrations/merb" + +module Webrat + class MerbAdapter < RackAdapter #:nodoc: + def initialize(context=nil) + app = context.respond_to?(:app) ? + context.app : Merb::Rack::Application.new + super(Rack::Test::Session.new(Rack::MockSession.new(app, "www.example.com"))) + end + end +end diff --git a/lib/webrat/rack.rb b/lib/webrat/adapters/rack.rb similarity index 100% rename from lib/webrat/rack.rb rename to lib/webrat/adapters/rack.rb diff --git a/lib/webrat/rails.rb b/lib/webrat/adapters/rails.rb similarity index 91% rename from lib/webrat/rails.rb rename to lib/webrat/adapters/rails.rb index 2687e62..21cc48f 100644 --- a/lib/webrat/rails.rb +++ b/lib/webrat/adapters/rails.rb @@ -1,7 +1,4 @@ -require "webrat" - -require "action_controller" -require "action_controller/integration" +require "webrat/integrations/rails" require "action_controller/record_identifier" module Webrat @@ -96,11 +93,4 @@ module Webrat integration_session.response end end -end - -module ActionController #:nodoc: - IntegrationTest.class_eval do - include Webrat::Methods - include Webrat::Matchers - end -end +end \ No newline at end of file diff --git a/lib/webrat/sinatra.rb b/lib/webrat/adapters/sinatra.rb similarity index 91% rename from lib/webrat/sinatra.rb rename to lib/webrat/adapters/sinatra.rb index 1ca82de..70c1185 100644 --- a/lib/webrat/sinatra.rb +++ b/lib/webrat/adapters/sinatra.rb @@ -1,5 +1,3 @@ -require "webrat/rack" - module Webrat class SinatraAdapter < RackAdapter def initialize(context) diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index f969373..98bb674 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -79,13 +79,11 @@ module Webrat def mode=(mode) @mode = mode.to_sym - # This is a temporary hack to support backwards compatibility - # with Merb 1.0.8 until it's updated to use the new Webrat.configure - # syntax - if @mode == :merb - require("webrat/merb_adapter") - else - require("webrat/#{mode}") + begin + require("webrat/integrations/#{mode}") + rescue LoadError + # Only some modes have integration code that needs to + # be loaded, so this is OK end end diff --git a/lib/webrat/core/logging.rb b/lib/webrat/core/logging.rb index 7a91c4d..7868c95 100644 --- a/lib/webrat/core/logging.rb +++ b/lib/webrat/core/logging.rb @@ -11,7 +11,7 @@ module Webrat when :rails defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : nil when :merb - Merb.logger + ::Merb.logger else @logger ||= begin require "logger" diff --git a/lib/webrat/integrations/merb.rb b/lib/webrat/integrations/merb.rb new file mode 100644 index 0000000..50e75ff --- /dev/null +++ b/lib/webrat/integrations/merb.rb @@ -0,0 +1,10 @@ +module Merb #:nodoc: + module Test #:nodoc: + module RequestHelper #:nodoc: + def request(uri, env = {}) + @_webrat_session ||= Webrat::MerbAdapter.new + @_webrat_session.response = @_webrat_session.request(uri, env) + end + end + end +end \ No newline at end of file diff --git a/lib/webrat/integrations/rails.rb b/lib/webrat/integrations/rails.rb new file mode 100644 index 0000000..aec2aee --- /dev/null +++ b/lib/webrat/integrations/rails.rb @@ -0,0 +1,9 @@ +require "action_controller" +require "action_controller/integration" + +module ActionController #:nodoc: + IntegrationTest.class_eval do + include Webrat::Methods + include Webrat::Matchers + end +end diff --git a/lib/webrat/merb_adapter.rb b/lib/webrat/merb_adapter.rb deleted file mode 100644 index 50633c9..0000000 --- a/lib/webrat/merb_adapter.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "webrat" -require "merb-core" -require "webrat/rack" - -module Webrat - class MerbAdapter < RackAdapter #:nodoc: - def initialize(context=nil) - app = context.respond_to?(:app) ? - context.app : Merb::Rack::Application.new - super(Rack::Test::Session.new(Rack::MockSession.new(app, "www.example.com"))) - end - end -end - -module Merb #:nodoc: - module Test #:nodoc: - module RequestHelper #:nodoc: - def request(uri, env = {}) - @_webrat_session ||= Webrat::MerbAdapter.new - @_webrat_session.response = @_webrat_session.request(uri, env) - end - end - end -end diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index e8b70e8..cf4ef46 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -3,6 +3,8 @@ require "webrat/selenium/selenium_rc_server" require "webrat/selenium/application_server_factory" require "webrat/selenium/application_servers/base" +require "selenium" + module Webrat class TimeoutError < WebratError end diff --git a/spec/private/core/configuration_spec.rb b/spec/private/core/configuration_spec.rb index 7db3f51..165ce4f 100755 --- a/spec/private/core/configuration_spec.rb +++ b/spec/private/core/configuration_spec.rb @@ -45,24 +45,6 @@ describe Webrat::Configuration do config.selenium_server_port.should == 1234 end - [:rails, - :selenium, - :rack, - :sinatra, - :mechanize].each do |mode| - it "should require correct lib when in #{mode} mode" do - config = Webrat::Configuration.new - config.should_receive(:require).with("webrat/#{mode}") - config.mode = mode - end - end - - it "should require merb_adapter when in merb mode" do - config = Webrat::Configuration.new - config.should_receive(:require).with("webrat/merb_adapter") - config.mode = :merb - end - describe "Selenium" do before :each do @config = Webrat::Configuration.new diff --git a/spec/private/mechanize/mechanize_adapter_spec.rb b/spec/private/mechanize/mechanize_adapter_spec.rb index 0a4074c..fc620d6 100644 --- a/spec/private/mechanize/mechanize_adapter_spec.rb +++ b/spec/private/mechanize/mechanize_adapter_spec.rb @@ -1,7 +1,5 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') -require "webrat/mechanize" - describe Webrat::MechanizeAdapter do before :each do Webrat.configuration.mode = :mechanize diff --git a/spec/private/rails/rails_adapter_spec.rb b/spec/private/rails/rails_adapter_spec.rb index 3b5cb46..c57ea58 100644 --- a/spec/private/rails/rails_adapter_spec.rb +++ b/spec/private/rails/rails_adapter_spec.rb @@ -1,7 +1,5 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') -require "webrat/rails" - describe Webrat::RailsAdapter do before :each do Webrat.configuration.mode = :rails diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 760a75d..b30de1c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -47,6 +47,8 @@ Spec::Runner.configure do |config| end end +require "merb-core" + Webrat.configure do |config| config.mode = :merb end