test/unit/directive_attr_test.rb in masterview-0.2.5 vs test/unit/directive_attr_test.rb in masterview-0.3.0
- old
+ new
@@ -1,52 +1,128 @@
#!/usr/bin/env ruby
require 'test/unit'
currentPath = File.dirname(__FILE__)
require File.join( currentPath, '../../lib/masterview' )
-require File.join( currentPath, '../../lib/masterview/directives/attr')
+#require File.join( currentPath, '../../lib/masterview/directives/attr')
+require File.join( currentPath, '../directive_test_helper' )
+DirectiveTestHelpers.load_masterview_directive('attr')
class TestAttr < Test::Unit::TestCase
- include MasterView::Directives
+ include DirectiveTestHelpers
+ Attr = MasterView::Directives::Attr # test subject
+
+ ELEMENT_TAG = 'div'
+ TEMPLATE_ELEMENT_ATTR_NAME = 'foo'
+ TEMPLATE_ELEMENT_ATTR_VALUE = 'bar'
+ TEMPLATE_ELEMENT_ATTRIBUTES = { TEMPLATE_ELEMENT_ATTR_NAME => TEMPLATE_ELEMENT_ATTR_VALUE }
+
def setup
- @directives = MasterView::DirectiveSet.new
- @tag = MasterView::Tag.new(@directives, 'foo', {'bar' => 'cat'}, :normal, nil)
+ create_template_element ELEMENT_TAG, :attributes => TEMPLATE_ELEMENT_ATTRIBUTES
end
- def test_hello
- @directives.directives = []
- attr_value = "{:hello => 'world'}"
- @directives << Attr.new(attr_value)
- dcs = @directives.determine_dcs(:stag)
- dcs.context = @tag.create_context
- assert_equal '', dcs.render.join
- assert_equal 'cat', dcs.context[:tag].attributes['bar']
- assert_equal 'world', dcs.context[:tag].attributes['hello']
+ def test_metadata
+ assert_equal MasterView::ConfigSettings.namespace_prefix, Attr.namespace_prefix
end
- def test_hello_hi
- @directives.directives = []
+ def check_element_rendering(expected_unchanged_attributes, expected_changedAttributes, expected_added_attributes)
+
+ num_original_attrs = element_attrs.size() # original attributes on the template element before mv:attr applied
+ num_unchanged_attributes = expected_unchanged_attributes.size()
+ num_changed_attributes = expected_changedAttributes.size()
+ num_new_attributes = expected_added_attributes.size()
+
+ # mv:attr directive manipulates the element's attributes, but doesn't directly act on the output
+ # It thus has an effect when the normal element processing occurs
+ assert_equal '', render_element_event(:stag) ##err... when does the stag actually get emitted?
+
+ assert_equal num_original_attrs, num_unchanged_attributes + num_changed_attributes, 'Oops: programmer error'
+
+ expected_unchanged_attributes.each_pair { | attr_name, expected_value |
+ assert_equal expected_value, element_attr_value(attr_name), 'Original attribute is not affected'
+ }
+ expected_changedAttributes.each_pair { | attr_name, expected_value |
+ assert_equal expected_value, element_attr_value(attr_name), 'Original attribute should be changed'
+ }
+ expected_added_attributes.each_pair { | attr_name, expected_value |
+ assert_equal expected_value, element_attr_value(attr_name), 'New attribute specified in the directive should be added'
+ }
+
+ assert_equal element_attrs.size(), num_original_attrs + num_new_attributes, 'Should end up with exactly original attributes + new attributes'
+
+ end
+
+ # test adding an attribute to the element (wrapped hash arg)
+ def test_add_using_hash_notation
+
+ attr_value = "{ :hello => 'world' }"
+ create_directive Attr, attr_value
+
+ check_element_rendering(
+ TEMPLATE_ELEMENT_ATTRIBUTES, # original
+ {}, # no changes
+ eval(attr_value) # new
+ )
+
+ end
+
+ # test adding multiple attributes to the element (unwrapped hash assocs notation)
+ def test_add_using_bare_keywords_notation
+
attr_value = ":hello => 'world', :hi => 'there'"
- @directives << Attr.new(attr_value)
- dcs = @directives.determine_dcs(:stag)
- dcs.context = @tag.create_context
- assert_equal '', dcs.render.join
- assert_equal 'cat', dcs.context[:tag].attributes['bar']
- assert_equal 'world', dcs.context[:tag].attributes['hello']
- assert_equal 'there', dcs.context[:tag].attributes['hi']
+ create_directive Attr, attr_value
+
+ check_element_rendering(
+ TEMPLATE_ELEMENT_ATTRIBUTES, # original
+ {}, # no changes
+ eval( "{ #{attr_value} }" ) # new
+ )
+
end
- def test_hello_erb
- @directives.directives = []
- attr_value = %q":hello => 'world', :hi => 'there', :wow => #{h product.name}"
- @directives << Attr.new(attr_value)
- dcs = @directives.determine_dcs(:stag)
- dcs.context = @tag.create_context
- assert_equal '', dcs.render.join
- assert_equal 'cat', dcs.context[:tag].attributes['bar']
- assert_equal 'world', dcs.context[:tag].attributes['hello']
- assert_equal 'there', dcs.context[:tag].attributes['hi']
- assert_equal '{{{= h product.name }}}', dcs.context[:tag].attributes['wow']
+ def test_add_attribute_with_erb_subst
+
+ attr_value = %q":hello => 'world', :wow => #{product.name}"
+ erbized_attr_value = '{{{= product.name }}}'
+ create_directive Attr, attr_value
+
+ check_element_rendering(
+ TEMPLATE_ELEMENT_ATTRIBUTES, # original
+ {}, # no changes
+ { :hello => 'world', :wow => erbized_attr_value } # new
+ )
+
+ end
+
+ # test adding multiple attributes to the element with changes to original markup
+ def test_add_and_change_attributes
+
+ modified_template_attr_value = 'wingding'
+ attr_value = ":hello => 'world', :#{TEMPLATE_ELEMENT_ATTR_NAME} => '#{modified_template_attr_value}'"
+ create_directive Attr, attr_value
+
+ check_element_rendering(
+ {}, # no unchanged
+ { TEMPLATE_ELEMENT_ATTR_NAME => modified_template_attr_value }, # changed
+ eval( "{ #{attr_value.split(',')[0]} }" ) # new
+ )
+
+ end
+
+ # test adding multiple attributes to the element with changes to original markup
+ def test_add_and_change_attribute_with_erb_subst
+
+ modified_template_attr_value = '#{product.name}'
+ erbized_attr_value = '{{{= product.name }}}'
+ attr_value = ":hello => 'world', :#{TEMPLATE_ELEMENT_ATTR_NAME} => " << modified_template_attr_value
+ create_directive Attr, attr_value
+
+ check_element_rendering(
+ {}, # no unchanged
+ { TEMPLATE_ELEMENT_ATTR_NAME => erbized_attr_value }, # changed
+ eval( "{ #{attr_value.split(',')[0]} }" ) # new
+ )
+
end
end