forked from sinatra/sinatra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextensions_test.rb
100 lines (83 loc) · 2.71 KB
/
extensions_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require File.expand_path('../helper', __FILE__)
class ExtensionsTest < Test::Unit::TestCase
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
module PainExtensions
def foo=(name); end
def bar?(name); end
def fizz!(name); end
end
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)
Sinatra::Application.register BarExtensions
assert Sinatra::Application.respond_to?(:bar)
assert Sinatra::Application.respond_to?(:foo)
assert !Sinatra::Base.respond_to?(:bar)
end
it 'allows extending by passing a block' do
Sinatra::Base.register {
def im_in_ur_anonymous_module; end
}
assert Sinatra::Base.respond_to?(:im_in_ur_anonymous_module)
end
it 'will make sure any public methods added via Application#register are delegated to Sinatra::Delegator' do
Sinatra::Application.register FooExtensions
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)
end
it 'will handle special method names' do
Sinatra::Application.register PainExtensions
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
it 'will not delegate methods on Base#register' do
Sinatra::Base.register QuuxExtensions
assert !Sinatra::Delegator.private_instance_methods.include?("quux")
end
it 'will extend the Sinatra::Application application by default' do
Sinatra.register BazExtensions
assert !Sinatra::Base.respond_to?(:baz)
assert Sinatra::Application.respond_to?(:baz)
end
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
end