sinatra-sinatra 0.9.1.2 → 0.9.1.3

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.
@@ -0,0 +1,145 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ require 'sass/error'
4
+
5
+ class RenderBacktraceTest < Test::Unit::TestCase
6
+ VIEWS = File.dirname(__FILE__) + '/views'
7
+
8
+ def assert_raise_at(filename, line, exception = RuntimeError)
9
+ f, l = nil
10
+ assert_raise(exception) do
11
+ begin
12
+ get('/')
13
+ rescue => e
14
+ f, l = e.backtrace.first.split(':')
15
+ raise
16
+ end
17
+ end
18
+ assert_equal(filename, f, "expected #{exception.name} in #{filename}, was #{f}")
19
+ assert_equal(line, l.to_i, "expected #{exception.name} in #{filename} at line #{line}, was at line #{l}")
20
+ end
21
+
22
+ def backtrace_app(&block)
23
+ mock_app {
24
+ use_in_file_templates!
25
+ set :views, RenderBacktraceTest::VIEWS
26
+ template :builder_template do
27
+ 'raise "error"'
28
+ end
29
+ template :erb_template do
30
+ '<% raise "error" %>'
31
+ end
32
+ template :haml_template do
33
+ '%h1= raise "error"'
34
+ end
35
+ template :sass_template do
36
+ '+syntax-error'
37
+ end
38
+ get '/', &block
39
+ }
40
+ end
41
+
42
+ it "provides backtrace for Builder template" do
43
+ backtrace_app { builder :error }
44
+ assert_raise_at(File.join(VIEWS,'error.builder'), 2)
45
+ end
46
+
47
+ it "provides backtrace for ERB template" do
48
+ backtrace_app { erb :error }
49
+ assert_raise_at(File.join(VIEWS,'error.erb'), 2)
50
+ end
51
+
52
+ it "provides backtrace for HAML template" do
53
+ backtrace_app { haml :error }
54
+ assert_raise_at(File.join(VIEWS,'error.haml'), 2)
55
+ end
56
+
57
+ it "provides backtrace for Sass template" do
58
+ backtrace_app { sass :error }
59
+ assert_raise_at(File.join(VIEWS,'error.sass'), 2, Sass::SyntaxError)
60
+ end
61
+
62
+ it "provides backtrace for ERB template with locals" do
63
+ backtrace_app { erb :error, {}, :french => true }
64
+ assert_raise_at(File.join(VIEWS,'error.erb'), 3)
65
+ end
66
+
67
+ it "provides backtrace for HAML template with locals" do
68
+ backtrace_app { haml :error, {}, :french => true }
69
+ assert_raise_at(File.join(VIEWS,'error.haml'), 3)
70
+ end
71
+
72
+ it "provides backtrace for inline Builder string" do
73
+ backtrace_app { builder "raise 'Ack! Thbbbt!'"}
74
+ assert_raise_at(__FILE__, (__LINE__-1))
75
+ end
76
+
77
+ it "provides backtrace for inline ERB string" do
78
+ backtrace_app { erb "<% raise 'bidi-bidi-bidi' %>" }
79
+ assert_raise_at(__FILE__, (__LINE__-1))
80
+ end
81
+
82
+ it "provides backtrace for inline HAML string" do
83
+ backtrace_app { haml "%h1= raise 'Lions and tigers and bears! Oh, my!'" }
84
+ assert_raise_at(__FILE__, (__LINE__-1))
85
+ end
86
+
87
+ # it "provides backtrace for inline Sass string" do
88
+ # backtrace_app { sass '+buh-bye' }
89
+ # assert_raise_at(__FILE__, (__LINE__-1), Sass::SyntaxError)
90
+ # end
91
+
92
+ it "provides backtrace for named Builder template" do
93
+ backtrace_app { builder :builder_template }
94
+ assert_raise_at(__FILE__, (__LINE__-68))
95
+ end
96
+
97
+ it "provides backtrace for named ERB template" do
98
+ backtrace_app { erb :erb_template }
99
+ assert_raise_at(__FILE__, (__LINE__-70))
100
+ end
101
+
102
+ it "provides backtrace for named HAML template" do
103
+ backtrace_app { haml :haml_template }
104
+ assert_raise_at(__FILE__, (__LINE__-72))
105
+ end
106
+
107
+ # it "provides backtrace for named Sass template" do
108
+ # backtrace_app { sass :sass_template }
109
+ # assert_raise_at(__FILE__, (__LINE__-74), Sass::SyntaxError)
110
+ # end
111
+
112
+ it "provides backtrace for in file Builder template" do
113
+ backtrace_app { builder :builder_in_file }
114
+ assert_raise_at(__FILE__, (__LINE__+22))
115
+ end
116
+
117
+ it "provides backtrace for in file ERB template" do
118
+ backtrace_app { erb :erb_in_file }
119
+ assert_raise_at(__FILE__, (__LINE__+20))
120
+ end
121
+
122
+ it "provides backtrace for in file HAML template" do
123
+ backtrace_app { haml :haml_in_file }
124
+ assert_raise_at(__FILE__, (__LINE__+18))
125
+ end
126
+
127
+ # it "provides backtrace for in file Sass template" do
128
+ # backtrace_app { sass :sass_in_file }
129
+ # assert_raise_at(__FILE__, (__LINE__+16), Sass::SyntaxError)
130
+ # end
131
+ end
132
+
133
+ __END__
134
+
135
+ @@ builder_in_file
136
+ raise "bif"
137
+
138
+ @@ erb_in_file
139
+ <% raise "bam" %>
140
+
141
+ @@ haml_in_file
142
+ %h1= raise "pow"
143
+
144
+ @@ sass_in_file
145
+ +blam
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe 'Sinatra::Request' do
3
+ class RequestTest < Test::Unit::TestCase
4
4
  it 'responds to #user_agent' do
5
5
  request = Sinatra::Request.new({'HTTP_USER_AGENT' => 'Test'})
6
6
  assert request.respond_to?(:user_agent)
@@ -2,8 +2,8 @@
2
2
 
3
3
  require File.dirname(__FILE__) + '/helper'
4
4
 
5
- describe 'Sinatra::Response' do
6
- before do
5
+ class ResponseTest < Test::Unit::TestCase
6
+ setup do
7
7
  @response = Sinatra::Response.new
8
8
  end
9
9
 
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe 'Result Handling' do
3
+ class ResultTest < Test::Unit::TestCase
4
4
  it "sets response.body when result is a String" do
5
5
  mock_app {
6
6
  get '/' do
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ module RouteAddedTest
4
+ @routes, @procs = [], []
5
+ def self.routes ; @routes ; end
6
+ def self.procs ; @procs ; end
7
+ def self.route_added(verb, path, proc)
8
+ @routes << [verb, path]
9
+ @procs << proc
10
+ end
11
+ end
12
+
13
+ class RouteAddedHookTest < Test::Unit::TestCase
14
+ setup {
15
+ RouteAddedTest.routes.clear
16
+ RouteAddedTest.procs.clear
17
+ }
18
+
19
+ it "should be notified of an added route" do
20
+ mock_app(Class.new(Sinatra::Base)) {
21
+ register RouteAddedTest
22
+ get('/') {}
23
+ }
24
+
25
+ assert_equal [["GET", "/"], ["HEAD", "/"]],
26
+ RouteAddedTest.routes
27
+ end
28
+
29
+ it "should include hooks from superclass" do
30
+ a = Class.new(Class.new(Sinatra::Base))
31
+ b = Class.new(a)
32
+
33
+ a.register RouteAddedTest
34
+ b.class_eval { post("/sub_app_route") {} }
35
+
36
+ assert_equal [["POST", "/sub_app_route"]],
37
+ RouteAddedTest.routes
38
+ end
39
+
40
+ it "should only run once per extension" do
41
+ mock_app(Class.new(Sinatra::Base)) {
42
+ register RouteAddedTest
43
+ register RouteAddedTest
44
+ get('/') {}
45
+ }
46
+
47
+ assert_equal [["GET", "/"], ["HEAD", "/"]],
48
+ RouteAddedTest.routes
49
+ end
50
+
51
+ it "should pass route blocks as an argument" do
52
+ mock_app(Class.new(Sinatra::Base)) {
53
+ register RouteAddedTest
54
+ get('/') {}
55
+ }
56
+
57
+ assert_kind_of Proc, RouteAddedTest.procs.first
58
+ end
59
+ end
@@ -5,7 +5,7 @@ def route_def(pattern)
5
5
  mock_app { get(pattern) { } }
6
6
  end
7
7
 
8
- describe "Routing" do
8
+ class RoutingTest < Test::Unit::TestCase
9
9
  %w[get put post delete].each do |verb|
10
10
  it "defines #{verb.upcase} request handlers with #{verb}" do
11
11
  mock_app {
@@ -44,6 +44,17 @@ describe "Routing" do
44
44
  assert_equal 404, status
45
45
  end
46
46
 
47
+ it "sets the content-type to text/html in the default 404 handler" do
48
+ mock_app {
49
+ before { content_type 'text/plain' }
50
+ }
51
+
52
+ get '/foo'
53
+ assert_equal 404, status
54
+ assert_equal 'text/html', response["Content-Type"]
55
+ assert body.include?("Sinatra doesn't know this ditty")
56
+ end
57
+
47
58
  it 'takes multiple definitions of a route' do
48
59
  mock_app {
49
60
  user_agent(/Foo/)
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe "Sass Templates" do
3
+ class SassTest < Test::Unit::TestCase
4
4
  def sass_app(&block)
5
5
  mock_app {
6
6
  set :views, File.dirname(__FILE__) + '/views'
@@ -33,4 +33,46 @@ describe "Sass Templates" do
33
33
  }
34
34
  assert_raise(Errno::ENOENT) { get('/') }
35
35
  end
36
+
37
+ it "passes SASS options to the Sass engine" do
38
+ sass_app {
39
+ sass "#sass\n :background-color #FFF\n :color #000\n", :style => :compact
40
+ }
41
+ assert ok?
42
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
43
+ end
44
+
45
+ it "passes default SASS options to the Sass engine" do
46
+ mock_app {
47
+ set :sass, {:style => :compact} # default Sass style is :nested
48
+ get '/' do
49
+ sass "#sass\n :background-color #FFF\n :color #000\n"
50
+ end
51
+ }
52
+ get '/'
53
+ assert ok?
54
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
55
+ end
56
+
57
+ it "merges the default SASS options with the overrides and passes them to the Sass engine" do
58
+ mock_app {
59
+ set :sass, {:style => :compact, :attribute_syntax => :alternate } # default Sass attribute_syntax is :normal (with : in front)
60
+ get '/' do
61
+ sass "#sass\n background-color: #FFF\n color: #000\n"
62
+ end
63
+ get '/raised' do
64
+ sass "#sass\n :background-color #FFF\n :color #000\n", :style => :expanded # retains global attribute_syntax settings
65
+ end
66
+ get '/expanded_normal' do
67
+ sass "#sass\n :background-color #FFF\n :color #000\n", :style => :expanded, :attribute_syntax => :normal
68
+ end
69
+ }
70
+ get '/'
71
+ assert ok?
72
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
73
+ assert_raise(Sass::SyntaxError) { get('/raised') }
74
+ get '/expanded_normal'
75
+ assert ok?
76
+ assert_equal "#sass {\n background-color: #FFF;\n color: #000;\n}\n", body
77
+ end
36
78
  end
@@ -14,8 +14,8 @@ class Rack::Handler::Mock
14
14
  end
15
15
  end
16
16
 
17
- describe 'Sinatra::Base.run!' do
18
- before do
17
+ class ServerTest < Test::Unit::TestCase
18
+ setup do
19
19
  mock_app {
20
20
  set :server, 'mock'
21
21
  set :host, 'foo.local'
@@ -24,7 +24,9 @@ describe 'Sinatra::Base.run!' do
24
24
  $stdout = File.open('/dev/null', 'wb')
25
25
  end
26
26
 
27
- after { $stdout = STDOUT }
27
+ def teardown
28
+ $stdout = STDOUT
29
+ end
28
30
 
29
31
  it "locates the appropriate Rack handler and calls ::run" do
30
32
  @app.run!
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe 'Sinatra' do
3
+ class SinatraTest < Test::Unit::TestCase
4
4
  it 'creates a new Sinatra::Base subclass on new' do
5
5
  app =
6
6
  Sinatra.new do
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe 'Static' do
4
- before do
3
+ class StaticTest < Test::Unit::TestCase
4
+ setup do
5
5
  mock_app {
6
6
  set :static, true
7
7
  set :public, File.dirname(__FILE__)
@@ -1,9 +1,9 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe 'Templating' do
3
+ class TemplatesTest < Test::Unit::TestCase
4
4
  def render_app(&block)
5
5
  mock_app {
6
- def render_test(template, data, options, &block)
6
+ def render_test(template, data, options, locals, &block)
7
7
  inner = block ? block.call : ''
8
8
  data + inner
9
9
  end
@@ -72,11 +72,23 @@ describe 'Templating' do
72
72
  mock_app {
73
73
  use_in_file_templates!
74
74
  }
75
- assert_equal "this is foo\n\n", @app.templates[:foo]
76
- assert_equal "X\n= yield\nX\n", @app.templates[:layout]
75
+ assert_equal "this is foo\n\n", @app.templates[:foo][:template]
76
+ assert_equal "X\n= yield\nX\n", @app.templates[:layout][:template]
77
+ end
78
+
79
+ test 'use_in_file_templates simply ignores IO errors' do
80
+ assert_nothing_raised {
81
+ mock_app {
82
+ use_in_file_templates!('/foo/bar')
83
+ }
84
+ }
85
+
86
+ assert @app.templates.empty?
77
87
  end
78
88
  end
79
89
 
90
+ # __END__ : this is not the real end of the script.
91
+
80
92
  __END__
81
93
 
82
94
  @@ foo
@@ -1,7 +1,7 @@
1
1
  require 'yaml'
2
2
  require File.dirname(__FILE__) + '/helper'
3
3
 
4
- describe 'Sinatra::Test' do
4
+ class TestTest < Test::Unit::TestCase
5
5
  def request
6
6
  YAML.load(body)
7
7
  end
@@ -14,7 +14,7 @@ describe 'Sinatra::Test' do
14
14
  YAML.load(request['test.params'])
15
15
  end
16
16
 
17
- before do
17
+ setup do
18
18
  mock_app {
19
19
  %w[get head post put delete].each { |verb|
20
20
  send(verb, '/') do
@@ -45,8 +45,8 @@ describe 'Sinatra::Test' do
45
45
  assert_equal('DELETE', request['REQUEST_METHOD'])
46
46
 
47
47
  head '/'
48
- assert_equal('596', response.headers['Content-Length'])
49
48
  assert_equal('', response.body)
49
+ assert response.headers['Content-Length'].to_i > 0
50
50
  end
51
51
 
52
52
  it 'allows to specify a body' do
@@ -92,8 +92,8 @@ describe 'Sinatra::Test' do
92
92
  get '/', :env => { :host => '1.2.3.4' }
93
93
  assert_equal '1.2.3.4', request['HTTP_HOST']
94
94
 
95
- get '/', :env => { :session => 'foo' }
96
- assert_equal 'foo', request['rack.session']
95
+ get '/', :env => { :session => {'foo' => 'bar'} }
96
+ assert_equal({'foo' => 'bar'}, request['rack.session'])
97
97
 
98
98
  get '/', :env => { :cookies => 'foo' }
99
99
  assert_equal 'foo', request['HTTP_COOKIE']
@@ -0,0 +1,3 @@
1
+ xml.error do
2
+ raise "goodbye"
3
+ end
@@ -0,0 +1,3 @@
1
+ Hello <%= 'World' %>
2
+ <% raise 'Goodbye' unless defined?(french) && french %>
3
+ <% raise 'Au revoir' if defined?(french) && french %>
@@ -0,0 +1,3 @@
1
+ %h1 Hello From Haml
2
+ = raise 'goodbye' unless defined?(french) && french
3
+ = raise 'au revoir' if defined?(french) && french