From 1d18b9b2fb48664074781aff7db6056058725458 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sat, 19 Nov 2016 10:30:57 -0500 Subject: Do not include `ActiveSupport` into test classes Will help get rid of errors like the following: ``` 1) Error: JDOMEngineTest#test_order=: ArgumentError: wrong number of arguments (0 for 1) /Users/jon/code/rails/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb:106:in `test_order=' ``` --- activesupport/test/xml_mini/jdom_engine_test.rb | 20 +++++++++----------- activesupport/test/xml_mini/libxml_engine_test.rb | 20 +++++++++----------- activesupport/test/xml_mini/libxmlsax_engine_test.rb | 20 +++++++++----------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/activesupport/test/xml_mini/jdom_engine_test.rb b/activesupport/test/xml_mini/jdom_engine_test.rb index 816d57972c..66b15aed4a 100644 --- a/activesupport/test/xml_mini/jdom_engine_test.rb +++ b/activesupport/test/xml_mini/jdom_engine_test.rb @@ -4,17 +4,15 @@ if RUBY_PLATFORM.include?("java") require "active_support/core_ext/hash/conversions" class JDOMEngineTest < ActiveSupport::TestCase - include ActiveSupport - FILES_DIR = File.dirname(__FILE__) + "/../fixtures/xml" def setup - @default_backend = XmlMini.backend - XmlMini.backend = "JDOM" + @default_backend = ActiveSupport::XmlMini.backend + ActiveSupport::XmlMini.backend = "JDOM" end def teardown - XmlMini.backend = @default_backend + ActiveSupport::XmlMini.backend = @default_backend end def test_file_from_xml @@ -84,13 +82,13 @@ if RUBY_PLATFORM.include?("java") end def test_setting_JDOM_as_backend - XmlMini.backend = "JDOM" - assert_equal XmlMini_JDOM, XmlMini.backend + ActiveSupport::XmlMini.backend = "JDOM" + assert_equal ActiveSupport::XmlMini_JDOM, ActiveSupport::XmlMini.backend end def test_blank_returns_empty_hash - assert_equal({}, XmlMini.parse(nil)) - assert_equal({}, XmlMini.parse("")) + assert_equal({}, ActiveSupport::XmlMini.parse(nil)) + assert_equal({}, ActiveSupport::XmlMini.parse("")) end def test_array_type_makes_an_array @@ -175,8 +173,8 @@ if RUBY_PLATFORM.include?("java") private def assert_equal_rexml(xml) - parsed_xml = XmlMini.parse(xml) - hash = XmlMini.with_backend("REXML") { XmlMini.parse(xml) } + parsed_xml = ActiveSupport::XmlMini.parse(xml) + hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } assert_equal(hash, parsed_xml) end end diff --git a/activesupport/test/xml_mini/libxml_engine_test.rb b/activesupport/test/xml_mini/libxml_engine_test.rb index 81b0d3c407..ae3a5562e0 100644 --- a/activesupport/test/xml_mini/libxml_engine_test.rb +++ b/activesupport/test/xml_mini/libxml_engine_test.rb @@ -8,17 +8,15 @@ else require "active_support/core_ext/hash/conversions" class LibxmlEngineTest < ActiveSupport::TestCase - include ActiveSupport - def setup - @default_backend = XmlMini.backend - XmlMini.backend = "LibXML" + @default_backend = ActiveSupport::XmlMini.backend + ActiveSupport::XmlMini.backend = "LibXML" LibXML::XML::Error.set_handler(&lambda { |error| }) #silence libxml, exceptions will do end def teardown - XmlMini.backend = @default_backend + ActiveSupport::XmlMini.backend = @default_backend end def test_exception_thrown_on_expansion_attack @@ -42,13 +40,13 @@ else end def test_setting_libxml_as_backend - XmlMini.backend = "LibXML" - assert_equal XmlMini_LibXML, XmlMini.backend + ActiveSupport::XmlMini.backend = "LibXML" + assert_equal ActiveSupport::XmlMini_LibXML, ActiveSupport::XmlMini.backend end def test_blank_returns_empty_hash - assert_equal({}, XmlMini.parse(nil)) - assert_equal({}, XmlMini.parse("")) + assert_equal({}, ActiveSupport::XmlMini.parse(nil)) + assert_equal({}, ActiveSupport::XmlMini.parse("")) end def test_array_type_makes_an_array @@ -193,9 +191,9 @@ else private def assert_equal_rexml(xml) - parsed_xml = XmlMini.parse(xml) + parsed_xml = ActiveSupport::XmlMini.parse(xml) xml.rewind if xml.respond_to?(:rewind) - hash = XmlMini.with_backend("REXML") { XmlMini.parse(xml) } + hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } assert_equal(hash, parsed_xml) end end diff --git a/activesupport/test/xml_mini/libxmlsax_engine_test.rb b/activesupport/test/xml_mini/libxmlsax_engine_test.rb index e25fa2813c..f52afd273e 100644 --- a/activesupport/test/xml_mini/libxmlsax_engine_test.rb +++ b/activesupport/test/xml_mini/libxmlsax_engine_test.rb @@ -8,15 +8,13 @@ else require "active_support/core_ext/hash/conversions" class LibXMLSAXEngineTest < ActiveSupport::TestCase - include ActiveSupport - def setup - @default_backend = XmlMini.backend - XmlMini.backend = "LibXMLSAX" + @default_backend = ActiveSupport::XmlMini.backend + ActiveSupport::XmlMini.backend = "LibXMLSAX" end def teardown - XmlMini.backend = @default_backend + ActiveSupport::XmlMini.backend = @default_backend end def test_exception_thrown_on_expansion_attack @@ -42,13 +40,13 @@ else end def test_setting_libxml_as_backend - XmlMini.backend = "LibXMLSAX" - assert_equal XmlMini_LibXMLSAX, XmlMini.backend + ActiveSupport::XmlMini.backend = "LibXMLSAX" + assert_equal ActiveSupport::XmlMini_LibXMLSAX, ActiveSupport::XmlMini.backend end def test_blank_returns_empty_hash - assert_equal({}, XmlMini.parse(nil)) - assert_equal({}, XmlMini.parse("")) + assert_equal({}, ActiveSupport::XmlMini.parse(nil)) + assert_equal({}, ActiveSupport::XmlMini.parse("")) end def test_array_type_makes_an_array @@ -185,9 +183,9 @@ else private def assert_equal_rexml(xml) - parsed_xml = XmlMini.parse(xml) + parsed_xml = ActiveSupport::XmlMini.parse(xml) xml.rewind if xml.respond_to?(:rewind) - hash = XmlMini.with_backend("REXML") { XmlMini.parse(xml) } + hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } assert_equal(hash, parsed_xml) end end -- cgit v1.2.3 From 3256dcd8b9b46c50d9b51524bc44d2e3032fd801 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sat, 19 Nov 2016 10:46:28 -0500 Subject: Install libxml-ruby gem so full XMLMini test suite can be run --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 10e146d887..37e5b4fd25 100644 --- a/Gemfile +++ b/Gemfile @@ -48,6 +48,7 @@ end # Active Support. gem "dalli", ">= 2.2.1" gem "listen", ">= 3.0.5", "< 3.2", require: false +gem "libxml-ruby", platforms: :ruby # Active Job. group :job do diff --git a/Gemfile.lock b/Gemfile.lock index 06e822964b..bec01cda4a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -214,6 +214,7 @@ GEM kindlerb (1.0.1) mustache nokogiri + libxml-ruby (2.9.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -381,6 +382,7 @@ DEPENDENCIES jquery-rails json (>= 2.0.0) kindlerb (>= 1.0.1) + libxml-ruby listen (>= 3.0.5, < 3.2) minitest (< 5.3.4) mocha (~> 0.14) -- cgit v1.2.3 From 305e2922a4b358f0ae5630672b6144d3013b603d Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sat, 19 Nov 2016 10:59:24 -0500 Subject: Create `CommonXMLMiniAdapterTest`; common tests module Created a new module (a la Action Cable subscription adapter's test suite) to be included in all sub class test to ensure compatability and reduce duplicated code. --- activesupport/test/xml_mini/common.rb | 229 +++++++++++++++++++++ activesupport/test/xml_mini/jdom_engine_test.rb | 142 +------------ activesupport/test/xml_mini/libxml_engine_test.rb | 190 +---------------- .../test/xml_mini/libxmlsax_engine_test.rb | 183 +--------------- .../test/xml_mini/nokogiri_engine_test.rb | 205 +----------------- .../test/xml_mini/nokogirisax_engine_test.rb | 206 +----------------- activesupport/test/xml_mini/rexml_engine_test.rb | 40 ++-- 7 files changed, 279 insertions(+), 916 deletions(-) create mode 100644 activesupport/test/xml_mini/common.rb diff --git a/activesupport/test/xml_mini/common.rb b/activesupport/test/xml_mini/common.rb new file mode 100644 index 0000000000..900fb31ba3 --- /dev/null +++ b/activesupport/test/xml_mini/common.rb @@ -0,0 +1,229 @@ +module CommonXMLMiniAdapterTest + def setup + @default_backend = ActiveSupport::XmlMini.backend + ActiveSupport::XmlMini.backend = adapter_name + end + + def teardown + ActiveSupport::XmlMini.backend = @default_backend + end + + def test_file_from_xml + hash = Hash.from_xml(<<-eoxml) + + + + + eoxml + assert hash.has_key?("blog") + assert hash["blog"].has_key?("logo") + + file = hash["blog"]["logo"] + assert_equal "logo.png", file.original_filename + assert_equal "image/png", file.content_type + end + + def test_exception_thrown_on_expansion_attack + assert_raise expansion_attack_error do + attack_xml = <<-EOT + + + + + + + + + ]> + + &a; + + EOT + Hash.from_xml(attack_xml) + end + end + + def test_setting_backend + ActiveSupport::XmlMini.backend = adapter_name + assert_equal ActiveSupport.const_get("XmlMini_#{adapter_name}"), ActiveSupport::XmlMini.backend + #assert_equal ActiveSupport::XmlMini_Nokogiri + end + + def test_blank_returns_empty_hash + assert_equal({}, ActiveSupport::XmlMini.parse(nil)) + assert_equal({}, ActiveSupport::XmlMini.parse("")) + end + + def test_array_type_makes_an_array + assert_equal_rexml(<<-eoxml) + + + a post + another post + + + eoxml + end + + def test_one_node_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_one_node_with_attributes_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_products_node_with_book_node_as_hash + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + def test_products_node_with_two_book_nodes_as_hash + assert_equal_rexml(<<-eoxml) + + + + + eoxml + end + + def test_single_node_with_content_as_hash + assert_equal_rexml(<<-eoxml) + + hello world + + eoxml + end + + def test_children_with_children + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text + assert_equal_rexml(<<-eoxml) + + + hello everyone + + + eoxml + end + + def test_children_with_non_adjacent_text + assert_equal_rexml(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + end + + def test_parse_from_io + jdom_skip + + io = StringIO.new(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + assert_equal_rexml(io) + end + + def test_children_with_simple_cdata + jdom_skip + + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_multiple_cdata + jdom_skip + + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text_and_cdata + jdom_skip + + assert_equal_rexml(<<-eoxml) + + + hello + morning + + + eoxml + end + + def test_children_with_blank_text + jdom_skip + + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + def test_children_with_blank_text_and_attribute + jdom_skip + + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + private + def adapter_name + raise NotImplementedError + end + + def assert_equal_rexml(xml) + parsed_xml = ActiveSupport::XmlMini.parse(xml) + xml.rewind if xml.respond_to?(:rewind) + hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } + assert_equal(hash, parsed_xml) + end + + def expansion_attack_error + raise NotImplementedError + end + + def jdom_skip + skip "JDOM does not support this test" if adapter_name == "JDOM" + end +end diff --git a/activesupport/test/xml_mini/jdom_engine_test.rb b/activesupport/test/xml_mini/jdom_engine_test.rb index 66b15aed4a..f42fc195cc 100644 --- a/activesupport/test/xml_mini/jdom_engine_test.rb +++ b/activesupport/test/xml_mini/jdom_engine_test.rb @@ -2,33 +2,12 @@ if RUBY_PLATFORM.include?("java") require "abstract_unit" require "active_support/xml_mini" require "active_support/core_ext/hash/conversions" + require_relative "./common" class JDOMEngineTest < ActiveSupport::TestCase FILES_DIR = File.dirname(__FILE__) + "/../fixtures/xml" - def setup - @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = "JDOM" - end - - def teardown - ActiveSupport::XmlMini.backend = @default_backend - end - - def test_file_from_xml - hash = Hash.from_xml(<<-eoxml) - - - - - eoxml - assert hash.has_key?("blog") - assert hash["blog"].has_key?("logo") - - file = hash["blog"]["logo"] - assert_equal "logo.png", file.original_filename - assert_equal "image/png", file.content_type - end + include CommonXMLMiniAdapterTest def test_not_allowed_to_expand_entities_to_files attack_xml = <<-EOT @@ -61,121 +40,14 @@ if RUBY_PLATFORM.include?("java") assert_equal "x", Hash.from_xml(attack_xml)["member"] end - def test_exception_thrown_on_expansion_attack - assert_raise Java::OrgXmlSax::SAXParseException do - attack_xml = <<-EOT - - - - - - - - ]> - - &a; - - EOT - Hash.from_xml(attack_xml) - end - end - - def test_setting_JDOM_as_backend - ActiveSupport::XmlMini.backend = "JDOM" - assert_equal ActiveSupport::XmlMini_JDOM, ActiveSupport::XmlMini.backend - end - - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end - - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end - - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end - - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end + private - def test_children_with_text - assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml + def adapter_name + "JDOM" end - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml + def expansion_attack_error + Java::OrgXmlSax::SAXParseException end - - private - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end end end diff --git a/activesupport/test/xml_mini/libxml_engine_test.rb b/activesupport/test/xml_mini/libxml_engine_test.rb index ae3a5562e0..ff4759b082 100644 --- a/activesupport/test/xml_mini/libxml_engine_test.rb +++ b/activesupport/test/xml_mini/libxml_engine_test.rb @@ -6,196 +6,24 @@ else require "abstract_unit" require "active_support/xml_mini" require "active_support/core_ext/hash/conversions" + require_relative "./common" class LibxmlEngineTest < ActiveSupport::TestCase - def setup - @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = "LibXML" + include CommonXMLMiniAdapterTest + def setup + super LibXML::XML::Error.set_handler(&lambda { |error| }) #silence libxml, exceptions will do end - def teardown - ActiveSupport::XmlMini.backend = @default_backend - end - - def test_exception_thrown_on_expansion_attack - assert_raise LibXML::XML::Error do - attack_xml = %{ - - - - - - - - ]> - - &a; - - } - Hash.from_xml(attack_xml) - end - end - - def test_setting_libxml_as_backend - ActiveSupport::XmlMini.backend = "LibXML" - assert_equal ActiveSupport::XmlMini_LibXML, ActiveSupport::XmlMini.backend - end - - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end - - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end - - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end - - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text - assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml - end - - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - end - - def test_parse_from_io - io = StringIO.new(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - assert_equal_rexml(io) - end - - def test_children_with_simple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_multiple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text_and_cdata - assert_equal_rexml(<<-eoxml) - - - hello - morning - - - eoxml - end + private - def test_children_with_blank_text - assert_equal_rexml(<<-eoxml) - - - - eoxml + def adapter_name + "LibXML" end - def test_children_with_blank_text_and_attribute - assert_equal_rexml(<<-eoxml) - - - - eoxml + def expansion_attack_error + LibXML::XML::Error end - - private - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end end - end diff --git a/activesupport/test/xml_mini/libxmlsax_engine_test.rb b/activesupport/test/xml_mini/libxmlsax_engine_test.rb index f52afd273e..73eff9ac44 100644 --- a/activesupport/test/xml_mini/libxmlsax_engine_test.rb +++ b/activesupport/test/xml_mini/libxmlsax_engine_test.rb @@ -6,188 +6,19 @@ else require "abstract_unit" require "active_support/xml_mini" require "active_support/core_ext/hash/conversions" + require_relative "./common" class LibXMLSAXEngineTest < ActiveSupport::TestCase - def setup - @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = "LibXMLSAX" - end - - def teardown - ActiveSupport::XmlMini.backend = @default_backend - end - - def test_exception_thrown_on_expansion_attack - assert_raise LibXML::XML::Error do - attack_xml = <<-EOT - - - - - - - - - ]> - - &a; - - EOT - - Hash.from_xml(attack_xml) - end - end - - def test_setting_libxml_as_backend - ActiveSupport::XmlMini.backend = "LibXMLSAX" - assert_equal ActiveSupport::XmlMini_LibXMLSAX, ActiveSupport::XmlMini.backend - end - - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end + include CommonXMLMiniAdapterTest - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end - - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end - - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text - assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml - end - - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - end - - def test_parse_from_io - io = StringIO.new(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - assert_equal_rexml(io) - end - - def test_children_with_simple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_multiple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end + private - def test_children_with_text_and_cdata - assert_equal_rexml(<<-eoxml) - - - hello - morning - - - eoxml + def adapter_name + "LibXMLSAX" end - def test_children_with_blank_text - assert_equal_rexml(<<-eoxml) - - - - eoxml + def expansion_attack_error + LibXML::XML::Error end - - private - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end end - end diff --git a/activesupport/test/xml_mini/nokogiri_engine_test.rb b/activesupport/test/xml_mini/nokogiri_engine_test.rb index 44b82da4e4..b0c4985a50 100644 --- a/activesupport/test/xml_mini/nokogiri_engine_test.rb +++ b/activesupport/test/xml_mini/nokogiri_engine_test.rb @@ -6,210 +6,19 @@ else require "abstract_unit" require "active_support/xml_mini" require "active_support/core_ext/hash/conversions" + require_relative "./common" class NokogiriEngineTest < ActiveSupport::TestCase - def setup - @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = "Nokogiri" - end - - def teardown - ActiveSupport::XmlMini.backend = @default_backend - end - - def test_file_from_xml - hash = Hash.from_xml(<<-eoxml) - - - - - eoxml - assert hash.has_key?("blog") - assert hash["blog"].has_key?("logo") - - file = hash["blog"]["logo"] - assert_equal "logo.png", file.original_filename - assert_equal "image/png", file.content_type - end + include CommonXMLMiniAdapterTest - def test_exception_thrown_on_expansion_attack - assert_raise Nokogiri::XML::SyntaxError do - attack_xml = <<-EOT - - - - - - - - - ]> - - &a; - - EOT - Hash.from_xml(attack_xml) - end - end - - def test_setting_nokogiri_as_backend - ActiveSupport::XmlMini.backend = "Nokogiri" - assert_equal ActiveSupport::XmlMini_Nokogiri, ActiveSupport::XmlMini.backend - end - - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end - - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end - - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end - - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text - assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml - end - - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - end - - def test_parse_from_io - io = StringIO.new(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - assert_equal_rexml(io) - end - - def test_children_with_simple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_multiple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text_and_cdata - assert_equal_rexml(<<-eoxml) - - - hello - morning - - - eoxml - end + private - def test_children_with_blank_text - assert_equal_rexml(<<-eoxml) - - - - eoxml + def expansion_attack_error + Nokogiri::XML::SyntaxError end - def test_children_with_blank_text_and_attribute - assert_equal_rexml(<<-eoxml) - - - - eoxml + def adapter_name + "Nokogiri" end - - private - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end end - end diff --git a/activesupport/test/xml_mini/nokogirisax_engine_test.rb b/activesupport/test/xml_mini/nokogirisax_engine_test.rb index 24b35cadf6..5be339b826 100644 --- a/activesupport/test/xml_mini/nokogirisax_engine_test.rb +++ b/activesupport/test/xml_mini/nokogirisax_engine_test.rb @@ -6,211 +6,19 @@ else require "abstract_unit" require "active_support/xml_mini" require "active_support/core_ext/hash/conversions" + require_relative "./common" class NokogiriSAXEngineTest < ActiveSupport::TestCase - def setup - @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = "NokogiriSAX" - end - - def teardown - ActiveSupport::XmlMini.backend = @default_backend - end - - def test_file_from_xml - hash = Hash.from_xml(<<-eoxml) - - - - - eoxml - assert hash.has_key?("blog") - assert hash["blog"].has_key?("logo") - - file = hash["blog"]["logo"] - assert_equal "logo.png", file.original_filename - assert_equal "image/png", file.content_type - end - - def test_exception_thrown_on_expansion_attack - assert_raise RuntimeError do - attack_xml = <<-EOT - - - - - - - - - ]> - - &a; - - EOT - - Hash.from_xml(attack_xml) - end - end - - def test_setting_nokogirisax_as_backend - ActiveSupport::XmlMini.backend = "NokogiriSAX" - assert_equal ActiveSupport::XmlMini_NokogiriSAX, ActiveSupport::XmlMini.backend - end - - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end - - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end + include CommonXMLMiniAdapterTest - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end - - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end - - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text - assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml - end - - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - end - - def test_parse_from_io - io = StringIO.new(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - assert_equal_rexml(io) - end - - def test_children_with_simple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_multiple_cdata - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text_and_cdata - assert_equal_rexml(<<-eoxml) - - - hello - morning - - - eoxml - end + private - def test_children_with_blank_text - assert_equal_rexml(<<-eoxml) - - - - eoxml + def adapter_name + "NokogiriSAX" end - def test_children_with_blank_text_and_attribute - assert_equal_rexml(<<-eoxml) - - - - eoxml + def expansion_attack_error + RuntimeError end - - private - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end end - end diff --git a/activesupport/test/xml_mini/rexml_engine_test.rb b/activesupport/test/xml_mini/rexml_engine_test.rb index dc62f3f671..0f4e690746 100644 --- a/activesupport/test/xml_mini/rexml_engine_test.rb +++ b/activesupport/test/xml_mini/rexml_engine_test.rb @@ -1,44 +1,30 @@ require "abstract_unit" require "active_support/xml_mini" +require_relative "./common" class REXMLEngineTest < ActiveSupport::TestCase - def test_default_is_rexml - assert_equal ActiveSupport::XmlMini_REXML, ActiveSupport::XmlMini.backend - end + include CommonXMLMiniAdapterTest - def test_set_rexml_as_backend - ActiveSupport::XmlMini.backend = "REXML" + def test_default_is_rexml assert_equal ActiveSupport::XmlMini_REXML, ActiveSupport::XmlMini.backend end - def test_parse_from_io - ActiveSupport::XmlMini.backend = "REXML" - io = StringIO.new(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - hash = ActiveSupport::XmlMini.parse(io) - assert hash.has_key?("root") - assert hash["root"].has_key?("products") - assert_match "good", hash["root"]["__content__"] - products = hash["root"]["products"] - assert products.has_key?("__content__") - assert_match "hello everyone", products["__content__"] - end - def test_parse_from_empty_string - ActiveSupport::XmlMini.backend = "REXML" assert_equal({}, ActiveSupport::XmlMini.parse("")) end def test_parse_from_frozen_string - ActiveSupport::XmlMini.backend = "REXML" xml_string = "".freeze assert_equal({ "root" => {} }, ActiveSupport::XmlMini.parse(xml_string)) end + + private + + def adapter_name + "REXML" + end + + def expansion_attack_error + RuntimeError + end end -- cgit v1.2.3 From 2bdc4ddc065cb395f287f1511f9cb14059a66652 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sun, 20 Nov 2016 09:06:17 -0500 Subject: Code styling cleanup --- activesupport/test/xml_mini/common.rb | 238 +++++++++++---------- activesupport/test/xml_mini/jdom_engine_test.rb | 16 +- activesupport/test/xml_mini/libxml_engine_test.rb | 12 +- .../test/xml_mini/libxmlsax_engine_test.rb | 12 +- .../test/xml_mini/nokogiri_engine_test.rb | 12 +- .../test/xml_mini/nokogirisax_engine_test.rb | 12 +- activesupport/test/xml_mini/rexml_engine_test.rb | 12 +- 7 files changed, 162 insertions(+), 152 deletions(-) diff --git a/activesupport/test/xml_mini/common.rb b/activesupport/test/xml_mini/common.rb index 900fb31ba3..c97bbf7e35 100644 --- a/activesupport/test/xml_mini/common.rb +++ b/activesupport/test/xml_mini/common.rb @@ -1,22 +1,24 @@ module CommonXMLMiniAdapterTest def setup @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = adapter_name + ActiveSupport::XmlMini.backend = engine + super end def teardown ActiveSupport::XmlMini.backend = @default_backend + super end def test_file_from_xml hash = Hash.from_xml(<<-eoxml) - - - - - eoxml - assert hash.has_key?("blog") - assert hash["blog"].has_key?("logo") + + + + + eoxml + assert hash.key?("blog") + assert hash["blog"].key?("logo") file = hash["blog"]["logo"] assert_equal "logo.png", file.original_filename @@ -25,29 +27,26 @@ module CommonXMLMiniAdapterTest def test_exception_thrown_on_expansion_attack assert_raise expansion_attack_error do - attack_xml = <<-EOT - - - - - - - - - ]> - - &a; - - EOT - Hash.from_xml(attack_xml) + Hash.from_xml(<<-eoxml) + + + + + + + + + ]> + + &a; + + eoxml end end def test_setting_backend - ActiveSupport::XmlMini.backend = adapter_name - assert_equal ActiveSupport.const_get("XmlMini_#{adapter_name}"), ActiveSupport::XmlMini.backend - #assert_equal ActiveSupport::XmlMini_Nokogiri + assert_engine_class ActiveSupport::XmlMini.backend end def test_blank_returns_empty_hash @@ -57,161 +56,164 @@ module CommonXMLMiniAdapterTest def test_array_type_makes_an_array assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml + + + a post + another post + + + eoxml end def test_one_node_document_as_hash assert_equal_rexml(<<-eoxml) - + eoxml end def test_one_node_with_attributes_document_as_hash assert_equal_rexml(<<-eoxml) - + eoxml end def test_products_node_with_book_node_as_hash assert_equal_rexml(<<-eoxml) - - - - eoxml + + + + eoxml end def test_products_node_with_two_book_nodes_as_hash assert_equal_rexml(<<-eoxml) - - - - - eoxml + + + + + eoxml end def test_single_node_with_content_as_hash assert_equal_rexml(<<-eoxml) - - hello world - - eoxml + + hello world + + eoxml end def test_children_with_children assert_equal_rexml(<<-eoxml) - - - - - - eoxml + + + + + + eoxml end def test_children_with_text assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml + + + hello everyone + + + eoxml end def test_children_with_non_adjacent_text assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml + + good + + hello everyone + + morning + + eoxml end def test_parse_from_io - jdom_skip - - io = StringIO.new(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - assert_equal_rexml(io) + skip_unless_extended_engine + + assert_equal_rexml(StringIO.new(<<-eoxml)) + + good + + hello everyone + + morning + + eoxml end def test_children_with_simple_cdata - jdom_skip + skip_unless_extended_engine assert_equal_rexml(<<-eoxml) - - - - - - eoxml + + + + + + eoxml end def test_children_with_multiple_cdata - jdom_skip + skip_unless_extended_engine assert_equal_rexml(<<-eoxml) - - - - - - eoxml + + + + + + eoxml end def test_children_with_text_and_cdata - jdom_skip + skip_unless_extended_engine assert_equal_rexml(<<-eoxml) - - - hello - morning - - - eoxml + + + hello + morning + + + eoxml end def test_children_with_blank_text - jdom_skip + skip_unless_extended_engine assert_equal_rexml(<<-eoxml) - - - - eoxml + + + + eoxml end def test_children_with_blank_text_and_attribute - jdom_skip + skip_unless_extended_engine assert_equal_rexml(<<-eoxml) - - - - eoxml + + + + eoxml end private - def adapter_name + def engine raise NotImplementedError end + def assert_engine_class(actual) + assert_equal ActiveSupport.const_get("XmlMini_#{engine}"), actual + end + def assert_equal_rexml(xml) parsed_xml = ActiveSupport::XmlMini.parse(xml) xml.rewind if xml.respond_to?(:rewind) @@ -223,7 +225,11 @@ module CommonXMLMiniAdapterTest raise NotImplementedError end - def jdom_skip - skip "JDOM does not support this test" if adapter_name == "JDOM" + def extended_engine? + true + end + + def skip_unless_extended_engine + skip "#{engine} is not an extended engine" unless extended_engine? end end diff --git a/activesupport/test/xml_mini/jdom_engine_test.rb b/activesupport/test/xml_mini/jdom_engine_test.rb index f42fc195cc..3034bc7148 100644 --- a/activesupport/test/xml_mini/jdom_engine_test.rb +++ b/activesupport/test/xml_mini/jdom_engine_test.rb @@ -42,12 +42,16 @@ if RUBY_PLATFORM.include?("java") private - def adapter_name - "JDOM" - end + def engine + "JDOM" + end - def expansion_attack_error - Java::OrgXmlSax::SAXParseException - end + def expansion_attack_error + Java::OrgXmlSax::SAXParseException + end + + def extended_engine? + false + end end end diff --git a/activesupport/test/xml_mini/libxml_engine_test.rb b/activesupport/test/xml_mini/libxml_engine_test.rb index ff4759b082..13f66cdfe7 100644 --- a/activesupport/test/xml_mini/libxml_engine_test.rb +++ b/activesupport/test/xml_mini/libxml_engine_test.rb @@ -18,12 +18,12 @@ else private - def adapter_name - "LibXML" - end + def engine + "LibXML" + end - def expansion_attack_error - LibXML::XML::Error - end + def expansion_attack_error + LibXML::XML::Error + end end end diff --git a/activesupport/test/xml_mini/libxmlsax_engine_test.rb b/activesupport/test/xml_mini/libxmlsax_engine_test.rb index 73eff9ac44..eb128d533d 100644 --- a/activesupport/test/xml_mini/libxmlsax_engine_test.rb +++ b/activesupport/test/xml_mini/libxmlsax_engine_test.rb @@ -13,12 +13,12 @@ else private - def adapter_name - "LibXMLSAX" - end + def engine + "LibXMLSAX" + end - def expansion_attack_error - LibXML::XML::Error - end + def expansion_attack_error + LibXML::XML::Error + end end end diff --git a/activesupport/test/xml_mini/nokogiri_engine_test.rb b/activesupport/test/xml_mini/nokogiri_engine_test.rb index b0c4985a50..3f976a3b57 100644 --- a/activesupport/test/xml_mini/nokogiri_engine_test.rb +++ b/activesupport/test/xml_mini/nokogiri_engine_test.rb @@ -13,12 +13,12 @@ else private - def expansion_attack_error - Nokogiri::XML::SyntaxError - end + def engine + "Nokogiri" + end - def adapter_name - "Nokogiri" - end + def expansion_attack_error + Nokogiri::XML::SyntaxError + end end end diff --git a/activesupport/test/xml_mini/nokogirisax_engine_test.rb b/activesupport/test/xml_mini/nokogirisax_engine_test.rb index 5be339b826..823c6243bb 100644 --- a/activesupport/test/xml_mini/nokogirisax_engine_test.rb +++ b/activesupport/test/xml_mini/nokogirisax_engine_test.rb @@ -13,12 +13,12 @@ else private - def adapter_name - "NokogiriSAX" - end + def engine + "NokogiriSAX" + end - def expansion_attack_error - RuntimeError - end + def expansion_attack_error + RuntimeError + end end end diff --git a/activesupport/test/xml_mini/rexml_engine_test.rb b/activesupport/test/xml_mini/rexml_engine_test.rb index 0f4e690746..4222436ae8 100644 --- a/activesupport/test/xml_mini/rexml_engine_test.rb +++ b/activesupport/test/xml_mini/rexml_engine_test.rb @@ -20,11 +20,11 @@ class REXMLEngineTest < ActiveSupport::TestCase private - def adapter_name - "REXML" - end + def engine + "REXML" + end - def expansion_attack_error - RuntimeError - end + def expansion_attack_error + RuntimeError + end end -- cgit v1.2.3 From 0ab54f4a49b388f0b75862b881fdef2545b380cf Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sun, 20 Nov 2016 10:09:20 -0500 Subject: Create XMLMiniEngineTest; base class for all tests --- activesupport/test/xml_mini/common.rb | 235 ------------------- activesupport/test/xml_mini/jdom_engine_test.rb | 11 +- activesupport/test/xml_mini/libxml_engine_test.rb | 15 +- .../test/xml_mini/libxmlsax_engine_test.rb | 15 +- .../test/xml_mini/nokogiri_engine_test.rb | 15 +- .../test/xml_mini/nokogirisax_engine_test.rb | 15 +- activesupport/test/xml_mini/rexml_engine_test.rb | 8 +- .../test/xml_mini/xml_mini_engine_test.rb | 250 +++++++++++++++++++++ 8 files changed, 267 insertions(+), 297 deletions(-) delete mode 100644 activesupport/test/xml_mini/common.rb create mode 100644 activesupport/test/xml_mini/xml_mini_engine_test.rb diff --git a/activesupport/test/xml_mini/common.rb b/activesupport/test/xml_mini/common.rb deleted file mode 100644 index c97bbf7e35..0000000000 --- a/activesupport/test/xml_mini/common.rb +++ /dev/null @@ -1,235 +0,0 @@ -module CommonXMLMiniAdapterTest - def setup - @default_backend = ActiveSupport::XmlMini.backend - ActiveSupport::XmlMini.backend = engine - super - end - - def teardown - ActiveSupport::XmlMini.backend = @default_backend - super - end - - def test_file_from_xml - hash = Hash.from_xml(<<-eoxml) - - - - - eoxml - assert hash.key?("blog") - assert hash["blog"].key?("logo") - - file = hash["blog"]["logo"] - assert_equal "logo.png", file.original_filename - assert_equal "image/png", file.content_type - end - - def test_exception_thrown_on_expansion_attack - assert_raise expansion_attack_error do - Hash.from_xml(<<-eoxml) - - - - - - - - - ]> - - &a; - - eoxml - end - end - - def test_setting_backend - assert_engine_class ActiveSupport::XmlMini.backend - end - - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end - - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end - - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end - - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end - - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text - assert_equal_rexml(<<-eoxml) - - - hello everyone - - - eoxml - end - - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good - - hello everyone - - morning - - eoxml - end - - def test_parse_from_io - skip_unless_extended_engine - - assert_equal_rexml(StringIO.new(<<-eoxml)) - - good - - hello everyone - - morning - - eoxml - end - - def test_children_with_simple_cdata - skip_unless_extended_engine - - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_multiple_cdata - skip_unless_extended_engine - - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end - - def test_children_with_text_and_cdata - skip_unless_extended_engine - - assert_equal_rexml(<<-eoxml) - - - hello - morning - - - eoxml - end - - def test_children_with_blank_text - skip_unless_extended_engine - - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - def test_children_with_blank_text_and_attribute - skip_unless_extended_engine - - assert_equal_rexml(<<-eoxml) - - - - eoxml - end - - private - def engine - raise NotImplementedError - end - - def assert_engine_class(actual) - assert_equal ActiveSupport.const_get("XmlMini_#{engine}"), actual - end - - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end - - def expansion_attack_error - raise NotImplementedError - end - - def extended_engine? - true - end - - def skip_unless_extended_engine - skip "#{engine} is not an extended engine" unless extended_engine? - end -end diff --git a/activesupport/test/xml_mini/jdom_engine_test.rb b/activesupport/test/xml_mini/jdom_engine_test.rb index 3034bc7148..a29949ab87 100644 --- a/activesupport/test/xml_mini/jdom_engine_test.rb +++ b/activesupport/test/xml_mini/jdom_engine_test.rb @@ -1,14 +1,9 @@ -if RUBY_PLATFORM.include?("java") - require "abstract_unit" - require "active_support/xml_mini" - require "active_support/core_ext/hash/conversions" - require_relative "./common" +require_relative "xml_mini_engine_test" - class JDOMEngineTest < ActiveSupport::TestCase +XMLMiniEngineTest.run_with_platform("java") do + class JDOMEngineTest < XMLMiniEngineTest FILES_DIR = File.dirname(__FILE__) + "/../fixtures/xml" - include CommonXMLMiniAdapterTest - def test_not_allowed_to_expand_entities_to_files attack_xml = <<-EOT + + + + eoxml + assert hash.key?("blog") + assert hash["blog"].key?("logo") + + file = hash["blog"]["logo"] + assert_equal "logo.png", file.original_filename + assert_equal "image/png", file.content_type + end + + def test_exception_thrown_on_expansion_attack + assert_raise expansion_attack_error do + Hash.from_xml(<<-eoxml) + + + + + + + + + ]> + + &a; + + eoxml + end + end + + def test_setting_backend + assert_engine_class ActiveSupport::XmlMini.backend + end + + def test_blank_returns_empty_hash + assert_equal({}, ActiveSupport::XmlMini.parse(nil)) + assert_equal({}, ActiveSupport::XmlMini.parse("")) + end + + def test_array_type_makes_an_array + assert_equal_rexml(<<-eoxml) + + + a post + another post + + + eoxml + end + + def test_one_node_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_one_node_with_attributes_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_products_node_with_book_node_as_hash + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + def test_products_node_with_two_book_nodes_as_hash + assert_equal_rexml(<<-eoxml) + + + + + eoxml + end + + def test_single_node_with_content_as_hash + assert_equal_rexml(<<-eoxml) + + hello world + + eoxml + end + + def test_children_with_children + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text + assert_equal_rexml(<<-eoxml) + + + hello everyone + + + eoxml + end + + def test_children_with_non_adjacent_text + assert_equal_rexml(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + end + + def test_parse_from_io + skip_unless_extended_engine + + assert_equal_rexml(StringIO.new(<<-eoxml)) + + good + + hello everyone + + morning + + eoxml + end + + def test_children_with_simple_cdata + skip_unless_extended_engine + + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_multiple_cdata + skip_unless_extended_engine + + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text_and_cdata + skip_unless_extended_engine + + assert_equal_rexml(<<-eoxml) + + + hello + morning + + + eoxml + end + + def test_children_with_blank_text + skip_unless_extended_engine + + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + def test_children_with_blank_text_and_attribute + skip_unless_extended_engine + + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + private + def engine + raise NotImplementedError + end + + def assert_engine_class(actual) + assert_equal ActiveSupport.const_get("XmlMini_#{engine}"), actual + end + + def assert_equal_rexml(xml) + parsed_xml = ActiveSupport::XmlMini.parse(xml) + xml.rewind if xml.respond_to?(:rewind) + hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } + assert_equal(hash, parsed_xml) + end + + def expansion_attack_error + raise NotImplementedError + end + + def extended_engine? + true + end + + def skip_unless_extended_engine + skip "#{engine} is not an extended engine" unless extended_engine? + end +end -- cgit v1.2.3 From 8e3901b5e6b4900cb4dd4cc1154180f42b51db2b Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sun, 20 Nov 2016 10:14:56 -0500 Subject: Move tests to a module, include in subclasses, style linting This fixes an error where the test runner would try and run XMLMiniEngineTest like a normal test class, except it's abstract. Now, to circumvent this, we don't include any of the actual tests in XMLMiniEngineTest; they are wrapped in a module that is included in subclass when they inherit from XMLMiniEngineTest. Pretty neat, huh? --- .../test/xml_mini/xml_mini_engine_test.rb | 383 +++++++++++---------- 1 file changed, 195 insertions(+), 188 deletions(-) diff --git a/activesupport/test/xml_mini/xml_mini_engine_test.rb b/activesupport/test/xml_mini/xml_mini_engine_test.rb index a7d4fe20ad..5be9084c9d 100644 --- a/activesupport/test/xml_mini/xml_mini_engine_test.rb +++ b/activesupport/test/xml_mini/xml_mini_engine_test.rb @@ -11,7 +11,12 @@ class XMLMiniEngineTest < ActiveSupport::TestCase end def self.run_with_platform(platform_name) - yielf if RUBY_PLATFORM.include?(platform_name) + yield if RUBY_PLATFORM.include?(platform_name) + end + + def self.inherited(base) + base.include EngineTests + super end def setup @@ -25,226 +30,228 @@ class XMLMiniEngineTest < ActiveSupport::TestCase super end - def test_file_from_xml - hash = Hash.from_xml(<<-eoxml) - - - - - eoxml - assert hash.key?("blog") - assert hash["blog"].key?("logo") - - file = hash["blog"]["logo"] - assert_equal "logo.png", file.original_filename - assert_equal "image/png", file.content_type - end - - def test_exception_thrown_on_expansion_attack - assert_raise expansion_attack_error do - Hash.from_xml(<<-eoxml) - - - - - - - - - ]> - - &a; - - eoxml + module EngineTests + def test_file_from_xml + hash = Hash.from_xml(<<-eoxml) + + + + + eoxml + assert hash.key?("blog") + assert hash["blog"].key?("logo") + + file = hash["blog"]["logo"] + assert_equal "logo.png", file.original_filename + assert_equal "image/png", file.content_type end - end - - def test_setting_backend - assert_engine_class ActiveSupport::XmlMini.backend - end - def test_blank_returns_empty_hash - assert_equal({}, ActiveSupport::XmlMini.parse(nil)) - assert_equal({}, ActiveSupport::XmlMini.parse("")) - end - - def test_array_type_makes_an_array - assert_equal_rexml(<<-eoxml) - - - a post - another post - - - eoxml - end - - def test_one_node_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end + def test_exception_thrown_on_expansion_attack + assert_raise expansion_attack_error do + Hash.from_xml(<<-eoxml) + + + + + + + + + ]> + + &a; + + eoxml + end + end - def test_one_node_with_attributes_document_as_hash - assert_equal_rexml(<<-eoxml) - - eoxml - end + def test_setting_backend + assert_engine_class ActiveSupport::XmlMini.backend + end - def test_products_node_with_book_node_as_hash - assert_equal_rexml(<<-eoxml) - - - - eoxml - end + def test_blank_returns_empty_hash + assert_equal({}, ActiveSupport::XmlMini.parse(nil)) + assert_equal({}, ActiveSupport::XmlMini.parse("")) + end - def test_products_node_with_two_book_nodes_as_hash - assert_equal_rexml(<<-eoxml) - - - - - eoxml - end + def test_array_type_makes_an_array + assert_equal_rexml(<<-eoxml) + + + a post + another post + + + eoxml + end - def test_single_node_with_content_as_hash - assert_equal_rexml(<<-eoxml) - - hello world - - eoxml - end + def test_one_node_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end - def test_children_with_children - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end + def test_one_node_with_attributes_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end - def test_children_with_text - assert_equal_rexml(<<-eoxml) - + def test_products_node_with_book_node_as_hash + assert_equal_rexml(<<-eoxml) - hello everyone + - - eoxml - end + eoxml + end - def test_children_with_non_adjacent_text - assert_equal_rexml(<<-eoxml) - - good + def test_products_node_with_two_book_nodes_as_hash + assert_equal_rexml(<<-eoxml) - hello everyone + + - morning - - eoxml - end - - def test_parse_from_io - skip_unless_extended_engine + eoxml + end - assert_equal_rexml(StringIO.new(<<-eoxml)) - - good + def test_single_node_with_content_as_hash + assert_equal_rexml(<<-eoxml) - hello everyone + hello world - morning - - eoxml - end - - def test_children_with_simple_cdata - skip_unless_extended_engine + eoxml + end - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end + def test_children_with_children + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end - def test_children_with_multiple_cdata - skip_unless_extended_engine + def test_children_with_text + assert_equal_rexml(<<-eoxml) + + + hello everyone + + + eoxml + end - assert_equal_rexml(<<-eoxml) - - - - - - eoxml - end + def test_children_with_non_adjacent_text + assert_equal_rexml(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + end - def test_children_with_text_and_cdata - skip_unless_extended_engine + def test_parse_from_io + skip_unless_extended_engine - assert_equal_rexml(<<-eoxml) - - - hello + assert_equal_rexml(StringIO.new(<<-eoxml)) + + good + + hello everyone + morning - - - eoxml - end - - def test_children_with_blank_text - skip_unless_extended_engine + + eoxml + end - assert_equal_rexml(<<-eoxml) - - - - eoxml - end + def test_children_with_simple_cdata + skip_unless_extended_engine - def test_children_with_blank_text_and_attribute - skip_unless_extended_engine + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end - assert_equal_rexml(<<-eoxml) - - - - eoxml - end + def test_children_with_multiple_cdata + skip_unless_extended_engine - private - def engine - raise NotImplementedError + assert_equal_rexml(<<-eoxml) + + + + + + eoxml end - def assert_engine_class(actual) - assert_equal ActiveSupport.const_get("XmlMini_#{engine}"), actual + def test_children_with_text_and_cdata + skip_unless_extended_engine + + assert_equal_rexml(<<-eoxml) + + + hello + morning + + + eoxml end - def assert_equal_rexml(xml) - parsed_xml = ActiveSupport::XmlMini.parse(xml) - xml.rewind if xml.respond_to?(:rewind) - hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } - assert_equal(hash, parsed_xml) - end + def test_children_with_blank_text + skip_unless_extended_engine - def expansion_attack_error - raise NotImplementedError + assert_equal_rexml(<<-eoxml) + + + + eoxml end - def extended_engine? - true - end + def test_children_with_blank_text_and_attribute + skip_unless_extended_engine - def skip_unless_extended_engine - skip "#{engine} is not an extended engine" unless extended_engine? + assert_equal_rexml(<<-eoxml) + + + + eoxml end + + private + def engine + raise NotImplementedError + end + + def assert_engine_class(actual) + assert_equal ActiveSupport.const_get("XmlMini_#{engine}"), actual + end + + def assert_equal_rexml(xml) + parsed_xml = ActiveSupport::XmlMini.parse(xml) + xml.rewind if xml.respond_to?(:rewind) + hash = ActiveSupport::XmlMini.with_backend("REXML") { ActiveSupport::XmlMini.parse(xml) } + assert_equal(hash, parsed_xml) + end + + def expansion_attack_error + raise NotImplementedError + end + + def extended_engine? + true + end + + def skip_unless_extended_engine + skip "#{engine} is not an extended engine" unless extended_engine? + end + end end -- cgit v1.2.3