Skip to content

Commit

Permalink
Using configuration in Webrat instead of defined? checks
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 23, 2008
1 parent 1017fdf commit f6ce5bb
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
12 changes: 10 additions & 2 deletions lib/webrat/core/configuration.rb
Expand Up @@ -12,18 +12,26 @@ def self.configuration
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
15 changes: 6 additions & 9 deletions lib/webrat/core/field.rb
Expand Up @@ -74,16 +74,13 @@ def raise_error_if_disabled
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

Expand Down
14 changes: 8 additions & 6 deletions lib/webrat/core/form.rb
Expand Up @@ -106,10 +106,6 @@ def form_method
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|
Expand Down Expand Up @@ -142,8 +138,14 @@ def merge_hash_values(a, b) # :nodoc:

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

Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/matchers/have_xpath.rb
Expand Up @@ -11,7 +11,7 @@ def initialize(expected, &block)
end

def matches?(stringlike)
if defined?(Nokogiri::XML)
if Webrat.configuration.parse_with_nokogiri?
matches_nokogiri?(stringlike)
else
matches_rexml?(stringlike)
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/session.rb
Expand Up @@ -106,7 +106,7 @@ def request_page(url, http_method, data) #:nodoc:
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
Expand Down
4 changes: 2 additions & 2 deletions lib/webrat/core/xml.rb
Expand Up @@ -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)
Expand All @@ -22,7 +22,7 @@ def self.document(stringlike)
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|
Expand Down
11 changes: 7 additions & 4 deletions 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
Expand All @@ -16,18 +19,18 @@
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
Expand All @@ -36,6 +39,6 @@
end

config = Webrat.configuration
config.open_error_files.should == false
config.should_not open_error_files
end
end

0 comments on commit f6ce5bb

Please sign in to comment.