Skip to content

Commit

Permalink
[#33 state:resolved] refactored to move the config down to Webrat::Co…
Browse files Browse the repository at this point in the history
…re as it seemed nicer
  • Loading branch information
gaffo committed Nov 14, 2008
1 parent 3fbf5ae commit 58cbf49
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 69 deletions.
37 changes: 36 additions & 1 deletion lib/webrat/core.rb
@@ -1,4 +1,3 @@
require "webrat/core/configuration"
require "webrat/core/xml"
require "webrat/core/nokogiri"
require "webrat/core/logging"
Expand All @@ -12,3 +11,39 @@
require "webrat/core/session"
require "webrat/core/methods"
require "webrat/core/matchers"

module Webrat
class Core

# Configures Webrat. If this is not done, Webrat will be created
# with all of the default settings.
# Example:
# Webrat::Core.configure do |config|
# config.open_error_files = false
# end
def self.configure(configuration = Core.new)
yield configuration if block_given?
@@configuration = configuration
end

def self.configuration
@@configuration = Core.new unless @@configuration
@@configuration
end

# Sets whether to save and open pages with error status codes in a browser
attr_accessor :open_error_files

def initialize
self.open_error_files = default_open_error_files
end

private
@@configuration = nil

def default_open_error_files
true
end

end
end
33 changes: 0 additions & 33 deletions lib/webrat/core/configuration.rb
@@ -1,33 +0,0 @@
module Webrat
module Core
class Configuration

# Configures Webrat. If this is not done, Webrat will be created
# with all of the default settings.
def self.configure(configuration = Webrat::Core::Configuration.new)
yield configuration if block_given?
@@configuration = configuration
end

def self.configuration
@@configuration = Webrat::Core::Configuration.new unless @@configuration
@@configuration
end

# Sets whether to save and open pages with error status codes in a browser
attr_accessor :open_error_files

def initialize
self.open_error_files = default_open_error_files
end

private
@@configuration = nil

def default_open_error_files
true
end

end
end
end
2 changes: 1 addition & 1 deletion lib/webrat/core/session.rb
Expand Up @@ -86,7 +86,7 @@ def request_page(url, http_method, data) #:nodoc:
send "#{http_method}", url, data || {}, h
end

save_and_open_page if exception_caught? && !Webrat::Core::Configuration.configuration.open_error_files
save_and_open_page if exception_caught? && Webrat::Core.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 spec/spec_helper.rb
Expand Up @@ -12,11 +12,11 @@
end


class Webrat::Core::Configuration
class Webrat::Core
@@previous_config = nil

def self.cache_config_for_test
@@configuration = Webrat::Core::Configuration.configuration
@@configuration = Webrat::Core.configuration
end

def self.reset_for_test
Expand Down
30 changes: 0 additions & 30 deletions spec/webrat/core/configuration_spec.rb

This file was deleted.

7 changes: 5 additions & 2 deletions spec/webrat/core/session_spec.rb
Expand Up @@ -87,11 +87,11 @@ def session.response_body

describe "#request_page" do
before(:each) do
Webrat::Core::Configuration.cache_config_for_test
Webrat::Core.cache_config_for_test
@session = Webrat::Session.new
end
after(:each) do
Webrat::Core::Configuration.reset_for_test
Webrat::Core.reset_for_test
end

it "should raise an error if the request is not a success" do
Expand All @@ -105,6 +105,9 @@ def session.response_body
end

it "should raise an error but not open if the request is not a success and config quashes save_and_open" do
Webrat::Core.configure do |config|
config.open_error_files = false
end
@session.stub!(:get)
@session.stub!(:response_body).and_return("Exception caught")
@session.stub!(:response_code).and_return(500)
Expand Down
30 changes: 30 additions & 0 deletions spec/webrat/core_spec.rb
@@ -0,0 +1,30 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")

describe Webrat::Core do

before do
Webrat::Core.cache_config_for_test
end

after do
Webrat::Core.reset_for_test
end

it "should have a default config" do
Webrat::Core.configuration.should be_an_instance_of(Webrat::Core)
end

it "should set default values" do
config = Webrat::Core.configuration
config.open_error_files.should == true
end

it "should be configurable with a block" do
Webrat::Core.configure do |config|
config.open_error_files = false
end
config = Webrat::Core.configuration
config.open_error_files.should == false
end

end

0 comments on commit 58cbf49

Please sign in to comment.