Browse files

r63@chuao: sd | 2007-10-08 11:35:59 -0400

 Tests now pass cleanly


git-svn-id: https://dev.notso.net/svn/rails/plugins/embedded_actions/trunk@125 71bf3dff-0d17-0410-8985-d5c41cf5d41c
  • Loading branch information...
1 parent 9cc2422 commit 6b2494809ddaf6a68c46dcc367fa3e47e9eeee9b sd committed Oct 8, 2007
View
15 Rakefile
@@ -0,0 +1,15 @@
+require 'rake'
+require 'rake/testtask'
+
+$:.unshift 'lib'
+#require './init.rb'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Run all unit tests.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/test_*.rb'
+ t.verbose = true
+end
View
6 lib/embedded_actions/caches_embedded.rb
@@ -23,13 +23,13 @@ def caches_embedded(*actions)
module InstanceMethods
def cache_embedded?(options)
- cache_this_instance = options.delete :caching # the rest of the request processing code doesn't have to know about this option
-
+ cache_this_instance = options[:params] && options[:params].delete(:caching) # the rest of the request processing code doesn't have to know about this option
return false unless self.perform_caching
if embedded_class(options).cached_embedded[options[:action].to_sym]
return true unless cache_this_instance == false
end
+
return cache_this_instance
end
@@ -38,7 +38,7 @@ def expire_embedded(options)
end
def embed_action_as_string_with_caching(options)
- force_refresh = options.delete :refresh_cache
+ force_refresh = options[:params] && options[:params].delete(:refresh_cache)
return embed_action_as_string_without_caching(options) unless self.cache_embedded?(options)
unless not force_refresh and cached = send(:read_fragment, options)
View
17 test/rails/app/controllers/test_controller.rb
@@ -26,6 +26,23 @@ def forced_refresh
end
def dump_params
+ render :inline => 'Params: <%= params.keys.sort.collect {|name| "#{name}: #{params[name]}"}.join ", " %>'
end
+
+ def action_with_respond_to
+ respond_to do |format|
+ format.html { render :inline => "html content" }
+ format.embedded { render :inline => "embedded content" }
+ format.all { render :inline => "catch all" }
+ end
+ end
+
+ def action_that_calls_action_with_respond_to
+ render :inline => "<%= embed_action :action => 'action_with_respond_to' %>"
+ end
+
+ def inline_erb_action
+ render :inline => params[:erb]
+ end
end
View
4 test/rails/test/test_helper.rb
@@ -37,11 +37,11 @@ class Test::Unit::TestCase
def assert_embed_erb(result, erb, msg = nil)
- TestController.send(:define_method, :test_action, Proc.new do
+ (class << @controller; self; end).send(:define_method, :test_action_for_assert_embed_erb, Proc.new do
render :inline => erb
end)
- get :test_action
+ get :test_action_for_assert_embed_erb
assert_equal result, @response.body, msg
end
end
View
36 test/test_default_embedded_options.rb
@@ -9,13 +9,7 @@
# Re-raise errors caught by the controller.
class TestController; def rescue_action(e) raise e end; end
-class TestController
- def default_url_options(options)
- {:id => 15, :category => "red"}
- end
-end
-
-class TestControllerWithDefaultUrlOptions < TestController
+class TestControllerWithLessDefaultUrlOptions < TestController
def default_url_options(options)
{:id => 15}
end
@@ -38,20 +32,22 @@ def default_embedded_options(options)
class DefaultEmbeddedOptionsTest < Test::Unit::TestCase
def setup
- @controller = TestController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
+ FileUtils.rm_rf "#{RAILS_ROOT}/tmp/cache/test.host"
end
def test_normalize_embedded_options
+ @controller = TestControllerWithMoreDefaultUrlOptions.new
+
assert_equal({:id => 1, :params => {:category => "red"}}, @controller.normalize_embedded_options({:id => 1, :category => "red"}))
assert_equal({:id => 1, :params => {:category => "red"}}, @controller.normalize_embedded_options({:id => 1, :params => {:category => "red"}}))
assert_equal({:params => {:category => "blue"}}, @controller.normalize_embedded_options({:category => "blue"}))
assert_equal({:params => {:category => "blue"}}, @controller.normalize_embedded_options({:category => "red", :params => {:category => "blue"}}))
end
def test_default_url_options
- @controller = TestControllerWithDefaultUrlOptions.new
+ @controller = TestControllerWithLessDefaultUrlOptions.new
assert_equal({:id => 15}, @controller.rewrite_embedded_options({}))
assert_equal({:id => 16}, @controller.rewrite_embedded_options({:id => 16}))
@@ -72,23 +68,19 @@ def test_default_embedded_options
end
def test_embed_action_with_default_options
- @controller = TestController.new
+ @controller = TestControllerWithMoreDefaultUrlOptions.new
- assert_embed_erb "Params: action: dump_params, category: red, controller: test, id: 15\n",
- "<%= embed_action :action => 'dump_params' %>",
- "embed_action should use default options"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params' %>"
+ assert_equal "Params: action: dump_params, category: red, controller: test_controller_with_more_default_url_options, id: 15", @response.body, "embed_action should use default options"
- assert_embed_erb "Params: action: dump_params, category: red, controller: test, id: 16\n",
- "<%= embed_action :action => 'dump_params', :id => 16 %>",
- "embed_action should use default options"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 16 %>"
+ assert_equal "Params: action: dump_params, category: red, controller: test_controller_with_more_default_url_options, id: 16", @response.body, "embed_action should use default options"
- assert_embed_erb "Params: action: dump_params, category: blue, controller: test, id: 16\n",
- "<%= embed_action :action => 'dump_params', :id => 16, :category => 'blue' %>",
- "embed_action should use default options"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 16, :category => 'blue' %>"
+ assert_equal "Params: action: dump_params, category: blue, controller: test_controller_with_more_default_url_options, id: 16", @response.body, "embed_action should use default options"
- assert_embed_erb "Params: action: dump_params, category: blue, controller: test, id: 16\n",
- "<%= embed_action :action => 'dump_params', :id => 16, :params => {:category => 'blue'} %>",
- "embed_action should use default options"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 16, :params => {:category => 'blue'} %>"
+ assert_equal "Params: action: dump_params, category: blue, controller: test_controller_with_more_default_url_options, id: 16", @response.body, "embed_action should use default options"
end
end
View
35 test/test_embedded_action.rb
@@ -18,33 +18,26 @@ def setup
end
def test_embed_action
- assert_embed_erb "Params: action: dump_params, controller: test, id: \n",
- "<%= embed_action :action => 'dump_params' %>",
- "embed_action should accept implicit controller"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params' %>"
+ assert_equal "Params: action: dump_params, controller: test, id: ", @response.body, "embed_action should accept implicit controller"
- assert_embed_erb "Params: action: dump_params, controller: test, id: \n",
- "<%= embed_action :controller => 'test', :action => 'dump_params' %>",
- "embed_action should accept explicit controller"
+ get :inline_erb_action, :erb => "<%= embed_action :controller => 'test', :action => 'dump_params' %>"
+ assert_equal "Params: action: dump_params, controller: test, id: ", @response.body, "embed_action should accept explicit controller"
- assert_embed_erb "Params: action: dump_params, controller: test, id: the id\n",
- "<%= embed_action :action => 'dump_params', :id => 'the id' %>",
- "embed_action should pass the id"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id' %>"
+ assert_equal "Params: action: dump_params, controller: test, id: the id", @response.body, "embed_action should pass the id"
- assert_embed_erb "Params: action: dump_params, color: blue, controller: test, id: the id\n",
- "<%= embed_action :action => 'dump_params', :id => 'the id', :params => {:color => 'blue'} %>",
- "embed_action should pass params as expected"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :params => {:color => 'blue'} %>"
+ assert_equal "Params: action: dump_params, color: blue, controller: test, id: the id", @response.body, "embed_action should pass params as expected"
- assert_embed_erb "Params: action: dump_params, color: blue, controller: test, id: the id\n",
- "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue' %>",
- "embed_action should merge into params anything that's not standard"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue' %>"
+ assert_equal "Params: action: dump_params, color: blue, controller: test, id: the id", @response.body, "embed_action should merge into params anything that's not standard"
- assert_embed_erb "Params: action: dump_params, color: red, controller: test, id: the id\n",
- "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {:color => 'red'} %>",
- "embed_action should override with the contents of params"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {:color => 'red'} %>"
+ assert_equal "Params: action: dump_params, color: red, controller: test, id: the id", @response.body, "embed_action should override with the contents of params"
- assert_embed_erb "Params: action: dump_params, color: red, controller: test, id: the id\n",
- "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {'color' => 'red'} %>",
- "embed_action should allow indifferent access"
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {'color' => 'red'} %>"
+ assert_equal "Params: action: dump_params, color: red, controller: test, id: the id", @response.body, "embed_action should allow indifferent access"
end
end
View
20 test/test_responds_to.rb
@@ -14,26 +14,16 @@ def setup
@controller = TestController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
+ FileUtils.rm_rf "#{RAILS_ROOT}/tmp/cache/test.host"
end
def test_responds_to_embedded
- TestController.class_eval do
- def action_with_respond_to
- respond_to do |format|
- format.html { render :inline => "html content" }
- format.embedded { render :inline => "embedded content" }
- format.all { render :inline => "catch all" }
- end
- end
- end
-
- assert_embed_erb "embedded content",
- "<%= embed_action :action => 'action_with_respond_to' %>",
- "should respond with embedded content"
- assert_equal "text/html", @response.content_type
-
get :action_with_respond_to
assert_equal "html content", @response.body, "should respond with html content"
assert_equal "text/html", @response.content_type
+
+ get :action_that_calls_action_with_respond_to
+ assert_equal "embedded content", @response.body, "should respond with html content"
+ assert_equal "text/html", @response.content_type
end
end

0 comments on commit 6b24948

Please sign in to comment.