summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Bleything <ben@bleything.net>2006-09-11 05:24:16 +0000
committerBen Bleything <ben@bleything.net>2006-09-11 05:24:16 +0000
commitbe823e51031ab5a73ea3c96e1013f20894596713 (patch)
tree97cf51eb853194ac5534b00b3a372fb18ffd2975
parenteb1f1d8e1c9755cd146194153ba44e53423698f1 (diff)
downloadplist-be823e51031ab5a73ea3c96e1013f20894596713.tar.gz
source:branches/generator-injection-removal/test/test_generator_data.rb
* renamed to test_data_elements.rb source:branches/generator-injection-removal/test/assets/test_data_elements.plist * example plist for data element tests source:branches/generator-injection-removal/test/test_data_elements.rb * Oops. Marshable -> Marshalable * Remove the class variables for expected values in favor of combining a couple of tests * set up a class variable for the results of parsing our test data * test generation and parsing in the same test source:branches/generator-injection-removal/lib/plist/parser.rb * Unroll the StringScanner instantiation so we can gsub the slurped string first * Strip comments out of the slurped xml string (with a really naive regex) * Attempt to Marshal.load <data> elements. If it fails, return a StringIO object with the contents of the element.
-rw-r--r--lib/plist/parser.rb41
-rw-r--r--test/assets/test_data_elements.plist24
-rw-r--r--test/test_data_elements.rb86
-rw-r--r--test/test_generator_data.rb72
4 files changed, 133 insertions, 90 deletions
diff --git a/lib/plist/parser.rb b/lib/plist/parser.rb
index c3634aa..584c395 100644
--- a/lib/plist/parser.rb
+++ b/lib/plist/parser.rb
@@ -75,11 +75,19 @@ module Plist
end_tag = /<\/(#{plist_tags})[^>]*>/i
require 'strscan'
- @scanner = StringScanner.new( if (File.exists? @filename_or_xml)
- File.open(@filename_or_xml, "r") {|f| f.read}
- else
- @filename_or_xml
- end )
+
+ contents = (
+ if (File.exists? @filename_or_xml)
+ File.open(@filename_or_xml) {|f| f.read}
+ else
+ @filename_or_xml
+ end
+ )
+
+ # FIXME - temporarily strip out comments until I (ben) can figure out how to make the scanner ignore them
+ contents.gsub!(/<!--.*?-->/, '')
+
+ @scanner = StringScanner.new( contents )
until @scanner.eos?
if @scanner.scan(XMLDECL_PATTERN)
elsif @scanner.scan(DOCTYPE_PATTERN)
@@ -201,19 +209,16 @@ module Plist
require 'base64'
class PData < PTag
def to_ruby
- # replacing Tempfile with StringIO
- # think it might be a bit nicer
- #require 'tempfile'
- #tf = Tempfile.new("plist.tmp")
- #tf.write Base64.decode64(text.gsub(/\s+/,''))
- #tf.close
- # is this a good idea?
- #tf.open
- #tf
- io = StringIO.new
- io.write Base64.decode64(text.gsub(/\s+/,''))
- io.rewind
- io
+ data = Base64.decode64(text.gsub(/\s+/, ''))
+
+ begin
+ return Marshal.load(data)
+ rescue Exception => e
+ io = StringIO.new
+ io.write data
+ io.rewind
+ return io
+ end
end
end
end
diff --git a/test/assets/test_data_elements.plist b/test/assets/test_data_elements.plist
new file mode 100644
index 0000000..fd548c0
--- /dev/null
+++ b/test/assets/test_data_elements.plist
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+ <dict>
+ <key>stringio</key>
+ <data>dGhpcyBpcyBhIHN0cmluZ2lvIG9iamVjdA==
+ </data>
+ <key>file</key>
+ <data>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAA==
+ </data>
+ <key>io</key>
+ <data>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ AAAAAAAAAAAAAA==
+ </data>
+ <key>marshal</key>
+ <!-- The <data> element below contains a Ruby object which has been serialized with Marshal.dump. -->
+ <data>BAhvOhZNYXJzaGFsYWJsZU9iamVjdAY6CUBmb28iHnRoaXMgb2JqZWN0IHdh
+ cyBtYXJzaGFsZWQ=
+ </data>
+ </dict>
+</plist> \ No newline at end of file
diff --git a/test/test_data_elements.rb b/test/test_data_elements.rb
new file mode 100644
index 0000000..ef8a4b0
--- /dev/null
+++ b/test/test_data_elements.rb
@@ -0,0 +1,86 @@
+##############################################################
+# Copyright 2006, Ben Bleything <ben@bleything.net> and #
+# Patrick May <patrick@hexane.org> #
+# #
+# Distributed under the MIT license. #
+##############################################################
+
+require 'test/unit'
+require 'plist'
+require 'stringio'
+
+class MarshalableObject
+ attr_accessor :foo
+
+ def initialize(str)
+ @foo = str
+ end
+end
+
+class TestDataElements < Test::Unit::TestCase
+ @@result = Plist::parse_xml('test/assets/test_data_elements.plist')
+
+ def test_marshal
+ expected = <<END
+<!-- The <data> element below contains a Ruby object which has been serialized with Marshal.dump. --><data>BAhvOhZNYXJzaGFsYWJsZU9iamVjdAY6CUBmb28iHnRoaXMgb2JqZWN0IHdh
+cyBtYXJzaGFsZWQ=
+</data>
+END
+
+ mo = MarshalableObject.new('this object was marshaled')
+
+ assert_equal expected.chomp, Plist::Emit.dump(mo, false)
+
+ assert_instance_of MarshalableObject, @@result['marshal']
+
+ assert_equal mo.foo, @@result['marshal'].foo
+ end
+
+ def test_generator_io_and_file
+ expected = <<END
+<data>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+AAAAAAAAAAAAAA==
+</data>
+END
+
+ expected.chomp!
+
+ fd = IO.sysopen('test/assets/example_data.bin')
+ io = IO.open(fd, 'r')
+
+ # File is a subclass of IO, so catching IO in the dispatcher should work for File as well...
+ f = File.open('test/assets/example_data.bin')
+
+ assert_equal expected, Plist::Emit.dump(io, false)
+ assert_equal expected, Plist::Emit.dump(f, false)
+
+ assert_instance_of StringIO, @@result['io']
+ assert_instance_of StringIO, @@result['file']
+
+ io.rewind
+ f.rewind
+
+ assert_equal io.read, @@result['io'].read
+ assert_equal f.read, @@result['file'].read
+
+ io.close
+ f.close
+ end
+
+ def test_generator_string_io
+ expected = <<END
+<data>dGhpcyBpcyBhIHN0cmluZ2lvIG9iamVjdA==
+</data>
+END
+
+ sio = StringIO.new('this is a stringio object')
+
+ assert_equal expected.chomp, Plist::Emit.dump(sio, false)
+
+ assert_instance_of StringIO, @@result['stringio']
+
+ sio.rewind
+ assert_equal sio.read, @@result['stringio'].read
+ end
+end \ No newline at end of file
diff --git a/test/test_generator_data.rb b/test/test_generator_data.rb
deleted file mode 100644
index 61526a4..0000000
--- a/test/test_generator_data.rb
+++ /dev/null
@@ -1,72 +0,0 @@
-##############################################################
-# Copyright 2006, Ben Bleything <ben@bleything.net> and #
-# Patrick May <patrick@hexane.org> #
-# #
-# Distributed under the MIT license. #
-##############################################################
-
-require 'test/unit'
-require 'plist'
-require 'stringio'
-
-class MarshableObject
- attr_accessor :foo
-
- def initialize(str)
- @foo = str
- end
-end
-
-class TestGeneratorData < Test::Unit::TestCase
- @@marshal_expected = <<END
-<!-- The <data> element below contains a Ruby object which has been serialized with Marshal.dump. --><data>BAhvOhRNYXJzaGFibGVPYmplY3QGOglAZm9vIh50aGlzIG9iamVjdCB3YXMg
-bWFyc2hhbGVk
-</data>
-END
-
- @@io_expected = <<END
-<data>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
-AAAAAAAAAAAAAA==
-</data>
-END
-
- @@sio_expected = <<END
-<data>dGhpcyBpcyBhIHN0cmluZ2lvIG9iamVjdA==
-</data>
-END
-
- # must chomp because heredocs won't let you not have a trailing newline.
- # this won't be a problem once nice indent code goes into this branch.
- [@@marshal_expected, @@io_expected, @@sio_expected].each {|a| a.chomp! }
-
- def test_marshaling_object
- mo = MarshableObject.new('this object was marshaled')
-
- assert_equal @@marshal_expected, Plist::Emit.dump(mo, false)
- end
-
- def test_io
- fd = IO.sysopen('test/assets/example_data.bin')
- io = IO.open(fd, 'r')
-
- assert_equal @@io_expected, Plist::Emit.dump(io, false)
-
- io.close
- end
-
- # File is a subclass of IO, so catching IO in the dispatcher should work for File as well...
- def test_file
- f = File.open('test/assets/example_data.bin')
-
- assert_equal @@io_expected, Plist::Emit.dump(f, false)
-
- f.close
- end
-
- def test_string_io
- sio = StringIO.new('this is a stringio object')
-
- assert_equal @@sio_expected, Plist::Emit.dump(sio, false)
- end
-end \ No newline at end of file