#!/usr/bin/env ruby require 'test/unit' currentPath = File.dirname(__FILE__) require File.join( currentPath, '../../lib/masterview' ) require File.join( currentPath, '../test_helper' ) class Parent < MasterView::DirectiveBase metadata :namespace => 'exa', :priority => 'High', :category => 'example', :description => 'Example of a parent directive which takes input from its children' def initialize(dcs) super @children_ids = [] end def add_child_id(id) @children_ids.push id end event :after_etag do render @children_ids.inspect end end class Child < MasterView::DirectiveBase metadata :namespace => 'exa', :category => 'example', :description => 'Example of a child directive which communicates with the parent' event :stag do parent = find_parent_directive(Parent) parent.add_child_id(attr_value) if parent end end class ChildWithBlock < MasterView::DirectiveBase metadata :namespace => 'exa', :category => 'example', :description => 'Example of a child directive which contains a filter block which communicates with the parent' event :stag do parent = find_parent_directive(Parent){ |d| d.attr_value=="hello" } parent.add_child_id(attr_value) if parent end end class TestFindDirectiveParent < Test::Unit::TestCase def setup MasterView::IOMgr.erb = MasterView::MIO::StringHashMIOTree.new({}, '.rhtml', :logging => true) end def test_find_multiple_levels template = <<-END
Hello World World
END expected = { 'foo/bar' => %q{
HelloWorldWorld
["a", "b", "c"]
} } assert_template_result expected, template end def test_find_checking_dir_class template = <<-END
Hello World World
END expected = { 'foo/bar' => %q{
HelloWorldWorld
["a", "b", "c"]
} } assert_template_result expected, template end def test_find_multiple_parents template = <<-END
Hello World World
END expected = { 'foo/bar' => %q{
HelloWorldWorld["b", "c"]
["a"]
} } assert_template_result expected, template end def test_find_parent_not_found template = <<-END
Hello World World
END expected = { 'foo/bar' => %q{
HelloWorldWorld
} } assert_template_result expected, template end def test_find_multiple_parents_using_filter_block template = <<-END
Hello World World
END expected = { 'foo/bar' => %q{
HelloWorldWorld[]
["a", "b", "c"]
} } assert_template_result expected, template end def test_find_multiple_parents_using_filter_block_not_found template = <<-END
Hello World World
END expected = { 'foo/bar' => %q{
HelloWorldWorld[]
[]
} } assert_template_result expected, template end end