Using configuration in Webrat instead of defined? checks

This commit is contained in:
Bryan Helmkamp 2008-11-23 14:59:07 -05:00
parent 1017fdfba7
commit f6ce5bbfb1
7 changed files with 35 additions and 25 deletions

View File

@ -12,18 +12,26 @@ module Webrat
end end
class Configuration class Configuration
attr_accessor :parse_with_nokogiri attr_writer :parse_with_nokogiri
attr_accessor :mode attr_accessor :mode
# Sets whether to save and open pages with error status codes in a browser # 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 def initialize
self.open_error_files = true self.open_error_files = true
self.parse_with_nokogiri = !Webrat.on_java? self.parse_with_nokogiri = !Webrat.on_java?
end end
def parse_with_nokogiri?
@parse_with_nokogiri ? true : false
end
def open_error_files?
@open_error_files ? true : false
end
end end
end end

View File

@ -74,16 +74,13 @@ module Webrat
def to_param def to_param
return nil if disabled? return nil if disabled?
key_and_value = "#{name}=#{escaped_value}" case Webrat.configuration.mode
when :rails
if defined?(CGIMethods) ActionController::AbstractRequest.parse_query_parameters("#{name}=#{escaped_value}")
CGIMethods.parse_query_parameters(key_and_value) when :merb
elsif defined?(ActionController::AbstractRequest) ::Merb::Parse.query("#{name}=#{escaped_value}")
ActionController::AbstractRequest.parse_query_parameters(key_and_value)
elsif defined?(::Merb)
::Merb::Parse.query(key_and_value)
else else
{ name => escaped_value } # For mechanize { name => escaped_value }
end end
end end

View File

@ -106,10 +106,6 @@ module Webrat
def form_action def form_action
@element["action"].blank? ? @session.current_url : @element["action"] @element["action"].blank? ? @session.current_url : @element["action"]
end end
HASH = [Hash]
HASH << HashWithIndifferentAccess rescue nil # Rails
HASH << Mash rescue nil # Merb
def merge(all_params, new_param) def merge(all_params, new_param)
new_param.each do |key, value| new_param.each do |key, value|
@ -142,8 +138,14 @@ module Webrat
def hash_classes def hash_classes
klasses = [Hash] 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 klasses
end end

View File

@ -11,7 +11,7 @@ module Webrat
end end
def matches?(stringlike) def matches?(stringlike)
if defined?(Nokogiri::XML) if Webrat.configuration.parse_with_nokogiri?
matches_nokogiri?(stringlike) matches_nokogiri?(stringlike)
else else
matches_rexml?(stringlike) matches_rexml?(stringlike)

View File

@ -106,7 +106,7 @@ module Webrat
send "#{http_method}", url, data || {}, h send "#{http_method}", url, data || {}, h
end 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? raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
@_scopes = nil @_scopes = nil

View File

@ -2,7 +2,7 @@ module Webrat
module XML module XML
def self.document(stringlike) def self.document(stringlike)
if defined?(Nokogiri::XML) if Webrat.configuration.parse_with_nokogiri?
Webrat.nokogiri_document(stringlike) Webrat.nokogiri_document(stringlike)
else else
return stringlike.dom if stringlike.respond_to?(:dom) return stringlike.dom if stringlike.respond_to?(:dom)
@ -22,7 +22,7 @@ module Webrat
end end
def self.css_search(element, *searches) def self.css_search(element, *searches)
if defined?(Nokogiri::XML) if Webrat.configuration.parse_with_nokogiri?
element.css(*searches) element.css(*searches)
else else
searches.map do |search| searches.map do |search|

View File

@ -1,6 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe Webrat::Configuration do describe Webrat::Configuration do
predicate_matchers[:parse_with_nokogiri] = :parse_with_nokogiri?
predicate_matchers[:open_error_files] = :open_error_files?
before do before do
Webrat.cache_config_for_test Webrat.cache_config_for_test
end end
@ -16,18 +19,18 @@ describe Webrat::Configuration do
it "should use Nokogiri as the parser by default" do it "should use Nokogiri as the parser by default" do
Webrat.stub!(:on_java? => false) Webrat.stub!(:on_java? => false)
config = Webrat::Configuration.new config = Webrat::Configuration.new
config.parse_with_nokogiri.should == true config.should parse_with_nokogiri
end end
it "should not use Nokogiri as the parser when on JRuby" do it "should not use Nokogiri as the parser when on JRuby" do
Webrat.stub!(:on_java? => true) Webrat.stub!(:on_java? => true)
config = Webrat::Configuration.new config = Webrat::Configuration.new
config.parse_with_nokogiri.should == false config.should_not parse_with_nokogiri
end end
it "should open error files by default" do it "should open error files by default" do
config = Webrat::Configuration.new config = Webrat::Configuration.new
config.open_error_files.should == true config.should open_error_files
end end
it "should be configurable with a block" do it "should be configurable with a block" do
@ -36,6 +39,6 @@ describe Webrat::Configuration do
end end
config = Webrat.configuration config = Webrat.configuration
config.open_error_files.should == false config.should_not open_error_files
end end
end end