Permalink
Browse files

Step 1: Hookup simple DSL and routing to actions

  • Loading branch information...
0 parents commit 688318857921a21c0fa0af1a0a210745b46c0cb0 @wycats committed with Carl Lerche Aug 27, 2009
@@ -0,0 +1,13 @@
+require 'rubygems'
+require 'rake'
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.test_files = FileList['tests/steps/*.rb']
+end
+
+1.upto(6) do |i|
+ Rake::TestTask.new("step_#{i}") do |t|
+ t.test_files = (1..i).map { |j| "tests/steps/test_step_0#{j}.rb" }
+ end
+end
@@ -0,0 +1,24 @@
+require "digest/sha2"
+require "action_dispatch"
+require "action_controller"
+require "usher"
+
+module Sinatra
+ class Base < ActionController::Metal
+ class << self
+ # Returns a valid rack application for the
+ # current sinatra application declaration.
+ def to_app
+ lambda { |env| [200, {}, "Placeholder!"] }
+ end
+
+ def get(*)
+ # Not implemented...
+ end
+
+ def post(*)
+ # Not implemented...
+ end
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+# Getting a very basic Sinatra::Base working
+#
+class TestStep01 < Test::Unit::TestCase
+ class MyApp < Sinatra::Base
+ get "/hello" do
+ "HELLO World"
+ end
+
+ post "/world" do
+ "Hello from POST"
+ end
+ end
+
+ def test_successful_get
+ visit("/hello")
+ assert_body "HELLO World"
+ end
+
+ def test_successful_post
+ visit("/world", :post)
+ assert_body "Hello from POST"
+ end
+
+ def test_missing_route
+ visit("/fail")
+ assert_missing
+ end
+
+end
@@ -0,0 +1,34 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+# Adding session support
+#
+class TestStep02 < Test::Unit::TestCase
+ class MyApp < Sinatra::Base
+ enable :sessions
+
+ get "/set1" do
+ session[:hello] = "WIN!!"
+ "Setting session"
+ end
+
+ get "/set2" do
+ session[:hello] = "WIN AGAIN!!"
+ "Setting session"
+ end
+
+ get "/get" do
+ "Session: #{session[:hello]}"
+ end
+ end
+
+ def test_using_sessions
+ visit("/set1")
+ visit("/get")
+ assert_body "Session: WIN!!"
+
+ visit("/set2")
+ visit("/get")
+ assert_body "Session: WIN AGAIN!!"
+ end
+
+end
@@ -0,0 +1,22 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+# Adding simple callbacks
+#
+class TestStep03 < Test::Unit::TestCase
+ class MyApp < Sinatra::Base
+
+ before do
+ @what = "awesome!!!"
+ end
+
+ get "/hooked" do
+ "This is: #{@what}"
+ end
+ end
+
+ def test_using_before_callbacks
+ visit("/hooked")
+ assert_body "This is: awesome!!!"
+ end
+
+end
@@ -0,0 +1,22 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+# Halting from callbacks
+#
+class TestStep04 < Test::Unit::TestCase
+ class MyApp < Sinatra::Base
+
+ before do
+ halt "WIN!"
+ end
+
+ get "/hooked" do
+ "EPIC FAIL!"
+ end
+ end
+
+ def test_using_before_callbacks
+ visit("/hooked")
+ assert_body "WIN!"
+ end
+
+end
@@ -0,0 +1,19 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+# Rendering templates
+#
+class TestStep05 < Test::Unit::TestCase
+ class MyApp < Sinatra::Base
+ set :views, File.join(File.dirname(__FILE__), "..", '..', "views")
+
+ get "/hello" do
+ erb :hello
+ end
+ end
+
+ def test_using_before_callbacks
+ visit("/hello")
+ assert_body "Hello from template"
+ end
+
+end
@@ -0,0 +1,18 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+# Inline template rendering
+#
+class TestStep06 < Test::Unit::TestCase
+ class MyApp < Sinatra::Base
+ get "/hello" do
+ @world = "world"
+ erb "Hello: <%= @world %>"
+ end
+ end
+
+ def test_using_before_callbacks
+ visit("/hello")
+ assert_body "Hello: world"
+ end
+
+end
@@ -0,0 +1,28 @@
+$:.unshift File.join(File.dirname(__FILE__), '..')
+require "rubygems"
+require "sinatrails"
+require "webrat"
+require "rack/test"
+require "test/unit"
+# require "active_support/test_case"
+
+Webrat.configure do |config|
+ config.mode = :rack
+end
+
+class Test::Unit::TestCase
+ include Webrat::Methods
+ include Rack::Test::Methods
+
+ def app
+ @app ||= self.class.const_get(:MyApp).to_app
+ end
+
+ def assert_body(body)
+ assert_equal body, last_response.body
+ end
+
+ def assert_missing
+ assert_equal 404, last_response.status
+ end
+end
@@ -0,0 +1 @@
+Hello from template

0 comments on commit 6883188

Please sign in to comment.