1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
require_relative './../test_helper'
require 'minitest/autorun'
# Parent | Child
# :from | no :from |
# -------------------|--------------
# :from | xml_name | xml_name-d
# value | value |
# -------------------|--------------
# :from | parent's |
# value | accessor | un-xml_name-d
# | name |
class Child
include ROXML
end
class NamedChild
include ROXML
xml_name :xml_name_of_child
end
class ParentOfNamedChild
include ROXML
xml_name :parent
xml_accessor :child_accessor_name, :as => NamedChild
end
class ParentOfNamedChildWithFrom
include ROXML
xml_name :parent
xml_accessor :child_accessor_name, :as => NamedChild, :from => 'child_from_name'
end
class ParentOfUnnamedChild
include ROXML
xml_name :parent
xml_accessor :child_accessor_name, :as => Child
end
class ParentOfUnnamedChildWithFrom
include ROXML
xml_name :parent
xml_accessor :child_accessor_name,:as => Child, :from => 'child_from_name'
end
class TestXMLName < Minitest::Test
def test_from_always_dominates_attribute_name_xml_name_or_not
parent = ParentOfNamedChildWithFrom.new
parent.child_accessor_name = Child.new
assert_equal "<parent><child_from_name/></parent>", parent.to_xml.to_s.gsub(/[\n ]/, '')
parent = ParentOfUnnamedChildWithFrom.new
parent.child_accessor_name = Child.new
assert_equal "<parent><child_from_name/></parent>", parent.to_xml.to_s.gsub(/[\n ]/, '')
end
def test_attribute_name_comes_from_the_xml_name_value_if_present
parent = ParentOfNamedChild.new
parent.child_accessor_name = Child.new
assert_equal "<parent><xml_name_of_child/></parent>", parent.to_xml.to_s.gsub(/[\n ]/, '')
end
def test_attribute_name_comes_from_parent_accessor_by_default
parent = ParentOfUnnamedChild.new
parent.child_accessor_name = Child.new
assert_equal "<parent><child_accessor_name/></parent>", parent.to_xml.to_s.gsub(/[\n ]/, '')
end
def test_it_should_be_inherited
class_with_inherited_name = Class.new(ParentOfNamedChild)
assert_equal :parent, class_with_inherited_name.tag_name
end
def test_it_should_be_inherited_over_multiple_levels
class_with_inherited_name = Class.new(Class.new(ParentOfNamedChild))
assert_equal :parent, class_with_inherited_name.tag_name
end
def test_named_books_picked_up
named = Library.from_xml(fixture(:library))
assert named.books
assert_equal :book, named.books.first.class.tag_name
end
def test_nameless_books_missing
nameless = LibraryWithBooksOfUnderivableName.from_xml(fixture(:library))
assert nameless.novels.empty?
end
def test_tag_name
assert_equal :dictionary, DictionaryOfTexts.tag_name
dict = DictionaryOfTexts.from_xml(fixture(:dictionary_of_texts))
assert_equal :dictionary, dict.class.tag_name
end
def test_roxml_attrs
assert_equal 'definition', DictionaryOfTexts.roxml_attrs.first.name
assert_equal 'word', DictionaryOfTexts.roxml_attrs.first.hash.key.name
assert_equal 'meaning', DictionaryOfTexts.roxml_attrs.first.hash.value.name
end
def test_xml_name_should_not_be_conventionalized_if_explicitly_set
reference = ROXML::XMLTextRef.new(ROXML::Definition.new(:name, :from => 'georss:name'), WrapModule::InstanceStandin.new)
assert_equal "georss:name", reference.name
end
def test_xml_name_not_screwed_up_by_xml_convention
reference = ROXML::XMLTextRef.new(ROXML::Definition.new(:name, :in => './'), WrapModule::InstanceStandin.new)
assert_equal "name value", reference.value_in(ROXML::XML.parse_string(%(
<Wrapper>
<MoreStuff>
<DeepWrapper>
<Name>name value</Name>
</DeepWrapper>
</MoreStuff>
</Wrapper>
)).root)
end
end
module WrapModule
class BaseClass
include ROXML
xml_convention :camelcase
end
class InstanceStandin < BaseClass
xml_reader :name, :in => './'
end
end
|