Added a swanky DSL

This commit is contained in:
Jonas Nicklas 2009-11-12 17:51:31 +01:00
parent c344a92387
commit 15c4519244
2 changed files with 140 additions and 0 deletions

41
lib/webcat/dsl.rb Normal file
View File

@ -0,0 +1,41 @@
module Webcat
extend(self)
attr_writer :default_driver, :current_driver
attr_accessor :app
def default_driver
@default_driver || :rack_test
end
def current_driver
@current_driver || default_driver
end
alias_method :mode, :current_driver
def use_default_driver
@current_driver = nil
end
def current_session
session_pool["#{current_driver}#{app.object_id}"] ||= Webcat::Session.new(current_driver, app)
end
SESSION_METHODS = [
:visit, :body, :click_link, :click_button, :fill_in, :choose,
:set_hidden_field, :check, :uncheck, :attach_file, :select
]
SESSION_METHODS.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{method}(*args, &block)
current_session.#{method}(*args, &block)
end
RUBY
end
private
def session_pool
@session_pool ||= {}
end
end

99
spec/dsl_spec.rb Normal file
View File

@ -0,0 +1,99 @@
require File.expand_path('spec_helper', File.dirname(__FILE__))
require 'webcat/dsl'
describe Webcat do
before do
Webcat.app = TestApp
end
after do
Webcat.default_driver = nil
Webcat.use_default_driver
end
describe '#default_driver' do
it "should default to rack_test" do
Webcat.default_driver.should == :rack_test
end
it "should be changeable" do
Webcat.default_driver = :culerity
Webcat.default_driver.should == :culerity
end
end
describe '#current_driver' do
it "should default to the default driver" do
Webcat.current_driver.should == :rack_test
Webcat.default_driver = :culerity
Webcat.current_driver.should == :culerity
end
it "should be changeable" do
Webcat.current_driver = :culerity
Webcat.current_driver.should == :culerity
end
end
describe '#use_default_driver' do
it "should restore the default driver" do
Webcat.current_driver = :culerity
Webcat.use_default_driver
Webcat.current_driver.should == :rack_test
end
end
describe '#app' do
it "should be changeable" do
Webcat.app = "foobar"
Webcat.app.should == 'foobar'
end
end
describe '#current_session' do
it "should choose a session object of the current driver type" do
Webcat.current_session.should be_a(Webcat::Session)
end
it "should use #app as the application" do
Webcat.app = proc {}
Webcat.current_session.app.should == Webcat.app
end
it "should change with the current driver" do
Webcat.current_session.mode.should == :rack_test
Webcat.current_driver = :culerity
Webcat.current_session.mode.should == :culerity
end
it "should be persistent even across driver changes" do
object_id = Webcat.current_session.object_id
Webcat.current_session.object_id.should == object_id
Webcat.current_driver = :culerity
Webcat.current_session.mode.should == :culerity
Webcat.current_session.object_id.should_not == object_id
Webcat.current_driver = :rack_test
Webcat.current_session.object_id.should == object_id
end
it "should change when changing application" do
object_id = Webcat.current_session.object_id
Webcat.current_session.object_id.should == object_id
Webcat.app = proc {}
Webcat.current_session.object_id.should_not == object_id
Webcat.current_session.app.should == Webcat.app
end
end
describe 'the DSL' do
before do
@session = Webcat
end
it_should_behave_like "session"
end
end