test/unit/xml_hash_test.rb in roxml-2.3.2 vs test/unit/xml_hash_test.rb in roxml-2.4.0
- old
+ new
@@ -1,10 +1,51 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
+class BookWithContributorHash
+ include ROXML
+
+ xml_reader :contributors, {:key => {:attr => 'role'},
+ :value => 'name'}
+end
+
class TestXMLHash < Test::Unit::TestCase
def setup
@contents = {'quaquaversally' => 'adjective: (of a geological formation) sloping downward from the center in all directions.',
'tergiversate' => 'To use evasions or ambiguities; equivocate.'}
+ end
+
+ def test_hash_preserves_data
+ b = BookWithContributorHash.from_xml(%{
+ <book isbn="0974514055">
+ <contributor role="author"><name>David Thomas</name></contributor>
+ <contributor role="supporting author"><name>Andrew Hunt</name></contributor>
+ <contributor role="supporting author"><name>Chad Fowler</name></contributor>
+ </book>
+ })
+ assert_equal({'author' => 'David Thomas', 'supporting author' => ['Andrew Hunt', 'Chad Fowler']},
+ b.contributors)
+ end
+
+ def test_hash_with_object_key_fails
+ assert_raise ArgumentError do
+ Class.new do
+ include ROXML
+
+ xml_reader :object_key_to_text, {:key => BookWithContributorHash,
+ :value => 'text_node'}
+ end
+ end
+ end
+
+ def test_hash_with_object_value_fails
+ assert_raise ArgumentError do
+ Class.new do
+ include ROXML
+
+ xml_reader :key_to_object_value, {:key => {:attr => 'text_node'},
+ :value => BookWithContributorHash}
+ end
+ end
end
def test_attrs_hash
dict = DictionaryOfAttrs.from_xml(fixture(:dictionary_of_attrs))
assert_equal Hash, dict.definitions.class
\ No newline at end of file