Permalink
Browse files

Refactored generator. Removed a couple of 'puts' from the tests.

  • Loading branch information...
1 parent 268dd30 commit aafcbae6c0a7b10a72c7daad3626f6e94669a4d1 @caseyhoward committed Dec 13, 2011
Showing with 21 additions and 16 deletions.
  1. +15 −9 lib/nokogiri-plist/generator.rb
  2. +6 −7 test/nokogiri-plist/test_generator.rb
@@ -53,21 +53,27 @@ def to_plist_xml(options = {}, xml = nil)
if xml
NokogiriPList::Generator.to_xml(self, xml)
else
- builder = if options.delete(:fragment)
- document_fragment = Nokogiri::XML::DocumentFragment.parse("")
- Nokogiri::XML::Builder.with(document_fragment) do |xml|
+ is_fragment = options.delete(:fragment)
+ document_class = is_fragment ? Nokogiri::XML::DocumentFragment : Nokogiri::XML::Document
+ document = document_class.parse("")
+ Nokogiri::XML::Builder.with(document) do |xml|
+ if is_fragment
NokogiriPList::Generator.to_xml(self, xml)
+ else
+ add_doctype_to_document(xml.doc)
+ xml.plist(:version => "1.0") do |xml|
+ NokogiriPList::Generator.to_xml(self, xml)
+ end
end
- document_fragment.to_xml(options)
- else
- builder = Nokogiri::XML::Builder.new do |xml|
- NokogiriPList::Generator.to_xml(self, xml)
- end
- builder.to_xml(options)
end
+ document.to_xml(options)
end
end
+ def add_doctype_to_document(document)
+ document.create_internal_subset("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd")
+ end
+
end
end
@@ -58,7 +58,6 @@ class NokogiriPList::GeneratorTest < Test::Unit::TestCase
context "String#to_plist_xml" do
should "output string plists correctly" do
- puts "a&b".to_plist_xml
expected_xml = <<-XML
<?xml version="1.0"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -70,7 +69,6 @@ class NokogiriPList::GeneratorTest < Test::Unit::TestCase
end
should "output plist fragments correctly" do
- puts "a&b".to_plist_xml
expected_xml = "<string>a&amp;b</string>"
assert_equal expected_xml, "a&b".to_plist_xml(:fragment => true)
end
@@ -183,11 +181,12 @@ class NokogiriPList::GeneratorTest < Test::Unit::TestCase
should "build the document with Nokogiri and pass the options" do
options = {}
- builder = mock()
- xml = "<?xml version=\"1.0\"?>\n<test/>"
- builder.expects(:to_xml).with(options).returns(xml)
- Nokogiri::XML::Builder.expects(:new).returns(builder)
- assert_equal "<?xml version=\"1.0\"?>\n<test/>", [1, 2, 3].to_plist_xml(options)
+ xml = mock()
+ document = mock()
+ document.expects(:to_xml).with(options).returns(xml)
+ Nokogiri::XML::Document.expects(:parse).with("").returns(document)
+ Nokogiri::XML::Builder.expects(:with).with(document)
+ assert_equal xml, [1, 2, 3].to_plist_xml(options)
end
end

0 comments on commit aafcbae

Please sign in to comment.