r1673@llin: dpavlin | 2007-11-28 00:46:28 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 27 Nov 2007 23:54:43 +0000 (23:54 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 27 Nov 2007 23:54:43 +0000 (23:54 +0000)
 added tests for WebPAC::Output base class and added
 single_values option

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1069 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Output.pm
lib/WebPAC/Output/Jifty.pm
t/5-output.t [new file with mode: 0755]

index e0d4812..e46cec5 100644 (file)
@@ -4,19 +4,14 @@ use warnings;
 use strict;
 
 use Carp qw/confess/;
+use Data::Dump qw/dump/;
 
 =head1 NAME
 
 WebPAC::Output - The great new WebPAC::Output!
 
-=head1 VERSION
-
-Version 0.01
-
 =cut
 
-our $VERSION = '0.01';
-
 =head1 SYNOPSIS
 
 Common routines for output formats
@@ -26,7 +21,8 @@ Common routines for output formats
 =head2 ds_to_hash
 
   my $hash = $self->ds_to_hash( $ds, 'display'
-       disable_key_mungle => 1,
+       disable_key_mungle => 0,
+       singe_values = 0,
   );
 
 =cut
@@ -48,11 +44,23 @@ sub ds_to_hash {
                        $name =~ s/\W+/_/g;
                }
 
-               # FIXME get rid of non hash values in data_structure for consistency?
-               next unless ref($ds->{$t}) eq 'HASH';
+               my $v = $ds->{$t} || die "bug";
 
-               if ( defined( $ds->{$t}->{$type} ) ) {
-                       $hash->{$name} = $ds->{$t}->{$type};
+               # FIXME get rid of non hash values in data_structure for consistency?
+               next unless ref($v) eq 'HASH';
+
+               if ( defined( $v->{$type} ) ) {
+                       if ( $opt->{single_values} && ref($v->{$type}) eq 'ARRAY' ) {
+                               $hash->{$name} = join(' ', map {
+                                       if(ref($_)) {
+                                               dump($_);
+                                       } else {
+                                               $_;
+                                       }
+                               } @{ $v->{$type} });
+                       } else {
+                               $hash->{$name} = $v->{$type};
+                       }
                }
        }
 
index 4a53eaf..1508d1d 100644 (file)
@@ -99,11 +99,7 @@ sub add {
 
        my $stat;
 
-       my $hash = $self->ds_to_hash( $ds, 'display' ) || next;
-
-       foreach my $f ( keys %$hash ) {
-               $hash->{$f} = join(' ', @{ $hash->{$f} }) if ref($hash->{$f}) eq 'ARRAY';
-       }
+       my $hash = $self->ds_to_hash( $ds, 'display', single_values => 1 ) || next;
 
        $log->debug("data: ", sub { dump( $hash ) });
 
diff --git a/t/5-output.t b/t/5-output.t
new file mode 100755 (executable)
index 0000000..7046c51
--- /dev/null
@@ -0,0 +1,68 @@
+#!/usr/bin/perl -w
+
+use strict;
+use blib;
+
+use Test::More tests => 8;
+
+BEGIN {
+use_ok( 'WebPAC::Test' );
+use_ok( 'WebPAC::Output' );
+}
+
+my $ds = {
+       '000' => { foo => [ 42 ], bar => 'bug!' },
+       '900' => { foo => [
+               { a => '900a1', b => '900b1' },
+               { a => '900a2', b => '900b2' },
+               { a => '900a3', b => '900b3' },
+       ] },
+       '902' => { foo => [
+               { a => '900a1', b => '900b1' },
+       ] },
+
+       'StRaNgE!Field' => { foo => 'value' },
+
+       'array' => { foo => [ 'a' .. 'c' ] },
+};
+
+my $hash;
+ok( $hash = WebPAC::Output->ds_to_hash( $ds, 'foo' ), 'ds_to_hash' );
+diag dump($hash) if $debug;
+is_deeply( $hash, {
+   "000" => [42],
+   900   => [
+              { a => "900a1", b => "900b1" },
+              { a => "900a2", b => "900b2" },
+              { a => "900a3", b => "900b3" },
+            ],
+   902   => [{ a => "900a1", b => "900b1" }],
+   array => ["a", "b", "c"],
+   strange_field => "value",
+}, 'is_deeply');
+
+ok( $hash = WebPAC::Output->ds_to_hash( $ds, 'foo', disable_key_mungle => 1 ), 'ds_to_hash disable_key_mungle' );
+diag dump($hash) if $debug;
+is_deeply( $hash, {
+   "000" => [42],
+   900   => [
+              { a => "900a1", b => "900b1" },
+              { a => "900a2", b => "900b2" },
+              { a => "900a3", b => "900b3" },
+            ],
+   902   => [{ a => "900a1", b => "900b1" }],
+   array => ["a", "b", "c"],
+   'StRaNgE!Field' => "value",
+}, 'is_deeply');
+
+ok( $hash = WebPAC::Output->ds_to_hash( $ds, 'foo', single_values => 1 ), 'ds_to_hash single_values' );
+diag dump($hash) if $debug;
+is_deeply( $hash, {
+   "000" => 42,
+   900 => "{ a => \"900a1\", b => \"900b1\" } { a => \"900a2\", b => \"900b2\" } { a => \"900a3\", b => \"900b3\" }",
+   902 => "{ a => \"900a1\", b => \"900b1\" }",
+   array => "a b c",
+   strange_field => "value",
+}, 'is_deeply');
+
+