sinatra-base 1.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.
Files changed (58) hide show
  1. data/AUTHORS +43 -0
  2. data/CHANGES +511 -0
  3. data/LICENSE +22 -0
  4. data/README.jp.rdoc +552 -0
  5. data/README.rdoc +636 -0
  6. data/Rakefile +116 -0
  7. data/lib/sinatra.rb +7 -0
  8. data/lib/sinatra/base.rb +1167 -0
  9. data/lib/sinatra/images/404.png +0 -0
  10. data/lib/sinatra/images/500.png +0 -0
  11. data/lib/sinatra/main.rb +28 -0
  12. data/lib/sinatra/showexceptions.rb +307 -0
  13. data/lib/sinatra/tilt.rb +746 -0
  14. data/sinatra-base.gemspec +94 -0
  15. data/test/base_test.rb +160 -0
  16. data/test/builder_test.rb +65 -0
  17. data/test/contest.rb +64 -0
  18. data/test/erb_test.rb +81 -0
  19. data/test/erubis_test.rb +82 -0
  20. data/test/extensions_test.rb +100 -0
  21. data/test/filter_test.rb +221 -0
  22. data/test/haml_test.rb +95 -0
  23. data/test/helper.rb +76 -0
  24. data/test/helpers_test.rb +582 -0
  25. data/test/less_test.rb +37 -0
  26. data/test/mapped_error_test.rb +197 -0
  27. data/test/middleware_test.rb +68 -0
  28. data/test/public/favicon.ico +0 -0
  29. data/test/request_test.rb +33 -0
  30. data/test/response_test.rb +42 -0
  31. data/test/result_test.rb +98 -0
  32. data/test/route_added_hook_test.rb +59 -0
  33. data/test/routing_test.rb +860 -0
  34. data/test/sass_test.rb +85 -0
  35. data/test/server_test.rb +47 -0
  36. data/test/settings_test.rb +368 -0
  37. data/test/sinatra_test.rb +13 -0
  38. data/test/static_test.rb +93 -0
  39. data/test/templates_test.rb +159 -0
  40. data/test/views/error.builder +3 -0
  41. data/test/views/error.erb +3 -0
  42. data/test/views/error.erubis +3 -0
  43. data/test/views/error.haml +3 -0
  44. data/test/views/error.sass +2 -0
  45. data/test/views/foo/hello.test +1 -0
  46. data/test/views/hello.builder +1 -0
  47. data/test/views/hello.erb +1 -0
  48. data/test/views/hello.erubis +1 -0
  49. data/test/views/hello.haml +1 -0
  50. data/test/views/hello.less +5 -0
  51. data/test/views/hello.sass +2 -0
  52. data/test/views/hello.test +1 -0
  53. data/test/views/layout2.builder +3 -0
  54. data/test/views/layout2.erb +2 -0
  55. data/test/views/layout2.erubis +2 -0
  56. data/test/views/layout2.haml +2 -0
  57. data/test/views/layout2.test +1 -0
  58. metadata +257 -0
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ begin
4
+ require 'sass'
5
+
6
+ class SassTest < Test::Unit::TestCase
7
+ def sass_app(&block)
8
+ mock_app {
9
+ set :views, File.dirname(__FILE__) + '/views'
10
+ get '/', &block
11
+ }
12
+ get '/'
13
+ end
14
+
15
+ it 'renders inline Sass strings' do
16
+ sass_app { sass "#sass\n :background-color #FFF\n" }
17
+ assert ok?
18
+ assert_equal "#sass {\n background-color: #FFF; }\n", body
19
+ end
20
+
21
+ it 'renders .sass files in views path' do
22
+ sass_app { sass :hello }
23
+ assert ok?
24
+ assert_equal "#sass {\n background-color: #FFF; }\n", body
25
+ end
26
+
27
+ it 'ignores the layout option' do
28
+ sass_app { sass :hello, :layout => :layout2 }
29
+ assert ok?
30
+ assert_equal "#sass {\n background-color: #FFF; }\n", body
31
+ end
32
+
33
+ it "raises error if template not found" do
34
+ mock_app {
35
+ get('/') { sass :no_such_template }
36
+ }
37
+ assert_raise(Errno::ENOENT) { get('/') }
38
+ end
39
+
40
+ it "passes SASS options to the Sass engine" do
41
+ sass_app {
42
+ sass "#sass\n :background-color #FFF\n :color #000\n", :style => :compact
43
+ }
44
+ assert ok?
45
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
46
+ end
47
+
48
+ it "passes default SASS options to the Sass engine" do
49
+ mock_app {
50
+ set :sass, {:style => :compact} # default Sass style is :nested
51
+ get '/' do
52
+ sass "#sass\n :background-color #FFF\n :color #000\n"
53
+ end
54
+ }
55
+ get '/'
56
+ assert ok?
57
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
58
+ end
59
+
60
+ it "merges the default SASS options with the overrides and passes them to the Sass engine" do
61
+ mock_app {
62
+ set :sass, {:style => :compact, :attribute_syntax => :alternate } # default Sass attribute_syntax is :normal (with : in front)
63
+ get '/' do
64
+ sass "#sass\n background-color: #FFF\n color: #000\n"
65
+ end
66
+ get '/raised' do
67
+ sass "#sass\n :background-color #FFF\n :color #000\n", :style => :expanded # retains global attribute_syntax settings
68
+ end
69
+ get '/expanded_normal' do
70
+ sass "#sass\n :background-color #FFF\n :color #000\n", :style => :expanded, :attribute_syntax => :normal
71
+ end
72
+ }
73
+ get '/'
74
+ assert ok?
75
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
76
+ assert_raise(Sass::SyntaxError) { get('/raised') }
77
+ get '/expanded_normal'
78
+ assert ok?
79
+ assert_equal "#sass {\n background-color: #FFF;\n color: #000;\n}\n", body
80
+ end
81
+ end
82
+
83
+ rescue
84
+ warn "#{$!.to_s}: skipping sass tests"
85
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ module Rack::Handler
4
+ class Mock
5
+ extend Test::Unit::Assertions
6
+
7
+ def self.run(app, options={})
8
+ assert(app < Sinatra::Base)
9
+ assert_equal 9001, options[:Port]
10
+ assert_equal 'foo.local', options[:Host]
11
+ yield new
12
+ end
13
+
14
+ def stop
15
+ end
16
+ end
17
+
18
+ register 'mock', 'Rack::Handler::Mock'
19
+ end
20
+
21
+ class ServerTest < Test::Unit::TestCase
22
+ setup do
23
+ mock_app {
24
+ set :server, 'mock'
25
+ set :bind, 'foo.local'
26
+ set :port, 9001
27
+ }
28
+ $stdout = File.open('/dev/null', 'wb')
29
+ end
30
+
31
+ def teardown
32
+ $stdout = STDOUT
33
+ end
34
+
35
+ it "locates the appropriate Rack handler and calls ::run" do
36
+ @app.run!
37
+ end
38
+
39
+ it "sets options on the app before running" do
40
+ @app.run! :sessions => true
41
+ assert @app.sessions?
42
+ end
43
+
44
+ it "falls back on the next server handler when not found" do
45
+ @app.run! :server => %w[foo bar mock]
46
+ end
47
+ end
@@ -0,0 +1,368 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class SettingsTest < Test::Unit::TestCase
4
+ setup do
5
+ @base = Sinatra.new(Sinatra::Base)
6
+ @base.set :environment, :foo
7
+
8
+ @application = Sinatra.new(Sinatra::Application)
9
+ @application.set :environment, :foo
10
+ end
11
+
12
+ it 'sets settings to literal values' do
13
+ @base.set(:foo, 'bar')
14
+ assert @base.respond_to?(:foo)
15
+ assert_equal 'bar', @base.foo
16
+ end
17
+
18
+ it 'sets settings to Procs' do
19
+ @base.set(:foo, Proc.new { 'baz' })
20
+ assert @base.respond_to?(:foo)
21
+ assert_equal 'baz', @base.foo
22
+ end
23
+
24
+ it 'sets settings using a block' do
25
+ @base.set(:foo){ 'baz' }
26
+ assert @base.respond_to?(:foo)
27
+ assert_equal 'baz', @base.foo
28
+ end
29
+
30
+ it 'raises an error with a value and a block' do
31
+ assert_raise ArgumentError do
32
+ @base.set(:fiz, 'boom!'){ 'baz' }
33
+ end
34
+ assert !@base.respond_to?(:fiz)
35
+ end
36
+
37
+ it "sets multiple settings with a Hash" do
38
+ @base.set :foo => 1234,
39
+ :bar => 'Hello World',
40
+ :baz => Proc.new { 'bizzle' }
41
+ assert_equal 1234, @base.foo
42
+ assert_equal 'Hello World', @base.bar
43
+ assert_equal 'bizzle', @base.baz
44
+ end
45
+
46
+ it 'inherits settings methods when subclassed' do
47
+ @base.set :foo, 'bar'
48
+ @base.set :biz, Proc.new { 'baz' }
49
+
50
+ sub = Class.new(@base)
51
+ assert sub.respond_to?(:foo)
52
+ assert_equal 'bar', sub.foo
53
+ assert sub.respond_to?(:biz)
54
+ assert_equal 'baz', sub.biz
55
+ end
56
+
57
+ it 'overrides settings in subclass' do
58
+ @base.set :foo, 'bar'
59
+ @base.set :biz, Proc.new { 'baz' }
60
+ sub = Class.new(@base)
61
+ sub.set :foo, 'bling'
62
+ assert_equal 'bling', sub.foo
63
+ assert_equal 'bar', @base.foo
64
+ end
65
+
66
+ it 'creates setter methods when first defined' do
67
+ @base.set :foo, 'bar'
68
+ assert @base.respond_to?('foo=')
69
+ @base.foo = 'biz'
70
+ assert_equal 'biz', @base.foo
71
+ end
72
+
73
+ it 'creates predicate methods when first defined' do
74
+ @base.set :foo, 'hello world'
75
+ assert @base.respond_to?(:foo?)
76
+ assert @base.foo?
77
+ @base.set :foo, nil
78
+ assert !@base.foo?
79
+ end
80
+
81
+ it 'uses existing setter methods if detected' do
82
+ class << @base
83
+ def foo
84
+ @foo
85
+ end
86
+ def foo=(value)
87
+ @foo = 'oops'
88
+ end
89
+ end
90
+
91
+ @base.set :foo, 'bam'
92
+ assert_equal 'oops', @base.foo
93
+ end
94
+
95
+ it "sets multiple settings to true with #enable" do
96
+ @base.enable :sessions, :foo, :bar
97
+ assert @base.sessions
98
+ assert @base.foo
99
+ assert @base.bar
100
+ end
101
+
102
+ it "sets multiple settings to false with #disable" do
103
+ @base.disable :sessions, :foo, :bar
104
+ assert !@base.sessions
105
+ assert !@base.foo
106
+ assert !@base.bar
107
+ end
108
+
109
+ it 'is accessible from instances via #settings' do
110
+ assert_equal :foo, @base.new.settings.environment
111
+ end
112
+
113
+ describe 'methodoverride' do
114
+ it 'is disabled on Base' do
115
+ assert ! @base.method_override?
116
+ end
117
+
118
+ it 'is enabled on Application' do
119
+ assert @application.method_override?
120
+ end
121
+
122
+ it 'enables MethodOverride middleware' do
123
+ @base.set :method_override, true
124
+ @base.put('/') { 'okay' }
125
+ @app = @base
126
+ post '/', {'_method'=>'PUT'}, {}
127
+ assert_equal 200, status
128
+ assert_equal 'okay', body
129
+ end
130
+
131
+ it 'is backward compatible with methodoverride' do
132
+ assert ! @base.methodoverride?
133
+ @base.enable :methodoverride
134
+ assert @base.methodoverride?
135
+ end
136
+ end
137
+
138
+ describe 'run' do
139
+ it 'is disabled on Base' do
140
+ assert ! @base.run?
141
+ end
142
+
143
+ it 'is enabled on Application except in test environment' do
144
+ assert @application.run?
145
+
146
+ @application.set :environment, :test
147
+ assert ! @application.run?
148
+ end
149
+ end
150
+
151
+ describe 'raise_errors' do
152
+ it 'is enabled on Base only in test' do
153
+ assert ! @base.raise_errors?
154
+
155
+ @base.set(:environment, :test)
156
+ assert @base.raise_errors?
157
+ end
158
+
159
+ it 'is enabled on Application only in test' do
160
+ assert ! @application.raise_errors?
161
+
162
+ @application.set(:environment, :test)
163
+ assert @application.raise_errors?
164
+ end
165
+ end
166
+
167
+ describe 'show_exceptions' do
168
+ it 'is disabled on Base except under development' do
169
+ assert ! @base.show_exceptions?
170
+ @base.environment = :development
171
+ assert @base.show_exceptions?
172
+ end
173
+
174
+ it 'is disabled on Application except in development' do
175
+ assert ! @application.show_exceptions?
176
+
177
+ @application.set(:environment, :development)
178
+ assert @application.show_exceptions?
179
+ end
180
+
181
+ it 'returns a friendly 500' do
182
+ klass = Sinatra.new(Sinatra::Application)
183
+ mock_app(klass) {
184
+ enable :show_exceptions
185
+
186
+ get '/' do
187
+ raise StandardError
188
+ end
189
+ }
190
+
191
+ get '/'
192
+ assert_equal 500, status
193
+ assert body.include?("StandardError")
194
+ assert body.include?("<code>show_exceptions</code> setting")
195
+ end
196
+ end
197
+
198
+ describe 'dump_errors' do
199
+ it 'is disabled on Base in test' do
200
+ @base.environment = :test
201
+ assert ! @base.dump_errors?
202
+ @base.environment = :development
203
+ assert @base.dump_errors?
204
+ @base.environment = :production
205
+ assert @base.dump_errors?
206
+ end
207
+
208
+ it 'dumps exception with backtrace to rack.errors' do
209
+ klass = Sinatra.new(Sinatra::Application)
210
+
211
+ mock_app(klass) {
212
+ enable :dump_errors
213
+ disable :raise_errors
214
+
215
+ error do
216
+ error = @env['rack.errors'].instance_variable_get(:@error)
217
+ error.rewind
218
+
219
+ error.read
220
+ end
221
+
222
+ get '/' do
223
+ raise
224
+ end
225
+ }
226
+
227
+ get '/'
228
+ assert body.include?("RuntimeError") && body.include?("settings_test.rb")
229
+ end
230
+ end
231
+
232
+ describe 'sessions' do
233
+ it 'is disabled on Base' do
234
+ assert ! @base.sessions?
235
+ end
236
+
237
+ it 'is disabled on Application' do
238
+ assert ! @application.sessions?
239
+ end
240
+ end
241
+
242
+ describe 'logging' do
243
+ it 'is disabled on Base' do
244
+ assert ! @base.logging?
245
+ end
246
+
247
+ it 'is enabled on Application except in test environment' do
248
+ assert @application.logging?
249
+
250
+ @application.set :environment, :test
251
+ assert ! @application.logging
252
+ end
253
+ end
254
+
255
+ describe 'static' do
256
+ it 'is disabled on Base by default' do
257
+ assert ! @base.static?
258
+ end
259
+
260
+ it 'is enabled on Base when public is set and exists' do
261
+ @base.set :environment, :development
262
+ @base.set :public, File.dirname(__FILE__)
263
+ assert @base.static?
264
+ end
265
+
266
+ it 'is enabled on Base when root is set and root/public exists' do
267
+ @base.set :environment, :development
268
+ @base.set :root, File.dirname(__FILE__)
269
+ assert @base.static?
270
+ end
271
+
272
+ it 'is disabled on Application by default' do
273
+ assert ! @application.static?
274
+ end
275
+
276
+ it 'is enabled on Application when public is set and exists' do
277
+ @application.set :environment, :development
278
+ @application.set :public, File.dirname(__FILE__)
279
+ assert @application.static?
280
+ end
281
+
282
+ it 'is enabled on Application when root is set and root/public exists' do
283
+ @application.set :environment, :development
284
+ @application.set :root, File.dirname(__FILE__)
285
+ assert @application.static?
286
+ end
287
+ end
288
+
289
+ describe 'bind' do
290
+ it 'defaults to 0.0.0.0' do
291
+ assert_equal '0.0.0.0', @base.bind
292
+ assert_equal '0.0.0.0', @application.bind
293
+ end
294
+ end
295
+
296
+ describe 'port' do
297
+ it 'defaults to 4567' do
298
+ assert_equal 4567, @base.port
299
+ assert_equal 4567, @application.port
300
+ end
301
+ end
302
+
303
+ describe 'server' do
304
+ it 'is one of thin, mongrel, webrick' do
305
+ assert_equal %w[thin mongrel webrick], @base.server
306
+ assert_equal %w[thin mongrel webrick], @application.server
307
+ end
308
+ end
309
+
310
+ describe 'app_file' do
311
+ it 'is nil' do
312
+ assert_nil @base.app_file
313
+ assert_nil @application.app_file
314
+ end
315
+ end
316
+
317
+ describe 'root' do
318
+ it 'is nil if app_file is not set' do
319
+ assert @base.root.nil?
320
+ assert @application.root.nil?
321
+ end
322
+
323
+ it 'is equal to the expanded basename of app_file' do
324
+ @base.app_file = __FILE__
325
+ assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
326
+
327
+ @application.app_file = __FILE__
328
+ assert_equal File.expand_path(File.dirname(__FILE__)), @application.root
329
+ end
330
+ end
331
+
332
+ describe 'views' do
333
+ it 'is nil if root is not set' do
334
+ assert @base.views.nil?
335
+ assert @application.views.nil?
336
+ end
337
+
338
+ it 'is set to root joined with views/' do
339
+ @base.root = File.dirname(__FILE__)
340
+ assert_equal File.dirname(__FILE__) + "/views", @base.views
341
+
342
+ @application.root = File.dirname(__FILE__)
343
+ assert_equal File.dirname(__FILE__) + "/views", @application.views
344
+ end
345
+ end
346
+
347
+ describe 'public' do
348
+ it 'is nil if root is not set' do
349
+ assert @base.public.nil?
350
+ assert @application.public.nil?
351
+ end
352
+
353
+ it 'is set to root joined with public/' do
354
+ @base.root = File.dirname(__FILE__)
355
+ assert_equal File.dirname(__FILE__) + "/public", @base.public
356
+
357
+ @application.root = File.dirname(__FILE__)
358
+ assert_equal File.dirname(__FILE__) + "/public", @application.public
359
+ end
360
+ end
361
+
362
+ describe 'lock' do
363
+ it 'is disabled by default' do
364
+ assert ! @base.lock?
365
+ assert ! @application.lock?
366
+ end
367
+ end
368
+ end