require_relative './../test_helper' require 'minitest/autorun' XML_CAMELLOWER = %{ 12 GED House of Leaves } XML_CAMELCASE = %{ 12 GED House of Leaves } XML_UNDERSCORE = %{ 12 GED House of Leaves } XML_DASHES = %{ 12 GED House of Leaves } XML_UPCASE = %{ 12 GED House of Leaves } class BookCase include ROXML xml_reader :book_count, :as => Integer, :required => true xml_reader :big_books, :as => [], :required => true end class BookCaseCamelCase < BookCase xml_convention :camelcase end class BookCaseUnderScore < BookCase xml_convention :underscore end class BookCaseDashes < BookCase xml_convention &:dasherize end class BookCaseCamelLower < BookCase xml_convention {|val| val.camelcase(:lower) } end class BookCaseUpCase < BookCase xml_convention {|val| val.gsub('_', '').upcase } end class InheritedBookCaseCamelCase < BookCaseCamelCase end class InheritedBookCaseUpCase < BookCaseUpCase end # Same as BookCase. Needed to keep BookCase clean for other tests class ParentBookCaseDefault include ROXML xml_reader :book_count, :as => Integer, :required => true xml_reader :big_books, :as => [], :required => true end class InheritedBookCaseDefault < ParentBookCaseDefault end class TestXMLConvention < Minitest::Test # TODO: Test convention applies to xml_name as well... def test_default_convention_is_underscore bc = BookCase.from_xml(XML_UNDERSCORE) assert_has_book_case_info(bc) end [BookCaseUpCase, BookCaseCamelLower, BookCaseDashes, BookCaseUnderScore, BookCaseCamelCase].each do |klass| define_method(:"test_xml_convention_#{klass.to_s.underscore}") do data = :"XML_#{klass.to_s.sub('BookCase', '').upcase}" assert_equal Proc, klass.roxml_naming_convention.class bc = klass.from_xml(Object.const_get(data)) assert_has_book_case_info(bc) end end def test_child_should_inherit_convention_if_it_doesnt_declare_one [InheritedBookCaseUpCase, InheritedBookCaseCamelCase].each do |klass| data = :"XML_#{klass.to_s.sub('InheritedBookCase', '').upcase}" assert_equal Proc, klass.roxml_naming_convention.class bc = klass.from_xml(Object.const_get(data)) assert_has_book_case_info(bc) end end def test_child_should_inherit_convention_even_if_it_is_added_after_child_declaration bc = InheritedBookCaseDefault.from_xml(XML_UNDERSCORE) assert_has_book_case_info(bc) ParentBookCaseDefault.class_eval do xml_convention :dasherize end bc = InheritedBookCaseDefault.from_xml(XML_DASHES) assert_has_book_case_info(bc) assert_raises ROXML::RequiredElementMissing do InheritedBookCaseDefault.from_xml(XML_UNDERSCORE) end end def test_tag_name_should_get_convention_treatment_as_well assert_equal "book-case-dashes", BookCaseDashes.tag_name assert_equal "INHERITEDBOOKCASEUPCASE", InheritedBookCaseUpCase.tag_name assert_equal "InheritedBookCaseCamelCase", InheritedBookCaseCamelCase.tag_name end def assert_has_book_case_info(bc) assert_equal 12, bc.book_count assert_equal ['GED', 'House of Leaves'], bc.big_books end end