Skip to content

Commit

Permalink
Preparing the way for rack-test.
Browse files Browse the repository at this point in the history
This basically means getting things to use an app method, rather than an @app instance variable.
  • Loading branch information
lazyatom committed May 5, 2011
1 parent b114400 commit 44023f7
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 43 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -7,6 +7,7 @@ PATH
haml (>= 3.1)
parslet (>= 1.2.0)
rack (>= 0.9.1)
rack-test (>= 0.5.7)
ratom (>= 0.3.5)
soup (>= 1.0.6)

Expand All @@ -24,6 +25,8 @@ GEM
parslet (1.2.0)
blankslate (~> 2.0)
rack (1.2.2)
rack-test (0.5.7)
rack (>= 1.0)
rake (0.8.7)
ratom (0.6.8)
libxml-ruby (>= 1.1.2)
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -48,6 +48,7 @@ if Object.const_defined?(:Gem)
s.add_dependency("BlueCloth", ">= 1.0.0")
s.add_dependency("haml", ">=3.1")
s.add_dependency("parslet", ">= 1.2.0")
s.add_dependency("rack-test", ">=0.5.7")

s.add_development_dependency("kintama", ">= 0.1.6") # add any other gems for testing/development
s.add_development_dependency("mocha")
Expand Down
2 changes: 1 addition & 1 deletion lib/vanilla/app.rb
Expand Up @@ -140,7 +140,7 @@ def register_renderer(klass, *types)
private

def prepare_renderers(additional_renderers={})
@renderers = Hash.new(config[:default_renderer] || Vanilla::Renderers::Base)
@renderers = Hash.new { config[:default_renderer] || Vanilla::Renderers::Base }
@renderers.merge!({
"base" => Vanilla::Renderers::Base,
"markdown" => Vanilla::Renderers::Markdown,
Expand Down
14 changes: 7 additions & 7 deletions test/dynasnip_test.rb
Expand Up @@ -9,21 +9,21 @@ class ::TestDyna < Dynasnip
end

should "make the attribute available as an instance method" do
assert_equal "test attribute content", TestDyna.new(@app).test_attribute
assert_equal "test attribute content", TestDyna.new(app).test_attribute
end

should "store the attribute in the soup" do
@app.soup << TestDyna.snip_attributes
assert_equal "test attribute content", @app.soup['test_dyna'].test_attribute
app.soup << TestDyna.snip_attributes
assert_equal "test attribute content", app.soup['test_dyna'].test_attribute
end

should "allow the attribute to be overriden by the soup contents" do
@app.soup << TestDyna.snip_attributes
snip = @app.soup['test_dyna']
app.soup << TestDyna.snip_attributes
snip = app.soup['test_dyna']
snip.test_attribute = "altered content"
snip.save

assert_equal "altered content", TestDyna.new(@app).test_attribute
assert_equal "altered content", TestDyna.new(app).test_attribute
end
end

Expand All @@ -36,7 +36,7 @@ def handle
end

should "show the usage defined in the snip" do
assert_equal "This is the usage", ShowUsage.new(@app).handle
assert_equal "This is the usage", ShowUsage.new(app).handle
end
end
end
2 changes: 1 addition & 1 deletion test/dynasnips/link_to_current_snip_test.rb
Expand Up @@ -4,7 +4,7 @@

context "The link_to_current_snip dynasnip" do
setup do
@app.soup << LinkToCurrentSnip.snip_attributes
app.soup << LinkToCurrentSnip.snip_attributes
create_snip :name => "test", :content => "test {link_to_current_snip}"
end

Expand Down
2 changes: 1 addition & 1 deletion test/dynasnips/link_to_test.rb
Expand Up @@ -22,6 +22,6 @@
private

def render_dynasnip(klass, *args)
klass.new(@app).handle(*args)
klass.new(app).handle(*args)
end
end
2 changes: 1 addition & 1 deletion test/dynasnips/page_title_test.rb
Expand Up @@ -4,7 +4,7 @@

context "The page_title dynasnip" do
setup do
@app.soup << PageTitle.snip_attributes
app.soup << PageTitle.snip_attributes
end

should "render as the requested snip name if that snip has no title" do
Expand Down
7 changes: 4 additions & 3 deletions test/renderers/ruby_renderer_test.rb
@@ -1,4 +1,5 @@
require "test_helper"
require "vanilla/dynasnip"

describe Vanilla::Renderers::Ruby do
context "when rendering normally" do
Expand All @@ -9,7 +10,7 @@ def handle(*args)
end

setup do
@app.soup << TestDyna.snip_attributes
app.soup << TestDyna.snip_attributes
end

should "render the result of the handle method" do
Expand All @@ -28,7 +29,7 @@ def post(*args)
end

setup do
@app.soup << RestishDyna.snip_attributes
app.soup << RestishDyna.snip_attributes
end

should "render the result of the get method on GET requests" do
Expand All @@ -48,7 +49,7 @@ def handle(*args)
end

setup do
@app.soup << Encloser.snip_attributes
app.soup << Encloser.snip_attributes
create_snip(:name => "test", :content => "{encloser}")
end

Expand Down
2 changes: 1 addition & 1 deletion test/snip_inclusion_test.rb
Expand Up @@ -61,6 +61,6 @@ def handle(args)

def render(content)
snip = create_snip :name => "test-content", :content => content
Vanilla::Renderers::Base.new(@app).render(snip)
Vanilla::Renderers::Base.new(app).render(snip)
end
end
14 changes: 9 additions & 5 deletions test/test_helper.rb
Expand Up @@ -3,24 +3,28 @@
require "fileutils"
require "rack/mock"
require "vanilla"
require "rack/test"

module Vanilla
module Test
class ::TestApp < Vanilla::App
end

def app
@__app ||= TestApp.new(:soup => soup_path)
end

def setup_clean_environment
clean_environment
TestApp.reset!
@app = TestApp.new(:soup => soup_path)

require File.expand_path("../../pristine_app/soups/system/current_snip", __FILE__)
@app.soup << CurrentSnip.snip_attributes
app.soup << CurrentSnip.snip_attributes
create_snip :name => "layout", :content => "{current_snip}"
end

def response_for(url)
@app.call(mock_env_for_url(url))
app.call(mock_env_for_url(url))
end

def response_body_for(url)
Expand All @@ -36,11 +40,11 @@ def assert_response_body(expected, uri)
end

def set_main_template(template_content)
@app.soup << {:name => "layout", :content => template_content}
app.soup << {:name => "layout", :content => template_content}
end

def create_snip(params)
@app.soup << params
app.soup << params
end

def mock_env_for_url(url)
Expand Down
20 changes: 8 additions & 12 deletions test/vanilla_app_test.rb
Expand Up @@ -5,7 +5,7 @@
context "when behaving as a Rack application" do
should "return an array of status code, headers and response" do
create_snip(:name => "test", :content => "content")
result = @app.call(mock_env_for_url("/test.text"))
result = app.call(mock_env_for_url("/test.text"))
assert_kind_of Array, result
assert_equal 200, result[0]
assert_kind_of Hash, result[1]
Expand All @@ -22,7 +22,7 @@
should "allow a customised root snip" do
create_snip :name => "start", :content => "default"
create_snip :name => "custom", :content => "custom"
@app = TestApp.new(:soup => soup_path, :root_snip => "custom")
app.config[:root_snip] = "custom"
assert_response_body "custom", "/"
end

Expand All @@ -32,9 +32,9 @@
FileUtils.mkdir_p(soup_dir)
File.open(File.join(soup_dir, "blah.snip"), "w") { |f| f.write "Hello superfriends" }

@app = TestApp.new(:soup => "my_soup", :root => tmp_dir)
app = TestApp.new(:soup => "my_soup", :root => tmp_dir)

assert_equal "Hello superfriends", @app.soup['blah'].content
assert_equal "Hello superfriends", app.soup['blah'].content
end

should "allow configuration against the class" do
Expand All @@ -49,13 +49,9 @@
end

context "when detecting the snip renderer" do
setup do
@app = TestApp.new(:soup => soup_path)
end

should "return the constant refered to in the render_as property of the snip" do
snip = create_snip(:name => "blah", :render_as => "Raw")
assert_equal Vanilla::Renderers::Raw, @app.renderer_for(snip)
assert_equal Vanilla::Renderers::Raw, app.renderer_for(snip)
end

context "using the snip extension" do
Expand All @@ -68,7 +64,7 @@
}.each do |extension, renderer|
should "return the renderer #{renderer} when the snip has extension #{extension}" do
snip = create_snip(:name => "blah", :extension => extension)
assert_equal renderer, @app.renderer_for(snip)
assert_equal renderer, app.renderer_for(snip)
end
end
end
Expand All @@ -81,12 +77,12 @@

should "return Vanilla::Renderers::Base if no render_as property exists" do
snip = create_snip(:name => "blah")
assert_equal Vanilla::Renderers::Base, @app.renderer_for(snip)
assert_equal Vanilla::Renderers::Base, app.renderer_for(snip)
end

should "return Vanilla::Renderers::Base if the render_as property is blank" do
snip = create_snip(:name => "blah", :render_as => '')
assert_equal Vanilla::Renderers::Base, @app.renderer_for(snip)
assert_equal Vanilla::Renderers::Base, app.renderer_for(snip)
end

should "raise an error if the specified renderer doesn't exist" do
Expand Down
4 changes: 2 additions & 2 deletions test/vanilla_presenting_test.rb
Expand Up @@ -82,7 +82,7 @@ def default_layout_snip

context "a snip using a renderer that specifies a template" do
setup do
@app.register_renderer CustomRenderer, "custom"
app.register_renderer CustomRenderer, "custom"
create_snip :name => "custom-layout", :content => "<custom>{current_snip}</custom>"
end

Expand All @@ -100,7 +100,7 @@ def default_layout_snip

context "and a custom default renderer has been provided" do
should "use that renderer" do
@app = TestApp.new(:soup => soup_path, :default_renderer => ::Vanilla::Renderers::Bold)
app.config[:default_renderer] = ::Vanilla::Renderers::Bold
create_snip :name => "layout", :content => "{test}", :render_as => "base"
create_snip :name => "test", :content => "test"
assert_response_body "<b>test</b>", "/test"
Expand Down
14 changes: 7 additions & 7 deletions test/vanilla_request_test.rb
Expand Up @@ -2,7 +2,7 @@

describe Vanilla::Request do
context "when requesting the root" do
setup { @request = Vanilla::Request.new(mock_env_for_url("/"), @app) }
setup { @request = Vanilla::Request.new(mock_env_for_url("/"), app) }

should "set snip to 'start' by default" do
assert_equal "start", @request.snip_name
Expand All @@ -14,14 +14,14 @@
end

context "when requesting urls" do
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip"), @app) }
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip"), app) }

should "use the first segement as the snip name" do
assert_equal "snip", @request.snip_name
end

should "try to load the snip based on the snip name" do
@app.soup.expects(:[]).with('snip').returns(:snip)
app.soup.expects(:[]).with('snip').returns(:snip)
assert_equal :snip, @request.snip
end

Expand All @@ -39,7 +39,7 @@
end

context "when requesting a snip part" do
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip/part"), @app) }
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip/part"), app) }

should "use the first segment as the snip, and the second segment as the part" do
assert_equal "snip", @request.snip_name
Expand All @@ -52,7 +52,7 @@
end

context "when requesting a snip with a format" do
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip.raw"), @app) }
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip.raw"), app) }

should "use the extension as the format" do
assert_equal "raw", @request.format
Expand All @@ -64,7 +64,7 @@
end

context "when requesting a snip part with a format" do
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip/part.raw"), @app) }
setup { @request = Vanilla::Request.new(mock_env_for_url("/snip/part.raw"), app) }

should "use the extension as the format" do
assert_equal "raw", @request.format
Expand All @@ -81,7 +81,7 @@

context "when requested with a _method parameter" do
should "return the method using the parameter" do
assert_equal 'put', Vanilla::Request.new(mock_env_for_url("/snip?_method=put"), @app).method
assert_equal 'put', Vanilla::Request.new(mock_env_for_url("/snip?_method=put"), app).method
end
end
end
5 changes: 3 additions & 2 deletions vanilla.gemspec
Expand Up @@ -22,8 +22,6 @@ Gem::Specification.new do |s|
"test/dynasnips/link_to_current_snip_test.rb",
"test/dynasnips/link_to_test.rb",
"test/dynasnips/page_title_test.rb",
"test/monkey",
"test/my_soup",
"test/renderers",
"test/renderers/base_renderer_test.rb",
"test/renderers/erb_renderer_test.rb",
Expand Down Expand Up @@ -127,6 +125,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency(%q<BlueCloth>, [">= 1.0.0"])
s.add_runtime_dependency(%q<haml>, [">= 3.1"])
s.add_runtime_dependency(%q<parslet>, [">= 1.2.0"])
s.add_runtime_dependency(%q<rack-test>, [">= 0.5.7"])
s.add_development_dependency(%q<kintama>, [">= 0.1.6"])
s.add_development_dependency(%q<mocha>, [">= 0"])
else
Expand All @@ -137,6 +136,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<BlueCloth>, [">= 1.0.0"])
s.add_dependency(%q<haml>, [">= 3.1"])
s.add_dependency(%q<parslet>, [">= 1.2.0"])
s.add_dependency(%q<rack-test>, [">= 0.5.7"])
s.add_dependency(%q<kintama>, [">= 0.1.6"])
s.add_dependency(%q<mocha>, [">= 0"])
end
Expand All @@ -148,6 +148,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<BlueCloth>, [">= 1.0.0"])
s.add_dependency(%q<haml>, [">= 3.1"])
s.add_dependency(%q<parslet>, [">= 1.2.0"])
s.add_dependency(%q<rack-test>, [">= 0.5.7"])
s.add_dependency(%q<kintama>, [">= 0.1.6"])
s.add_dependency(%q<mocha>, [">= 0"])
end
Expand Down

0 comments on commit 44023f7

Please sign in to comment.