From 2811a089bc2e7b4cb9b5122ecdd3141fddeeedd6 Mon Sep 17 00:00:00 2001 From: gaffo Date: Mon, 8 Dec 2008 23:19:44 -0600 Subject: [PATCH] webrat config now does the require --- README.rdoc | 5 ++++- lib/webrat/core/configuration.rb | 16 ++++++++++++++++ lib/webrat/core/field.rb | 4 ++-- lib/webrat/core/form.rb | 4 ++-- lib/webrat/core/logging.rb | 4 ++-- lib/webrat/core/session.rb | 12 ++++++------ lib/webrat/mechanize.rb | 2 -- lib/webrat/merb.rb | 3 --- lib/webrat/rack.rb | 2 -- lib/webrat/rails.rb | 4 +--- lib/webrat/selenium.rb | 2 -- lib/webrat/sinatra.rb | 2 -- spec/spec_helper.rb | 7 +++++++ spec/webrat/core/configuration_spec.rb | 15 ++++++++++++++- spec/webrat/mechanize/mechanize_session_spec.rb | 1 + spec/webrat/rails/helper.rb | 1 + 16 files changed, 56 insertions(+), 28 deletions(-) diff --git a/README.rdoc b/README.rdoc index d9416a1..367244d 100644 --- a/README.rdoc +++ b/README.rdoc @@ -52,7 +52,10 @@ To install the latest code as a plugin: (_Note:_ This may be less stable than us In your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber) add: - require "webrat/rails" + require "webrat" + Webrat.configure do |config| + config.mode = Webrat::Configuration::RAILS_MODE + end == Install with Merb diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index 13eb195..f94f28e 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -12,6 +12,14 @@ module Webrat end class Configuration + + RAILS_MODE = :rails + SELENIUM_MODE = :selenium + RACK_MODE = :rack + SINATRA_MODE = :sinatra + MECHANIZE_MODE = :mechanize + MERB_MODE = :merb + # Should XHTML be parsed with Nokogiri? Defaults to true, except on JRuby. When false, Hpricot and REXML are used attr_writer :parse_with_nokogiri @@ -34,6 +42,14 @@ module Webrat @open_error_files ? true : false end + # Allows setting of webrat's mode, valid modes are: + # RAILS_MODE - Used in typical rails development + # SELENIUM_MODE - Used for webrat in selenium mode + def mode=(mode) + @mode = mode + require("webrat/#{mode}") + end + end end \ No newline at end of file diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 75c0d96..5ab0ddf 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -80,9 +80,9 @@ module Webrat return nil if disabled? case Webrat.configuration.mode - when :rails + when Webrat::Configuration::RAILS_MODE ActionController::AbstractRequest.parse_query_parameters("#{name}=#{escaped_value}") - when :merb + when Webrat::Configuration::MERB_MODE ::Merb::Parse.query("#{name}=#{escaped_value}") else { name => escaped_value } diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 1a9eea4..682e1d1 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -144,9 +144,9 @@ module Webrat klasses = [Hash] case Webrat.configuration.mode - when :rails + when Webrat::Configuration::RAILS_MODE klasses << HashWithIndifferentAccess - when :merb + when Webrat::Configuration::MERB_MODE klasses << Mash end diff --git a/lib/webrat/core/logging.rb b/lib/webrat/core/logging.rb index 817548e..6e791ab 100644 --- a/lib/webrat/core/logging.rb +++ b/lib/webrat/core/logging.rb @@ -8,9 +8,9 @@ module Webrat def logger # :nodoc: case Webrat.configuration.mode - when :rails + when Webrat::Configuration::RAILS_MODE defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : nil - when :merb + when Webrat::Configuration::MERB_MODE Merb.logger else nil diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 33e00fb..be6a55c 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -10,17 +10,17 @@ module Webrat def self.session_class case Webrat.configuration.mode - when :rails + when Webrat::Configuration::RAILS_MODE RailsSession - when :merb + when Webrat::Configuration::MERB_MODE MerbSession - when :selenium + when Webrat::Configuration::SELENIUM_MODE SeleniumSession - when :rack + when Webrat::Configuration::RACK_MODE RackSession - when :sinatra + when Webrat::Configuration::SINATRA_MODE SinatraSession - when :mechanize + when Webrat::Configuration::MECHANIZE_MODE MechanizeSession else raise WebratError.new("Unknown Webrat mode: #{Webrat.configuration.mode.inspect}") diff --git a/lib/webrat/mechanize.rb b/lib/webrat/mechanize.rb index e87ec30..6f1a656 100644 --- a/lib/webrat/mechanize.rb +++ b/lib/webrat/mechanize.rb @@ -39,5 +39,3 @@ module Webrat #:nodoc: end end - -Webrat.configuration.mode = :mechanize \ No newline at end of file diff --git a/lib/webrat/merb.rb b/lib/webrat/merb.rb index 35258e9..5ace8b5 100644 --- a/lib/webrat/merb.rb +++ b/lib/webrat/merb.rb @@ -72,6 +72,3 @@ class Merb::Test::RspecStory #:nodoc: @browser ||= Webrat::MerbSession.new end end - -Webrat.configuration.mode = :merb - diff --git a/lib/webrat/rack.rb b/lib/webrat/rack.rb index 6f7cf81..b537afb 100644 --- a/lib/webrat/rack.rb +++ b/lib/webrat/rack.rb @@ -22,5 +22,3 @@ module Webrat end end end - -Webrat.configuration.mode = :rack \ No newline at end of file diff --git a/lib/webrat/rails.rb b/lib/webrat/rails.rb index ec2c37e..2a89508 100644 --- a/lib/webrat/rails.rb +++ b/lib/webrat/rails.rb @@ -80,6 +80,4 @@ module ActionController #:nodoc: include Webrat::Methods end end -end - -Webrat.configuration.mode = :rails \ No newline at end of file +end \ No newline at end of file diff --git a/lib/webrat/selenium.rb b/lib/webrat/selenium.rb index fe61f34..7472f7f 100644 --- a/lib/webrat/selenium.rb +++ b/lib/webrat/selenium.rb @@ -3,8 +3,6 @@ gem "selenium-client", ">=1.2.9" require "selenium/client" require "webrat/selenium/selenium_session" -Webrat.configuration.mode = :selenium - module Webrat def self.with_selenium_server #:nodoc: diff --git a/lib/webrat/sinatra.rb b/lib/webrat/sinatra.rb index 400cc29..5f2c8df 100644 --- a/lib/webrat/sinatra.rb +++ b/lib/webrat/sinatra.rb @@ -17,5 +17,3 @@ module Webrat end end - -Webrat.configuration.mode = :sinatra \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 23900b3..79e3a17 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ Spec::Runner.configure do |config| def with_html(html) webrat_session.response_body = html end + end module Webrat @@ -30,4 +31,10 @@ module Webrat def self.reset_for_test @@configuration = @@previous_config if @@previous_config end + + class Configuration + def mode_for_test= (mode) + @mode = mode + end + end end \ No newline at end of file diff --git a/spec/webrat/core/configuration_spec.rb b/spec/webrat/core/configuration_spec.rb index edb39b1..70f34d1 100755 --- a/spec/webrat/core/configuration_spec.rb +++ b/spec/webrat/core/configuration_spec.rb @@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") describe Webrat::Configuration do predicate_matchers[:parse_with_nokogiri] = :parse_with_nokogiri? predicate_matchers[:open_error_files] = :open_error_files? - + before do Webrat.cache_config_for_test end @@ -41,4 +41,17 @@ describe Webrat::Configuration do config = Webrat.configuration config.should_not open_error_files end + + [Webrat::Configuration::RAILS_MODE, + Webrat::Configuration::SELENIUM_MODE, + Webrat::Configuration::RACK_MODE, + Webrat::Configuration::SINATRA_MODE, + Webrat::Configuration::MERB_MODE, + Webrat::Configuration::MECHANIZE_MODE].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 end \ No newline at end of file diff --git a/spec/webrat/mechanize/mechanize_session_spec.rb b/spec/webrat/mechanize/mechanize_session_spec.rb index 81d234e..c0d5df5 100644 --- a/spec/webrat/mechanize/mechanize_session_spec.rb +++ b/spec/webrat/mechanize/mechanize_session_spec.rb @@ -3,6 +3,7 @@ require "mechanize" require "webrat/mechanize" describe Webrat::MechanizeSession do + before(:each) do @mech = Webrat::MechanizeSession.new end diff --git a/spec/webrat/rails/helper.rb b/spec/webrat/rails/helper.rb index 5bf8af6..9704a41 100644 --- a/spec/webrat/rails/helper.rb +++ b/spec/webrat/rails/helper.rb @@ -7,3 +7,4 @@ silence_warnings do end require "webrat/rails" +Webrat.configuration.mode_for_test = Webrat::Configuration::RAILS_MODE