提交 8e3901b5 编写于 作者: J Jon Moss

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?
上级 0ab54f4a
...@@ -11,7 +11,12 @@ def self.run_with_gem(gem_name) ...@@ -11,7 +11,12 @@ def self.run_with_gem(gem_name)
end end
def self.run_with_platform(platform_name) 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 end
def setup def setup
...@@ -25,226 +30,228 @@ def teardown ...@@ -25,226 +30,228 @@ def teardown
super super
end end
def test_file_from_xml module EngineTests
hash = Hash.from_xml(<<-eoxml) def test_file_from_xml
<blog> hash = Hash.from_xml(<<-eoxml)
<logo type="file" name="logo.png" content_type="image/png"> <blog>
</logo> <logo type="file" name="logo.png" content_type="image/png">
</blog> </logo>
eoxml </blog>
assert hash.key?("blog") eoxml
assert hash["blog"].key?("logo") assert hash.key?("blog")
assert hash["blog"].key?("logo")
file = hash["blog"]["logo"]
assert_equal "logo.png", file.original_filename file = hash["blog"]["logo"]
assert_equal "image/png", file.content_type assert_equal "logo.png", file.original_filename
end assert_equal "image/png", file.content_type
def test_exception_thrown_on_expansion_attack
assert_raise expansion_attack_error do
Hash.from_xml(<<-eoxml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE member [
<!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
<!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
<!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
<!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
<!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
<!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
<!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
]>
<member>
&a;
</member>
eoxml
end end
end
def test_setting_backend
assert_engine_class ActiveSupport::XmlMini.backend
end
def test_blank_returns_empty_hash def test_exception_thrown_on_expansion_attack
assert_equal({}, ActiveSupport::XmlMini.parse(nil)) assert_raise expansion_attack_error do
assert_equal({}, ActiveSupport::XmlMini.parse("")) Hash.from_xml(<<-eoxml)
end <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE member [
def test_array_type_makes_an_array <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
assert_equal_rexml(<<-eoxml) <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
<blog> <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
<posts type="array"> <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
<post>a post</post> <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
<post>another post</post> <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
</posts> <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
</blog> ]>
eoxml <member>
end &a;
</member>
def test_one_node_document_as_hash eoxml
assert_equal_rexml(<<-eoxml) end
<products/> end
eoxml
end
def test_one_node_with_attributes_document_as_hash def test_setting_backend
assert_equal_rexml(<<-eoxml) assert_engine_class ActiveSupport::XmlMini.backend
<products foo="bar"/> end
eoxml
end
def test_products_node_with_book_node_as_hash def test_blank_returns_empty_hash
assert_equal_rexml(<<-eoxml) assert_equal({}, ActiveSupport::XmlMini.parse(nil))
<products> assert_equal({}, ActiveSupport::XmlMini.parse(""))
<book name="awesome" id="12345" /> end
</products>
eoxml
end
def test_products_node_with_two_book_nodes_as_hash def test_array_type_makes_an_array
assert_equal_rexml(<<-eoxml) assert_equal_rexml(<<-eoxml)
<products> <blog>
<book name="awesome" id="12345" /> <posts type="array">
<book name="america" id="67890" /> <post>a post</post>
</products> <post>another post</post>
eoxml </posts>
end </blog>
eoxml
end
def test_single_node_with_content_as_hash def test_one_node_document_as_hash
assert_equal_rexml(<<-eoxml) assert_equal_rexml(<<-eoxml)
<products> <products/>
hello world eoxml
</products> end
eoxml
end
def test_children_with_children def test_one_node_with_attributes_document_as_hash
assert_equal_rexml(<<-eoxml) assert_equal_rexml(<<-eoxml)
<root> <products foo="bar"/>
<products> eoxml
<book name="america" id="67890" /> end
</products>
</root>
eoxml
end
def test_children_with_text def test_products_node_with_book_node_as_hash
assert_equal_rexml(<<-eoxml) assert_equal_rexml(<<-eoxml)
<root>
<products> <products>
hello everyone <book name="awesome" id="12345" />
</products> </products>
</root> eoxml
eoxml end
end
def test_children_with_non_adjacent_text def test_products_node_with_two_book_nodes_as_hash
assert_equal_rexml(<<-eoxml) assert_equal_rexml(<<-eoxml)
<root>
good
<products> <products>
hello everyone <book name="awesome" id="12345" />
<book name="america" id="67890" />
</products> </products>
morning eoxml
</root> end
eoxml
end
def test_parse_from_io
skip_unless_extended_engine
assert_equal_rexml(StringIO.new(<<-eoxml)) def test_single_node_with_content_as_hash
<root> assert_equal_rexml(<<-eoxml)
good
<products> <products>
hello everyone hello world
</products> </products>
morning eoxml
</root> end
eoxml
end
def test_children_with_simple_cdata
skip_unless_extended_engine
assert_equal_rexml(<<-eoxml) def test_children_with_children
<root> assert_equal_rexml(<<-eoxml)
<products> <root>
<![CDATA[cdatablock]]> <products>
</products> <book name="america" id="67890" />
</root> </products>
eoxml </root>
end eoxml
end
def test_children_with_multiple_cdata def test_children_with_text
skip_unless_extended_engine assert_equal_rexml(<<-eoxml)
<root>
<products>
hello everyone
</products>
</root>
eoxml
end
assert_equal_rexml(<<-eoxml) def test_children_with_non_adjacent_text
<root> assert_equal_rexml(<<-eoxml)
<products> <root>
<![CDATA[cdatablock1]]><![CDATA[cdatablock2]]> good
</products> <products>
</root> hello everyone
eoxml </products>
end morning
</root>
eoxml
end
def test_children_with_text_and_cdata def test_parse_from_io
skip_unless_extended_engine skip_unless_extended_engine
assert_equal_rexml(<<-eoxml) assert_equal_rexml(StringIO.new(<<-eoxml))
<root> <root>
<products> good
hello <![CDATA[cdatablock]]> <products>
hello everyone
</products>
morning morning
</products> </root>
</root> eoxml
eoxml end
end
def test_children_with_blank_text
skip_unless_extended_engine
assert_equal_rexml(<<-eoxml) def test_children_with_simple_cdata
<root> skip_unless_extended_engine
<products> </products>
</root>
eoxml
end
def test_children_with_blank_text_and_attribute assert_equal_rexml(<<-eoxml)
skip_unless_extended_engine <root>
<products>
<![CDATA[cdatablock]]>
</products>
</root>
eoxml
end
assert_equal_rexml(<<-eoxml) def test_children_with_multiple_cdata
<root> skip_unless_extended_engine
<products type="file"> </products>
</root>
eoxml
end
private assert_equal_rexml(<<-eoxml)
def engine <root>
raise NotImplementedError <products>
<![CDATA[cdatablock1]]><![CDATA[cdatablock2]]>
</products>
</root>
eoxml
end end
def assert_engine_class(actual) def test_children_with_text_and_cdata
assert_equal ActiveSupport.const_get("XmlMini_#{engine}"), actual skip_unless_extended_engine
assert_equal_rexml(<<-eoxml)
<root>
<products>
hello <![CDATA[cdatablock]]>
morning
</products>
</root>
eoxml
end end
def assert_equal_rexml(xml) def test_children_with_blank_text
parsed_xml = ActiveSupport::XmlMini.parse(xml) skip_unless_extended_engine
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 assert_equal_rexml(<<-eoxml)
raise NotImplementedError <root>
<products> </products>
</root>
eoxml
end end
def extended_engine? def test_children_with_blank_text_and_attribute
true skip_unless_extended_engine
end
def skip_unless_extended_engine assert_equal_rexml(<<-eoxml)
skip "#{engine} is not an extended engine" unless extended_engine? <root>
<products type="file"> </products>
</root>
eoxml
end 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 end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册