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
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

View File

@ -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

View File

@ -107,10 +107,6 @@ module Webrat
@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|
case all_params[key]
@ -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

View File

@ -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)

View File

@ -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

View File

@ -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|

View File

@ -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