Created
March 1, 2009 15:57
-
-
Save kusor/72364 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') | |
require 'rubygems' | |
require 'sinatra' | |
require 'sinatra/extension_template' | |
get '/' do | |
'Classic sample application' | |
end | |
get '/defined' do | |
my_sample_method('foo') | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
module Sinatra | |
# Use from classic "top-level" applications with: | |
# require 'sinatra' | |
# require 'sinatra/extension_template' | |
# | |
# Use from modular applications with: | |
# require 'sinatra/base' | |
# require 'sinatra/extension_template' | |
# class MyApp < Sinatra::Base | |
# register Sinatra::ExtensionTemplate | |
# end | |
# | |
module ExtensionTemplate | |
module Helpers | |
# Helper method sample | |
def my_sample_method(foo='sample') | |
foo | |
end | |
end | |
# Extend DSL sample | |
def self.registered(app) | |
# Extend the request context | |
app.helpers ExtensionTemplate::Helpers | |
app.get '/sample' do | |
'Hello from extension!' | |
end | |
end | |
end | |
# Extend the DSL context | |
register ExtensionTemplate | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.join(File.dirname(__FILE__), 'classic_app_sample') | |
require 'test/unit' | |
require 'sinatra/test' | |
set :environment, :test | |
class TestApplication < Test::Unit::TestCase | |
include Sinatra::Test | |
def setup | |
@app = Sinatra::Application | |
end | |
def test_home_ok | |
get '/' | |
assert_equal 200, @response.status | |
assert @response.body.length > 0 | |
end | |
def test_helper_method | |
assert defined?(:my_sample_method) | |
get '/defined' | |
assert_equal 200, @response.status | |
assert_equal 'foo', @response.body | |
end | |
def test_dsl_extension_fails_unless_explicitly_registerd_for_tests | |
get '/sample' | |
assert_equal 404, @response.status | |
end | |
def test_dsl_extension_ok_when_explicitly_registerd_for_tests | |
@app.register Sinatra::ExtensionTemplate | |
get '/sample' | |
assert_equal 200, @response.status | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment