rtomayko-sinatra 0.8.10 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sinatra/test.rb CHANGED
@@ -1,114 +1,110 @@
1
1
  require 'sinatra/base'
2
+ require 'test/unit'
2
3
 
3
- module Sinatra
4
-
5
- module Test
6
- include Rack::Utils
7
-
8
- attr_reader :app, :request, :response
9
-
10
- def test_request(verb, path, *args)
11
- @app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
12
- fail "@app not set - cannot make request" if @app.nil?
13
- @request = Rack::MockRequest.new(@app)
14
- opts, input =
15
- case args.size
16
- when 2 # input, env
17
- input, env = args
18
- if input.kind_of?(Hash) # params, env
19
- [env, param_string(input)]
20
- else
21
- [env, input]
22
- end
23
- when 1 # params
24
- if (data = args.first).kind_of?(Hash)
25
- env = (data.delete(:env) || {})
26
- [env, param_string(data)]
27
- else
28
- [{}, data]
29
- end
30
- when 0
31
- [{}, '']
4
+ module Sinatra::Test
5
+ include Rack::Utils
6
+
7
+ attr_reader :app, :request, :response
8
+
9
+ def mock_app(base=Sinatra::Base, &block)
10
+ @app = Sinatra.new(base, &block)
11
+ end
12
+
13
+ def request(verb, path, *args)
14
+ fail "@app not set - cannot make request" if @app.nil?
15
+ @request = Rack::MockRequest.new(@app)
16
+ opts, input =
17
+ case args.size
18
+ when 2 # input, env
19
+ input, env = args
20
+ if input.kind_of?(Hash) # params, env
21
+ [env, param_string(input)]
32
22
  else
33
- raise ArgumentError, "zero, one, or two arguments expected"
23
+ [env, input]
34
24
  end
35
- opts = rack_opts(opts)
36
- opts[:input] ||= input
37
- yield @request if block_given?
38
- @response = @request.request(verb, path, opts)
39
- end
25
+ when 1 # params
26
+ if (data = args.first).kind_of?(Hash)
27
+ env = (data.delete(:env) || {})
28
+ [env, param_string(data)]
29
+ else
30
+ [{}, data]
31
+ end
32
+ when 0
33
+ [{}, '']
34
+ else
35
+ raise ArgumentError, "zero, one, or two arguments expected"
36
+ end
37
+ opts = rack_opts(opts)
38
+ opts[:input] ||= input
39
+ yield @request if block_given?
40
+ @response = @request.request(verb, path, opts)
41
+ end
40
42
 
41
- def get(path, *args, &b) ; test_request('GET', path, *args, &b) ; end
42
- def head(path, *args, &b) ; test_request('HEAD', path, *args, &b) ; end
43
- def post(path, *args, &b) ; test_request('POST', path, *args, &b) ; end
44
- def put(path, *args, &b) ; test_request('PUT', path, *args, &b) ; end
45
- def delete(path, *args, &b) ; test_request('DELETE', path, *args, &b) ; end
43
+ def get(path, *args, &b) ; request('GET', path, *args, &b) ; end
44
+ def head(path, *args, &b) ; request('HEAD', path, *args, &b) ; end
45
+ def post(path, *args, &b) ; request('POST', path, *args, &b) ; end
46
+ def put(path, *args, &b) ; request('PUT', path, *args, &b) ; end
47
+ def delete(path, *args, &b) ; request('DELETE', path, *args, &b) ; end
46
48
 
47
- def follow!
48
- test_request 'GET', @response.location
49
- end
49
+ def follow!
50
+ request 'GET', @response.location
51
+ end
50
52
 
51
- def body ; @response.body ; end
52
- def status ; @response.status ; end
53
+ def should
54
+ @response.should
55
+ end
53
56
 
54
- # Delegate other missing methods to @response.
55
- def method_missing(name, *args, &block)
56
- if @response && @response.respond_to?(name)
57
- @response.send(name, *args, &block)
58
- else
59
- super
60
- end
61
- end
57
+ def body
58
+ @response.body
59
+ end
62
60
 
63
- # Also check @response since we delegate there.
64
- def respond_to?(symbol, include_private=false)
65
- super || (@response && @response.respond_to?(symbol, include_private))
66
- end
61
+ def status
62
+ @response.status
63
+ end
67
64
 
68
- RACK_OPT_NAMES = {
69
- :accept => "HTTP_ACCEPT",
70
- :agent => "HTTP_USER_AGENT",
71
- :host => "HTTP_HOST",
72
- :session => "HTTP_COOKIE",
73
- :cookies => "HTTP_COOKIE",
74
- :content_type => "CONTENT_TYPE"
75
- }
76
-
77
- def rack_opts(opts)
78
- opts.inject({}) do |hash,(key,val)|
79
- key = RACK_OPT_NAMES[key] || key
80
- hash[key] = val
81
- hash
82
- end
65
+ RACK_OPT_NAMES = {
66
+ :accept => "HTTP_ACCEPT",
67
+ :agent => "HTTP_USER_AGENT",
68
+ :host => "HTTP_HOST",
69
+ :session => "HTTP_COOKIE",
70
+ :cookies => "HTTP_COOKIE",
71
+ :content_type => "CONTENT_TYPE"
72
+ }
73
+
74
+ def rack_opts(opts)
75
+ opts.inject({}) do |hash,(key,val)|
76
+ key = RACK_OPT_NAMES[key] || key
77
+ hash[key] = val
78
+ hash
83
79
  end
80
+ end
84
81
 
85
- def env_for(opts={})
86
- opts = rack_opts(opts)
87
- Rack::MockRequest.env_for(opts)
88
- end
82
+ def env_for(opts={})
83
+ opts = rack_opts(opts)
84
+ Rack::MockRequest.env_for(opts)
85
+ end
89
86
 
90
- def param_string(hash)
91
- hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
92
- end
87
+ def param_string(hash)
88
+ hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
89
+ end
93
90
 
94
- if defined? Sinatra::Compat
95
- # Deprecated. Use: "get" instead of "get_it".
96
- %w(get head post put delete).each do |verb|
97
- eval <<-RUBY, binding, __FILE__, __LINE__
98
- def #{verb}_it(*args, &block)
99
- sinatra_warn "The #{verb}_it method is deprecated; use #{verb} instead."
100
- test_request('#{verb.upcase}', *args, &block)
101
- end
102
- RUBY
103
- end
91
+ if defined? Sinatra::Compat
92
+ # Deprecated. Use: "get" instead of "get_it".
93
+ %w(get head post put delete).each do |verb|
94
+ alias_method "#{verb}_it", verb
95
+ remove_method verb
104
96
  end
105
- end
106
97
 
107
- class TestHarness
108
- include Test
98
+ include Sinatra::Delegator
109
99
 
110
- def initialize(app=nil)
111
- @app = app || Sinatra::Application
100
+ # Deprecated. Tests no longer delegate missing methods to the
101
+ # mock response. Use: @response
102
+ def method_missing(name, *args, &block)
103
+ if @response && @response.respond_to?(name)
104
+ @response.send(name, *args, &block)
105
+ else
106
+ super
107
+ end
112
108
  end
113
109
  end
114
110
  end
@@ -1,9 +1,2 @@
1
1
  require 'sinatra/test'
2
2
  require 'spec/interop/test'
3
-
4
- Sinatra::Default.set(
5
- :env => :test,
6
- :run => false,
7
- :raise_errors => true,
8
- :logging => false
9
- )
@@ -1,9 +1,2 @@
1
1
  require 'test/spec'
2
2
  require 'sinatra/test'
3
- require 'sinatra/test/unit'
4
-
5
- module Sinatra::Test
6
- def should
7
- @response.should
8
- end
9
- end
@@ -1,5 +1,5 @@
1
- require 'sinatra/test'
2
1
  require 'test/unit'
2
+ require 'sinatra/test'
3
3
 
4
4
  Test::Unit::TestCase.send :include, Sinatra::Test
5
5
 
data/sinatra.gemspec CHANGED
@@ -3,19 +3,17 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'sinatra'
6
- s.version = '0.8.10'
7
- s.date = '2009-01-16'
6
+ s.version = '0.9.0'
7
+ s.date = '2009-01-06'
8
8
 
9
9
  s.description = "Classy web-development dressed in a DSL"
10
10
  s.summary = "Classy web-development dressed in a DSL"
11
11
 
12
12
  s.authors = ["Blake Mizerany"]
13
- s.email = "sinatrarb@googlegroups.com"
14
13
 
15
14
  # = MANIFEST =
16
15
  s.files = %w[
17
- AUTHORS
18
- CHANGES
16
+ ChangeLog
19
17
  LICENSE
20
18
  README.rdoc
21
19
  Rakefile
@@ -59,8 +57,8 @@ Gem::Specification.new do |s|
59
57
  lib/sinatra/images/404.png
60
58
  lib/sinatra/images/500.png
61
59
  lib/sinatra/main.rb
60
+ lib/sinatra/rack/methodoverride.rb
62
61
  lib/sinatra/test.rb
63
- lib/sinatra/test/bacon.rb
64
62
  lib/sinatra/test/rspec.rb
65
63
  lib/sinatra/test/spec.rb
66
64
  lib/sinatra/test/unit.rb
@@ -71,7 +69,6 @@ Gem::Specification.new do |s|
71
69
  test/erb_test.rb
72
70
  test/filter_test.rb
73
71
  test/haml_test.rb
74
- test/helper.rb
75
72
  test/helpers_test.rb
76
73
  test/mapped_error_test.rb
77
74
  test/middleware_test.rb
@@ -99,7 +96,7 @@ Gem::Specification.new do |s|
99
96
  s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/}
100
97
 
101
98
  s.extra_rdoc_files = %w[README.rdoc LICENSE]
102
- s.add_dependency 'rack', '>= 0.9.1'
99
+ s.add_dependency 'rack', '>= 0.9.0'
103
100
 
104
101
  s.has_rdoc = true
105
102
  s.homepage = "http://sinatra.rubyforge.org"
data/test/base_test.rb CHANGED
@@ -1,8 +1,12 @@
1
- require File.dirname(__FILE__) + '/helper'
1
+ require 'test/spec'
2
+ require 'sinatra/base'
3
+ require 'sinatra/test'
2
4
 
3
5
  describe 'Sinatra::Base' do
6
+ include Sinatra::Test
7
+
4
8
  it 'includes Rack::Utils' do
5
- assert Sinatra::Base.included_modules.include?(Rack::Utils)
9
+ Sinatra::Base.should.include Rack::Utils
6
10
  end
7
11
 
8
12
  it 'can be used as a Rack application' do
@@ -11,12 +15,12 @@ describe 'Sinatra::Base' do
11
15
  'Hello World'
12
16
  end
13
17
  }
14
- assert @app.respond_to?(:call)
18
+ @app.should.respond_to :call
15
19
 
16
20
  request = Rack::MockRequest.new(@app)
17
21
  response = request.get('/')
18
- assert response.ok?
19
- assert_equal 'Hello World', response.body
22
+ response.should.be.ok
23
+ response.body.should.equal 'Hello World'
20
24
  end
21
25
 
22
26
  it 'can be used as Rack middleware' do
@@ -31,38 +35,15 @@ describe 'Sinatra::Base' do
31
35
  end
32
36
  }
33
37
  middleware = mock_middleware.new(app)
34
- assert_same app, middleware.app
38
+ middleware.app.should.be app
35
39
 
36
40
  request = Rack::MockRequest.new(middleware)
37
41
  response = request.get('/')
38
- assert response.ok?
39
- assert_equal 'Hello World', response.body
42
+ response.should.be.ok
43
+ response.body.should.equal 'Hello World'
40
44
 
41
45
  response = request.get('/goodbye')
42
- assert response.ok?
43
- assert_equal 'Goodbye World', response.body
44
- end
45
-
46
- it 'can take multiple definitions of a route' do
47
- app = mock_app {
48
- user_agent(/Foo/)
49
- get '/foo' do
50
- 'foo'
51
- end
52
-
53
- get '/foo' do
54
- 'not foo'
55
- end
56
- }
57
-
58
- request = Rack::MockRequest.new(app)
59
- response = request.get('/foo', 'HTTP_USER_AGENT' => 'Foo')
60
- assert response.ok?
61
- assert_equal 'foo', response.body
62
-
63
- request = Rack::MockRequest.new(app)
64
- response = request.get('/foo')
65
- assert response.ok?
66
- assert_equal 'not foo', response.body
46
+ response.should.be.ok
47
+ response.body.should.equal 'Goodbye World'
67
48
  end
68
49
  end
data/test/builder_test.rb CHANGED
@@ -1,6 +1,10 @@
1
- require File.dirname(__FILE__) + '/helper'
1
+ require 'test/spec'
2
+ require 'sinatra/base'
3
+ require 'sinatra/test'
2
4
 
3
5
  describe "Builder Templates" do
6
+ include Sinatra::Test
7
+
4
8
  def builder_app(&block)
5
9
  mock_app {
6
10
  set :views, File.dirname(__FILE__) + '/views'
@@ -11,8 +15,8 @@ describe "Builder Templates" do
11
15
 
12
16
  it 'renders inline Builder strings' do
13
17
  builder_app { builder 'xml.instruct!' }
14
- assert ok?
15
- assert_equal %{<?xml version="1.0" encoding="UTF-8"?>\n}, body
18
+ should.be.ok
19
+ body.should.equal %{<?xml version="1.0" encoding="UTF-8"?>\n}
16
20
  end
17
21
 
18
22
  it 'renders inline blocks' do
@@ -22,8 +26,8 @@ describe "Builder Templates" do
22
26
  xml.couple @name
23
27
  end
24
28
  }
25
- assert ok?
26
- assert_equal "<couple>Frank &amp; Mary</couple>\n", body
29
+ should.be.ok
30
+ body.should.equal "<couple>Frank &amp; Mary</couple>\n"
27
31
  end
28
32
 
29
33
  it 'renders .builder files in views path' do
@@ -31,8 +35,8 @@ describe "Builder Templates" do
31
35
  @name = "Blue"
32
36
  builder :hello
33
37
  }
34
- assert ok?
35
- assert_equal %(<exclaim>You're my boy, Blue!</exclaim>\n), body
38
+ should.be.ok
39
+ body.should.equal %(<exclaim>You're my boy, Blue!</exclaim>\n)
36
40
  end
37
41
 
38
42
  it "renders with inline layouts" do
@@ -43,22 +47,22 @@ describe "Builder Templates" do
43
47
  get('/') { builder %(xml.em 'Hello World') }
44
48
  }
45
49
  get '/'
46
- assert ok?
47
- assert_equal "<layout>\n<em>Hello World</em>\n</layout>\n", body
50
+ should.be.ok
51
+ body.should.equal "<layout>\n<em>Hello World</em>\n</layout>\n"
48
52
  end
49
53
 
50
54
  it "renders with file layouts" do
51
55
  builder_app {
52
56
  builder %(xml.em 'Hello World'), :layout => :layout2
53
57
  }
54
- assert ok?
55
- assert_equal "<layout>\n<em>Hello World</em>\n</layout>\n", body
58
+ should.be.ok
59
+ body.should.equal "<layout>\n<em>Hello World</em>\n</layout>\n"
56
60
  end
57
61
 
58
62
  it "raises error if template not found" do
59
63
  mock_app {
60
64
  get('/') { builder :no_such_template }
61
65
  }
62
- assert_raise(Errno::ENOENT) { get('/') }
66
+ lambda { get('/') }.should.raise(Errno::ENOENT)
63
67
  end
64
68
  end
data/test/erb_test.rb CHANGED
@@ -1,6 +1,10 @@
1
- require File.dirname(__FILE__) + '/helper'
1
+ require 'test/spec'
2
+ require 'sinatra/base'
3
+ require 'sinatra/test'
2
4
 
3
5
  describe "ERB Templates" do
6
+ include Sinatra::Test
7
+
4
8
  def erb_app(&block)
5
9
  mock_app {
6
10
  set :views, File.dirname(__FILE__) + '/views'
@@ -11,14 +15,14 @@ describe "ERB Templates" do
11
15
 
12
16
  it 'renders inline ERB strings' do
13
17
  erb_app { erb '<%= 1 + 1 %>' }
14
- assert ok?
15
- assert_equal '2', body
18
+ should.be.ok
19
+ body.should.equal '2'
16
20
  end
17
21
 
18
22
  it 'renders .erb files in views path' do
19
23
  erb_app { erb :hello }
20
- assert ok?
21
- assert_equal "Hello World\n", body
24
+ should.be.ok
25
+ body.should.equal "Hello World\n"
22
26
  end
23
27
 
24
28
  it 'takes a :locals option' do
@@ -26,8 +30,8 @@ describe "ERB Templates" do
26
30
  locals = {:foo => 'Bar'}
27
31
  erb '<%= foo %>', :locals => locals
28
32
  }
29
- assert ok?
30
- assert_equal 'Bar', body
33
+ should.be.ok
34
+ body.should.equal 'Bar'
31
35
  end
32
36
 
33
37
  it "renders with inline layouts" do
@@ -36,15 +40,16 @@ describe "ERB Templates" do
36
40
  get('/') { erb 'Sparta' }
37
41
  }
38
42
  get '/'
39
- assert ok?
40
- assert_equal 'THIS. IS. SPARTA!', body
43
+ should.be.ok
44
+ body.should.equal 'THIS. IS. SPARTA!'
41
45
  end
42
46
 
43
47
  it "renders with file layouts" do
44
48
  erb_app {
45
49
  erb 'Hello World', :layout => :layout2
46
50
  }
47
- assert ok?
48
- assert_equal "ERB Layout!\nHello World\n", body
51
+ should.be.ok
52
+ body.should.equal "ERB Layout!\nHello World\n"
49
53
  end
54
+
50
55
  end