sinatra 0.9.0.5 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ describe 'Registering extensions' do
4
+ module FooExtensions
5
+ def foo
6
+ end
7
+
8
+ private
9
+ def im_hiding_in_ur_foos
10
+ end
11
+ end
12
+
13
+ module BarExtensions
14
+ def bar
15
+ end
16
+ end
17
+
18
+ module BazExtensions
19
+ def baz
20
+ end
21
+ end
22
+
23
+ module QuuxExtensions
24
+ def quux
25
+ end
26
+ end
27
+
28
+ it 'will add the methods to the DSL for the class in which you register them and its subclasses' do
29
+ Sinatra::Base.register FooExtensions
30
+ assert Sinatra::Base.respond_to?(:foo)
31
+
32
+ Sinatra::Default.register BarExtensions
33
+ assert Sinatra::Default.respond_to?(:bar)
34
+ assert Sinatra::Default.respond_to?(:foo)
35
+ assert !Sinatra::Base.respond_to?(:bar)
36
+ end
37
+
38
+ it 'allows extending by passing a block' do
39
+ Sinatra::Base.register {
40
+ def im_in_ur_anonymous_module; end
41
+ }
42
+ assert Sinatra::Base.respond_to?(:im_in_ur_anonymous_module)
43
+ end
44
+
45
+ it 'will make sure any public methods added via Default#register are delegated to Sinatra::Delegator' do
46
+ Sinatra::Default.register FooExtensions
47
+ assert Sinatra::Delegator.private_instance_methods.
48
+ map { |m| m.to_sym }.include?(:foo)
49
+ assert !Sinatra::Delegator.private_instance_methods.
50
+ map { |m| m.to_sym }.include?(:im_hiding_in_ur_foos)
51
+ end
52
+
53
+ it 'will not delegate methods on Base#register' do
54
+ Sinatra::Base.register QuuxExtensions
55
+ assert !Sinatra::Delegator.private_instance_methods.include?("quux")
56
+ end
57
+
58
+ it 'will extend the Sinatra::Default application by default' do
59
+ Sinatra.register BazExtensions
60
+ assert !Sinatra::Base.respond_to?(:baz)
61
+ assert Sinatra::Default.respond_to?(:baz)
62
+ end
63
+
64
+ module BizzleExtension
65
+ def bizzle
66
+ bizzle_option
67
+ end
68
+
69
+ def self.registered(base)
70
+ fail "base should be BizzleApp" unless base == BizzleApp
71
+ fail "base should have already extended BizzleExtension" unless base.respond_to?(:bizzle)
72
+ base.set :bizzle_option, 'bizzle!'
73
+ end
74
+ end
75
+
76
+ class BizzleApp < Sinatra::Base
77
+ end
78
+
79
+ it 'sends .registered to the extension module after extending the class' do
80
+ BizzleApp.register BizzleExtension
81
+ assert_equal 'bizzle!', BizzleApp.bizzle_option
82
+ assert_equal 'bizzle!', BizzleApp.bizzle
83
+ end
84
+ end
data/test/helper.rb CHANGED
@@ -1,25 +1,81 @@
1
1
  begin
2
- require 'test/spec'
2
+ require 'rack'
3
3
  rescue LoadError
4
4
  require 'rubygems'
5
- require 'test/spec'
5
+ require 'rack'
6
6
  end
7
7
 
8
- $:.unshift File.dirname(File.dirname(__FILE__)) + '/lib'
9
- require 'sinatra/base'
8
+ libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
9
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
10
+
11
+ require 'test/unit'
10
12
  require 'sinatra/test'
11
- require 'sinatra/test/spec'
12
13
 
13
- module Sinatra::Test
14
+ class Sinatra::Base
15
+ # Allow assertions in request context
16
+ include Test::Unit::Assertions
17
+ end
18
+
19
+ class Test::Unit::TestCase
20
+ include Sinatra::Test
21
+
14
22
  # Sets up a Sinatra::Base subclass defined with the block
15
23
  # given. Used in setup or individual spec methods to establish
16
24
  # the application.
17
25
  def mock_app(base=Sinatra::Base, &block)
18
26
  @app = Sinatra.new(base, &block)
19
27
  end
28
+
29
+ def restore_default_options
30
+ Sinatra::Default.set(
31
+ :environment => :development,
32
+ :raise_errors => Proc.new { test? },
33
+ :dump_errors => true,
34
+ :sessions => false,
35
+ :logging => Proc.new { ! test? },
36
+ :methodoverride => true,
37
+ :static => true,
38
+ :run => Proc.new { ! test? }
39
+ )
40
+ end
20
41
  end
21
42
 
22
- class Sinatra::Base
23
- # Allow assertions in request context
24
- include Test::Unit::Assertions
43
+ ##
44
+ # test/spec/mini
45
+ # http://pastie.caboo.se/158871
46
+ # chris@ozmm.org
47
+ #
48
+ def describe(*args, &block)
49
+ return super unless (name = args.first.capitalize) && block
50
+ name = "#{name.gsub(/\W/, '')}Test"
51
+ Object.send :const_set, name, Class.new(Test::Unit::TestCase)
52
+ klass = Object.const_get(name)
53
+ klass.class_eval do
54
+ def self.it(name, &block)
55
+ define_method("test_#{name.gsub(/\W/,'_').downcase}", &block)
56
+ end
57
+ def self.xspecify(*args) end
58
+ def self.before(&block) define_method(:setup, &block) end
59
+ def self.after(&block) define_method(:teardown, &block) end
60
+ end
61
+ klass.class_eval &block
62
+ klass
63
+ end
64
+
65
+ def describe_option(name, &block)
66
+ klass = describe("Option #{name}", &block)
67
+ klass.before do
68
+ restore_default_options
69
+ @base = Sinatra.new
70
+ @default = Class.new(Sinatra::Default)
71
+ end
72
+ klass
73
+ end
74
+
75
+ # Do not output warnings for the duration of the block.
76
+ def silence_warnings
77
+ $VERBOSE, v = nil, $VERBOSE
78
+ yield
79
+ ensure
80
+ $VERBOSE = v
25
81
  end