From f6ce5bbfb137e0ec3182d3e41de474c81100e935 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 23 Nov 2008 14:59:07 -0500 Subject: [PATCH] Using configuration in Webrat instead of defined? checks --- lib/webrat/core/configuration.rb | 12 ++++++++++-- lib/webrat/core/field.rb | 15 ++++++--------- lib/webrat/core/form.rb | 14 ++++++++------ lib/webrat/core/matchers/have_xpath.rb | 2 +- lib/webrat/core/session.rb | 2 +- lib/webrat/core/xml.rb | 4 ++-- spec/webrat/core/configuration_spec.rb | 11 +++++++---- 7 files changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index 4276c0a..3b216e7 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -12,18 +12,26 @@ module Webrat end class Configuration - attr_accessor :parse_with_nokogiri + attr_writer :parse_with_nokogiri attr_accessor :mode # Sets whether to save and open pages with error status codes in a browser - attr_accessor :open_error_files + attr_writer :open_error_files def initialize self.open_error_files = true self.parse_with_nokogiri = !Webrat.on_java? end + def parse_with_nokogiri? + @parse_with_nokogiri ? true : false + end + + def open_error_files? + @open_error_files ? true : false + end + end end \ No newline at end of file diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 37983b2..cfffb9e 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -74,16 +74,13 @@ module Webrat def to_param return nil if disabled? - key_and_value = "#{name}=#{escaped_value}" - - if defined?(CGIMethods) - CGIMethods.parse_query_parameters(key_and_value) - elsif defined?(ActionController::AbstractRequest) - ActionController::AbstractRequest.parse_query_parameters(key_and_value) - elsif defined?(::Merb) - ::Merb::Parse.query(key_and_value) + case Webrat.configuration.mode + when :rails + ActionController::AbstractRequest.parse_query_parameters("#{name}=#{escaped_value}") + when :merb + ::Merb::Parse.query("#{name}=#{escaped_value}") else - { name => escaped_value } # For mechanize + { name => escaped_value } end end diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 29bbe1b..88a0a44 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -106,10 +106,6 @@ module Webrat def form_action @element["action"].blank? ? @session.current_url : @element["action"] end - - HASH = [Hash] - HASH << HashWithIndifferentAccess rescue nil # Rails - HASH << Mash rescue nil # Merb def merge(all_params, new_param) new_param.each do |key, value| @@ -142,8 +138,14 @@ module Webrat def hash_classes klasses = [Hash] - klasses << HashWithIndifferentAccess if defined?(HashWithIndifferentAccess) # Rails - klasses << Mash if defined?(Mash) # Merb + + case Webrat.configuration.mode + when :rails + klasses << HashWithIndifferentAccess + when :merb + klasses << Mash + end + klasses end diff --git a/lib/webrat/core/matchers/have_xpath.rb b/lib/webrat/core/matchers/have_xpath.rb index 740761a..907fde9 100644 --- a/lib/webrat/core/matchers/have_xpath.rb +++ b/lib/webrat/core/matchers/have_xpath.rb @@ -11,7 +11,7 @@ module Webrat end def matches?(stringlike) - if defined?(Nokogiri::XML) + if Webrat.configuration.parse_with_nokogiri? matches_nokogiri?(stringlike) else matches_rexml?(stringlike) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 92c33fd..8a653b9 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -106,7 +106,7 @@ module Webrat send "#{http_method}", url, data || {}, h end - save_and_open_page if exception_caught? && Webrat.configuration.open_error_files + save_and_open_page if exception_caught? && Webrat.configuration.open_error_files? raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? @_scopes = nil diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index bf576a0..e66fb69 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -2,7 +2,7 @@ module Webrat module XML def self.document(stringlike) - if defined?(Nokogiri::XML) + if Webrat.configuration.parse_with_nokogiri? Webrat.nokogiri_document(stringlike) else return stringlike.dom if stringlike.respond_to?(:dom) @@ -22,7 +22,7 @@ module Webrat end def self.css_search(element, *searches) - if defined?(Nokogiri::XML) + if Webrat.configuration.parse_with_nokogiri? element.css(*searches) else searches.map do |search| diff --git a/spec/webrat/core/configuration_spec.rb b/spec/webrat/core/configuration_spec.rb index ac7e694..edb39b1 100755 --- a/spec/webrat/core/configuration_spec.rb +++ b/spec/webrat/core/configuration_spec.rb @@ -1,6 +1,9 @@ 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 @@ -16,18 +19,18 @@ describe Webrat::Configuration do it "should use Nokogiri as the parser by default" do Webrat.stub!(:on_java? => false) config = Webrat::Configuration.new - config.parse_with_nokogiri.should == true + config.should parse_with_nokogiri end it "should not use Nokogiri as the parser when on JRuby" do Webrat.stub!(:on_java? => true) config = Webrat::Configuration.new - config.parse_with_nokogiri.should == false + config.should_not parse_with_nokogiri end it "should open error files by default" do config = Webrat::Configuration.new - config.open_error_files.should == true + config.should open_error_files end it "should be configurable with a block" do @@ -36,6 +39,6 @@ describe Webrat::Configuration do end config = Webrat.configuration - config.open_error_files.should == false + config.should_not open_error_files end end \ No newline at end of file