-
-
Save nakajima/108213 to your computer and use it in GitHub Desktop.
forked to follow
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 "test/unit" | |
require "contest" | |
require "rack/test" | |
require "sinatra/base" | |
require "nakajima" | |
class MyApp < Sinatra::Base | |
set :environment, :test | |
get "/" do | |
"Hi, #{params["name"]}." | |
end | |
get "/with-setup" do | |
val = $SETUP_VALUE | |
$SETUP_VALUE = nil | |
val | |
end | |
get "/previous-setup-cleared" do | |
val = $SETUP_VALUE | |
$SETUP_VALUE = nil | |
val | |
end | |
end | |
class CallbackChain | |
def initialize | |
@chain = [] | |
end | |
def prepare | |
callbacks = [] | |
@next_chain = [] | |
@chain.inject([]) do |callbacks, (block, remaining)| | |
callbacks.tap do | |
if remaining > 0 | |
callbacks << block | |
@next_chain.push([ block, remaining-1 ]) | |
end | |
end | |
end | |
end | |
def clean | |
@chain.replace(@next_chain) | |
end | |
def once(block) | |
@chain << [block, 1] | |
end | |
def twice(block) | |
@chain << [block, 2] | |
end | |
def thrice(block) | |
@chain << [block, 3] | |
end | |
end | |
class Test::Unit::TestCase | |
class << self | |
include Test::Unit::Assertions | |
def get(uri, params={}, env={}, &block) | |
callbacks = chain.prepare | |
define_method "test GET #{uri} #{params.inspect} #{env.inspect}" do | |
callbacks.each { |fn| fn.call } | |
session.get(uri, params, env, &block) | |
end | |
chain.clean | |
end | |
def once(&block) | |
chain.once(block) | |
end | |
def twice(&block) | |
chain.twice(block) | |
end | |
def thrice(&block) | |
chain.thrice(block) | |
end | |
def chain | |
@chain ||= CallbackChain.new | |
end | |
end | |
def session | |
@session ||= Rack::Test::Session.new(MyApp) | |
end | |
end | |
class MyAppTest < Test::Unit::TestCase | |
get("/", {"name" => "John Doe"}) do |response| | |
assert response.ok? | |
assert_equal "Hi, John Doe.", response.body.to_s | |
end | |
once { $SETUP_VALUE = 'hello world!' } | |
get("/with-setup#once-only") do |response| | |
assert_equal 'hello world!', response.body.to_s | |
end | |
get("/previous-setup-cleared") do |response| | |
assert_equal '', response.body.to_s | |
end | |
twice { $SETUP_VALUE = 'hello world again!' } | |
get("/with-setup#once") do |response| | |
assert_equal 'hello world again!', response.body.to_s | |
end | |
get("/with-setup#twice") do |response| | |
assert_equal 'hello world again!', response.body.to_s | |
end | |
get("/previous-setup-cleared#after-twice") do |response| | |
assert_equal '', response.body.to_s | |
end | |
context "with $SETUP_VALUE set to 'foo'" do | |
setup do | |
$SETUP_VALUE = "foo" | |
end | |
get "/with-setup" do |response| | |
assert_equal "foo", response.body.to_s | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment