2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2009-01-28 11:30:10 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class ExtensionsTest < Minitest::Test
|
2009-01-28 11:30:10 -05:00
|
|
|
module FooExtensions
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def im_hiding_in_ur_foos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module BarExtensions
|
|
|
|
def bar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module BazExtensions
|
|
|
|
def baz
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module QuuxExtensions
|
|
|
|
def quux
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-05 01:12:28 -05:00
|
|
|
module PainExtensions
|
|
|
|
def foo=(name); end
|
|
|
|
def bar?(name); end
|
|
|
|
def fizz!(name); end
|
|
|
|
end
|
|
|
|
|
2009-01-28 11:30:10 -05:00
|
|
|
it 'will add the methods to the DSL for the class in which you register them and its subclasses' do
|
|
|
|
Sinatra::Base.register FooExtensions
|
|
|
|
assert Sinatra::Base.respond_to?(:foo)
|
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
Sinatra::Application.register BarExtensions
|
|
|
|
assert Sinatra::Application.respond_to?(:bar)
|
|
|
|
assert Sinatra::Application.respond_to?(:foo)
|
2009-01-28 11:30:10 -05:00
|
|
|
assert !Sinatra::Base.respond_to?(:bar)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows extending by passing a block' do
|
2012-05-21 17:21:59 -04:00
|
|
|
Sinatra::Base.register { def im_in_ur_anonymous_module; end }
|
2009-01-28 11:30:10 -05:00
|
|
|
assert Sinatra::Base.respond_to?(:im_in_ur_anonymous_module)
|
|
|
|
end
|
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'will make sure any public methods added via Application#register are delegated to Sinatra::Delegator' do
|
|
|
|
Sinatra::Application.register FooExtensions
|
2009-02-03 19:02:41 -05:00
|
|
|
assert Sinatra::Delegator.private_instance_methods.
|
|
|
|
map { |m| m.to_sym }.include?(:foo)
|
|
|
|
assert !Sinatra::Delegator.private_instance_methods.
|
|
|
|
map { |m| m.to_sym }.include?(:im_hiding_in_ur_foos)
|
2009-01-28 11:30:10 -05:00
|
|
|
end
|
|
|
|
|
2009-03-05 01:12:28 -05:00
|
|
|
it 'will handle special method names' do
|
2009-10-14 07:01:06 -04:00
|
|
|
Sinatra::Application.register PainExtensions
|
2009-03-05 01:12:28 -05:00
|
|
|
assert Sinatra::Delegator.private_instance_methods.
|
|
|
|
map { |m| m.to_sym }.include?(:foo=)
|
|
|
|
assert Sinatra::Delegator.private_instance_methods.
|
|
|
|
map { |m| m.to_sym }.include?(:bar?)
|
|
|
|
assert Sinatra::Delegator.private_instance_methods.
|
|
|
|
map { |m| m.to_sym }.include?(:fizz!)
|
|
|
|
end
|
|
|
|
|
2009-01-28 11:30:10 -05:00
|
|
|
it 'will not delegate methods on Base#register' do
|
|
|
|
Sinatra::Base.register QuuxExtensions
|
|
|
|
assert !Sinatra::Delegator.private_instance_methods.include?("quux")
|
|
|
|
end
|
|
|
|
|
2009-10-14 07:01:06 -04:00
|
|
|
it 'will extend the Sinatra::Application application by default' do
|
2009-01-28 11:30:10 -05:00
|
|
|
Sinatra.register BazExtensions
|
|
|
|
assert !Sinatra::Base.respond_to?(:baz)
|
2009-10-14 07:01:06 -04:00
|
|
|
assert Sinatra::Application.respond_to?(:baz)
|
2009-01-28 11:30:10 -05:00
|
|
|
end
|
2009-02-22 04:51:39 -05:00
|
|
|
|
|
|
|
module BizzleExtension
|
|
|
|
def bizzle
|
|
|
|
bizzle_option
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.registered(base)
|
|
|
|
fail "base should be BizzleApp" unless base == BizzleApp
|
|
|
|
fail "base should have already extended BizzleExtension" unless base.respond_to?(:bizzle)
|
|
|
|
base.set :bizzle_option, 'bizzle!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BizzleApp < Sinatra::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends .registered to the extension module after extending the class' do
|
|
|
|
BizzleApp.register BizzleExtension
|
|
|
|
assert_equal 'bizzle!', BizzleApp.bizzle_option
|
|
|
|
assert_equal 'bizzle!', BizzleApp.bizzle
|
|
|
|
end
|
2009-01-28 11:30:10 -05:00
|
|
|
end
|