[Bps-public-commit] r14234 - in Data-Plist: . lib/Data/Plist

kyoki at bestpractical.com kyoki at bestpractical.com
Thu Jul 17 16:13:06 EDT 2008


Author: kyoki
Date: Thu Jul 17 16:12:52 2008
New Revision: 14234

Modified:
   Data-Plist/   (props changed)
   Data-Plist/lib/Data/Plist/BinaryWriter.pm
   Data-Plist/lib/Data/Plist/Writer.pm
   Data-Plist/lib/Data/Plist/XMLWriter.pm
   Data-Plist/t/binary-write.t

Log:
 r29737 at nyx:  kyoki | 2008-07-17 16:12:51 -0400
 more tests. added option of having a preserialized input


Modified: Data-Plist/lib/Data/Plist/BinaryWriter.pm
==============================================================================
--- Data-Plist/lib/Data/Plist/BinaryWriter.pm	(original)
+++ Data-Plist/lib/Data/Plist/BinaryWriter.pm	Thu Jul 17 16:12:52 2008
@@ -13,7 +13,7 @@
     $self = $self->new() unless ref $self;
 
     my ( $fh, $object ) = @_;
-    $object = $self->serialize($object);
+    $object = $self->serialize($object) if ($self->{serialize});
     binmode $fh;
     $self->{fh}    = $fh;
     $self->{index} = [];
@@ -33,7 +33,7 @@
         print $fh ( pack $self->pack_in($offset_size), $_ );
     }
     print $fh ( pack "x6CC", ( $offset_size + 1 ), $self->{refsize} );
-    print $fh ( pack "x4N", $self->{size} );
+    print $fh ( pack "x4N", scalar keys %{$self->{objcache}} );
     print $fh ( pack "x4N", $top_index );
     print $fh ( pack "x4N", $table_offset );
     close $fh;
@@ -151,10 +151,10 @@
     return ( @{ $self->{index} } - 1 );
 }
 
-sub write_uid {
+sub write_UID {
     my $self = shift;
     my ($id) = @_;
-    return $self->write_int( $id, "8" );
+    return $self->write_integer( $id, "8" );
 }
 
 sub write_real {
@@ -201,6 +201,7 @@
 }
 
 sub count {
+    # this might be slightly over, since it doesn't take into account duplicates
     my $self       = shift;
     my ($arrayref) = @_;
     my $type       = $arrayref->[0];
@@ -259,11 +260,11 @@
 
 sub pack_in {
     my $self = shift;
-    my ($bytes) = @_;
-    if ( $bytes == 3 ) {
-        die "Cannot encode 3 byte integers";
+    my ($power ) = @_;
+    if ( $power == 4 ) {
+        die "Cannot encode 2**4 byte integers";
     }
-    my $fmt = [ "C", "n", "N" ]->[$bytes];
+    my $fmt = [ "C", "n", "N", "N" ]->[$power];
     return $fmt;
 }
 

Modified: Data-Plist/lib/Data/Plist/Writer.pm
==============================================================================
--- Data-Plist/lib/Data/Plist/Writer.pm	(original)
+++ Data-Plist/lib/Data/Plist/Writer.pm	Thu Jul 17 16:12:52 2008
@@ -6,7 +6,8 @@
 
 sub new {
     my $class = shift;
-    return bless {} => $class;
+    my %args = (serialize => 1, @_);
+    return bless \%args => $class;
 }
 
 sub write {

Modified: Data-Plist/lib/Data/Plist/XMLWriter.pm
==============================================================================
--- Data-Plist/lib/Data/Plist/XMLWriter.pm	(original)
+++ Data-Plist/lib/Data/Plist/XMLWriter.pm	Thu Jul 17 16:12:52 2008
@@ -21,7 +21,8 @@
         "http://www.apple.com/DTDs/PropertyList-1.0.dtd"
     );
     $self->{x}->startTag( plist => version => "1.0" );
-    $self->xml_write( $self->serialize($object) );
+    $object = $self->serialize($object) if ($self->{serialize});
+    $self->xml_write( $object );
     $self->{x}->endTag("plist");
     $self->{x}->end();
 

Modified: Data-Plist/t/binary-write.t
==============================================================================
--- Data-Plist/t/binary-write.t	(original)
+++ Data-Plist/t/binary-write.t	Thu Jul 17 16:12:52 2008
@@ -1,4 +1,4 @@
-use Test::More no_plan => 1;
+use Test::More tests => 160;
 
 use strict;
 use warnings;
@@ -11,12 +11,6 @@
 my $in;
 my $out;
 
-# Create the object
-my $write = Data::Plist::BinaryWriter->new;
-my $read  = Data::Plist::BinaryReader->new;
-ok( $write, "Created a binary writer" );
-isa_ok( $write, "Data::Plist::BinaryWriter" );
-
 # Empty dict
 round_trip( {}, 42 );
 
@@ -63,7 +57,20 @@
 # Caching
 round_trip({'kitteh' => 'Angleton', 'Laundry' => 'Angleton'}, 73);
 
+# UIDs
+preserialized_trip( [  UID => 1 ], 43 );
+
+# Miscs
+preserialized_trip([ false => 0 ], 42);
+preserialized_trip([ true => 1 ], 42);
+preserialized_trip([ fill => 15 ], 44);
+preserialized_trip([ null => 0 ], 42);
+
 sub round_trip {
+    my $write = Data::Plist::BinaryWriter->new;
+    my $read  = Data::Plist::BinaryReader->new;
+    ok( $write, "Created a binary writer" );
+    isa_ok( $write, "Data::Plist::BinaryWriter" );
     my ( $input, $expected_size ) = @_;
     $out = $write->write($input);
     ok( $out, "Created data structure" );
@@ -71,8 +78,24 @@
     is( length($out), $expected_size,
         "Bplist is " . $expected_size . " bytes long." );
     $in = eval { $read->open_string($out) };
-    is_deeply( $@, '' );
     ok( $in, "Read back bplist" );
     isa_ok( $in, "Data::Plist" );
     is_deeply( $in->data, $input, "Read back " . $input );
 }
+
+sub preserialized_trip {
+    my $write = Data::Plist::BinaryWriter->new(serialize => 0);
+    my $read  = Data::Plist::BinaryReader->new;
+    ok( $write, "Created a binary writer" );
+    isa_ok( $write, "Data::Plist::BinaryWriter" );
+    my ( $input, $expected_size ) = @_;
+    $out = $write->write($input);
+    ok( $out, "Created data structure" );
+    like( $out, qr/^bplist00/, "Bplist begins with correct header" );
+    is( length($out), $expected_size,
+        "Bplist is " . $expected_size . " bytes long." );
+    $in = eval { $read->open_string($out) };
+    ok( $in, "Read back bplist" );
+    isa_ok( $in, "Data::Plist" );
+    is_deeply( $in->raw_data, $input, "Read back " . $input );
+}



More information about the Bps-public-commit mailing list