Skip to content

Commit

Permalink
Modify assert_template to use notifications. Also, remove ActionContr…
Browse files Browse the repository at this point in the history
…oller::Base#template since it is no longer needed.
  • Loading branch information
Carlhuda committed Mar 17, 2010
1 parent 7872fa9 commit d9375f3
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 79 deletions.
9 changes: 0 additions & 9 deletions actionpack/lib/action_controller/metal/compatibility.rb
Expand Up @@ -40,15 +40,6 @@ def rescue_action(env)
def initialize_template_class(*) end
def assign_shortcuts(*) end

def template
@template ||= view_context
end

def process_action(*)
template
super
end

def _normalize_options(options)
if options[:action] && options[:action].to_s.include?(?/)
ActiveSupport::Deprecation.warn "Giving a path to render :action is deprecated. " <<
Expand Down
20 changes: 20 additions & 0 deletions actionpack/lib/action_controller/test_case.rb
Expand Up @@ -13,6 +13,13 @@ module TemplateAssertions
def setup_subscriptions
@partials = Hash.new(0)
@templates = Hash.new(0)
@layouts = Hash.new(0)

ActiveSupport::Notifications.subscribe("action_view.render_template") do |name, start, finish, id, payload|
path = payload[:layout]
@layouts[path] += 1
end

ActiveSupport::Notifications.subscribe("action_view.render_template!") do |name, start, finish, id, payload|
path = payload[:virtual_path]
next unless path
Expand Down Expand Up @@ -69,6 +76,19 @@ def assert_template(options = {}, message = nil)
"expecting ? to be rendered ? time(s) but rendered ? time(s)",
expected_partial, expected_count, actual_count)
assert(actual_count == expected_count.to_i, msg)
elsif options.key?(:layout)
msg = build_message(message,
"expecting layout <?> but action rendered <?>",
expected_layout, @layouts.keys)

case layout = options[:layout]
when String
assert(@layouts.include?(expected_layout), msg)
when Regexp
assert(@layouts.any? {|l| l =~ layout }, msg)
when nil
assert(@layouts.empty?, msg)
end
else
msg = build_message(message,
"expecting partial <?> but action rendered <?>",
Expand Down
9 changes: 7 additions & 2 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -196,7 +196,7 @@ class << self
end

attr_accessor :base_path, :assigns, :template_extension, :lookup_context
attr_internal :captures, :request, :layout, :controller, :template, :config
attr_internal :captures, :request, :controller, :template, :config

delegate :find_template, :template_exists?, :formats, :formats=, :locale, :locale=,
:view_paths, :view_paths=, :with_fallbacks, :update_details, :to => :lookup_context
Expand All @@ -206,6 +206,11 @@ class << self

delegate :logger, :to => :controller, :allow_nil => true

# TODO: HACK FOR RJS
def view_context
self
end

def self.xss_safe? #:nodoc:
true
end
Expand Down Expand Up @@ -254,7 +259,7 @@ def initialize(lookup_context = nil, assigns_for_first_render = {}, controller =
@helpers = self.class.helpers || Module.new

@_controller = controller
@_config = ActiveSupport::InheritableOptions.new(controller.config) if controller
@_config = ActiveSupport::InheritableOptions.new(controller.config) if controller && controller.respond_to?(:config)
@_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
@_virtual_path = nil

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/prototype_helper.rb
Expand Up @@ -582,7 +582,7 @@ def method_missing(method, *arguments)
# page.hide 'spinner'
# end
def update_page(&block)
JavaScriptGenerator.new(@template, &block).to_s.html_safe
JavaScriptGenerator.new(view_context, &block).to_s.html_safe
end

# Works like update_page but wraps the generated JavaScript in a <script>
Expand Down
3 changes: 1 addition & 2 deletions actionpack/lib/action_view/render/rendering.rb
Expand Up @@ -53,13 +53,12 @@ def _render_template(template, layout = nil, options = {}) #:nodoc:
layout = find_layout(layout) if layout

ActiveSupport::Notifications.instrument("action_view.render_template",
:identifier => template.identifier, :layout => layout.try(:identifier)) do
:identifier => template.identifier, :layout => layout.try(:virtual_path)) do

content = template.render(self, locals) { |*name| _layout_for(*name) }
@_content_for[:layout] = content

if layout
@_layout = layout.identifier
content = _render_layout(layout, locals)
end

Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_view/test_case.rb
Expand Up @@ -4,6 +4,8 @@
module ActionView
class TestCase < ActiveSupport::TestCase
class TestController < ActionController::Base
include ActionDispatch::TestProcess

attr_accessor :request, :response, :params

def self.controller_path
Expand Down Expand Up @@ -44,7 +46,7 @@ def setup_with_controller
end

def config
@controller.config
@controller.config if @controller.respond_to?(:config)
end

def render(options = {}, local_assigns = {}, &block)
Expand Down
8 changes: 5 additions & 3 deletions actionpack/test/controller/action_pack_assertions_test.rb
Expand Up @@ -181,6 +181,8 @@ def redirect_to_top_level_named_route
end
end

# require "action_dispatch/test_process"

# a test case to exercise the new capabilities TestRequest & TestResponse
class ActionPackAssertionsControllerTest < ActionController::TestCase
# -- assertion-based testing ------------------------------------------------
Expand Down Expand Up @@ -303,14 +305,14 @@ def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in
# make sure that the template objects exist
def test_template_objects_alive
process :assign_this
assert !@controller.template.instance_variable_get(:"@hi")
assert @controller.template.instance_variable_get(:"@howdy")
assert !@controller.instance_variable_get(:"@hi")
assert @controller.instance_variable_get(:"@howdy")
end

# make sure we don't have template objects when we shouldn't
def test_template_object_missing
process :nothing
assert_nil @controller.template.assigns['howdy']
assert_nil @controller.instance_variable_get(:@howdy)
end

# check the empty flashing
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/filters_test.rb
Expand Up @@ -651,9 +651,9 @@ def test_conditional_skipping_of_filters
assert_equal %w( ensure_login find_user ), assigns["ran_filter"]

test_process(ConditionalSkippingController, "login")
assert_nil @controller.template.controller.instance_variable_get("@ran_after_filter")
assert_nil @controller.instance_variable_get("@ran_after_filter")
test_process(ConditionalSkippingController, "change_password")
assert_equal %w( clean_up ), @controller.template.controller.instance_variable_get("@ran_after_filter")
assert_equal %w( clean_up ), @controller.instance_variable_get("@ran_after_filter")
end

def test_conditional_skipping_of_filters_when_parent_filter_is_also_conditional
Expand Down
16 changes: 8 additions & 8 deletions actionpack/test/controller/layout_test.rb
Expand Up @@ -120,19 +120,19 @@ class LayoutSetInResponseTest < ActionController::TestCase
def test_layout_set_when_using_default_layout
@controller = DefaultLayoutController.new
get :hello
assert @controller.template.layout.include?('layouts/layout_test')
assert_template :layout => "layouts/layout_test"
end

def test_layout_set_when_set_in_controller
@controller = HasOwnLayoutController.new
get :hello
assert @controller.template.layout.include?('layouts/item')
assert_template :layout => "layouts/item"
end

def test_layout_only_exception_when_included
@controller = OnlyLayoutController.new
get :hello
assert @controller.template.layout.include?('layouts/item')
assert_template :layout => "layouts/item"
end

def test_layout_only_exception_when_excepted
Expand All @@ -144,7 +144,7 @@ def test_layout_only_exception_when_excepted
def test_layout_except_exception_when_included
@controller = ExceptLayoutController.new
get :hello
assert @controller.template.layout.include?('layouts/item')
assert_template :layout => "layouts/item"
end

def test_layout_except_exception_when_excepted
Expand All @@ -156,19 +156,19 @@ def test_layout_except_exception_when_excepted
def test_layout_set_when_using_render
@controller = SetsLayoutInRenderController.new
get :hello
assert @controller.template.layout.include?('layouts/third_party_template_library')
assert_template :layout => "layouts/third_party_template_library"
end

def test_layout_is_not_set_when_none_rendered
@controller = RendersNoLayoutController.new
get :hello
assert_nil @controller.template.layout
assert_template :layout => nil
end

def test_layout_is_picked_from_the_controller_instances_view_path
@controller = PrependsViewPathController.new
get :hello
assert @controller.template.layout =~ /layouts\/alt\.\w+/
assert_template :layout => /layouts\/alt\.\w+/
end

def test_absolute_pathed_layout
Expand Down Expand Up @@ -219,7 +219,7 @@ def test_symlinked_layout_is_rendered
@controller = LayoutSymlinkedTest.new
get :hello
assert_response 200
assert @controller.template.layout.include?("layouts/symlinked/symlinked_layout")
assert_template :layout => "layouts/symlinked/symlinked_layout"
end
end
end
6 changes: 3 additions & 3 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -418,7 +418,7 @@ def render_to_string_with_inline_and_render

def rendering_with_conflicting_local_vars
@name = "David"
def @template.name() nil end
def view_context.name() nil end
render :action => "potential_conflicts"
end

Expand Down Expand Up @@ -526,11 +526,11 @@ def partial_with_locals
end

def partial_with_form_builder
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc.new {})
render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}, Proc.new {})
end

def partial_with_form_builder_subclass
render :partial => LabellingFormBuilder.new(:post, nil, @template, {}, Proc.new {})
render :partial => LabellingFormBuilder.new(:post, nil, view_context, {}, Proc.new {})
end

def partial_collection
Expand Down
3 changes: 1 addition & 2 deletions actionpack/test/template/body_parts_test.rb
Expand Up @@ -8,9 +8,8 @@ def response_body() "" end

def index
RENDERINGS.each do |rendering|
@template.punctuate_body! rendering
view_context.punctuate_body! rendering
end
@performed_render = true
end
end

Expand Down
11 changes: 11 additions & 0 deletions actionpack/test/template/erb/form_for_test.rb
@@ -0,0 +1,11 @@
require "abstract_unit"
require "template/erb/helper"

module ERBTest
class TagHelperTest < BlockTestCase
test "form_for works" do
output = render_content "form_for(:staticpage, :url => {:controller => 'blah', :action => 'update'})", ""
assert_equal "<form action=\"/blah/update\" method=\"post\"></form>", output
end
end
end
30 changes: 30 additions & 0 deletions actionpack/test/template/erb/helper.rb
@@ -0,0 +1,30 @@
module ERBTest
class ViewContext
mock_controller = Class.new do
include SharedTestRoutes.url_helpers
end

include ActionView::Helpers::TagHelper
include ActionView::Helpers::JavaScriptHelper
include ActionView::Helpers::FormHelper

attr_accessor :output_buffer

def protect_against_forgery?() false end

define_method(:controller) do
mock_controller.new
end
end

class BlockTestCase < ActiveSupport::TestCase
def render_content(start, inside)
template = block_helper(start, inside)
ActionView::Template::Handlers::Erubis.new(template).evaluate(ViewContext.new)
end

def block_helper(str, rest)
"<%= #{str} do %>#{rest}<% end %>"
end
end
end
40 changes: 3 additions & 37 deletions actionpack/test/template/erb/tag_helper_test.rb
@@ -1,36 +1,10 @@
require "abstract_unit"
require "template/erb/helper"

module ERBTest
class ViewContext
mock_controller = Class.new do
include SharedTestRoutes.url_helpers
end

include ActionView::Helpers::TagHelper
include ActionView::Helpers::JavaScriptHelper
include ActionView::Helpers::FormHelper

attr_accessor :output_buffer

def protect_against_forgery?() false end

define_method(:controller) do
mock_controller.new
end
end

class DeprecatedViewContext < ViewContext
# include ActionView::Helpers::DeprecatedBlockHelpers
end

module SharedTagHelpers
extend ActiveSupport::Testing::Declarative

def render_content(start, inside)
template = block_helper(start, inside)
ActionView::Template::Handlers::Erubis.new(template).evaluate(context.new)
end

def maybe_deprecated
if @deprecated
assert_deprecated { yield }
Expand Down Expand Up @@ -64,23 +38,15 @@ def maybe_deprecated
end
end

class TagHelperTest < ActiveSupport::TestCase
def context
ViewContext
end

class TagHelperTest < BlockTestCase
def block_helper(str, rest)
"<%= #{str} do %>#{rest}<% end %>"
end

include SharedTagHelpers
end

class DeprecatedTagHelperTest < ActiveSupport::TestCase
def context
DeprecatedViewContext
end

class DeprecatedTagHelperTest < BlockTestCase
def block_helper(str, rest)
"<% __in_erb_template=true %><% #{str} do %>#{rest}<% end %>"
end
Expand Down

0 comments on commit d9375f3

Please sign in to comment.