Skip to content

Commit

Permalink
Add the ability to get objects back
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed Dec 7, 2009
1 parent fd9ae8b commit dded997
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 58 deletions.
14 changes: 14 additions & 0 deletions lib/CPAN/Testers/WWW/Reports/Parser.pm
Expand Up @@ -36,9 +36,14 @@ sub new {
'format' => uc $hash{'format'},
'data' => $hash{'data'},
'file' => $hash{'file'},
'report_objects' => $hash{'report_objects'},
};
bless $self, $class;

if($self->{report_objects}) {
eval "use CPAN::Testers::WWW::Reports::Report";
}

my $parser = 'CPAN::Testers::WWW::Reports::Parser::' . $self->{'format'};
eval "use $parser";
croak "Cannot access $self->{'format'} parser, have you installed the necessary support modules?\n"
Expand Down Expand Up @@ -131,6 +136,11 @@ sub report {
}
}

if($self->{report_objects}) {
my $rep = CPAN::Testers::WWW::Reports::Report->new(\%report);
return $rep;
}

return \%report;
}

Expand Down Expand Up @@ -177,6 +187,7 @@ CPAN::Testers::WWW::Reports::Parser - CPAN Testers reports data parser
my $obj = CPAN::Testers::WWW::Reports::Parser->new(
format => 'YAML', # or 'JSON'
file => $file # or data => $data
report_objects => 1, # Optional, works with $obj->report()
);
# iterator, accessing aternate field names
Expand All @@ -188,6 +199,9 @@ CPAN::Testers::WWW::Reports::Parser - CPAN Testers reports data parser
# the individual field names, as the $data variable is a hashref to a
# hash of a single report.
}
# if 'report_objects' was set, then $obj->report() will return
# CPAN::Testers::WWW::Reports::Report objects instead of a hashref
# iterator, filtering field names
$obj->filter(@fields);
Expand Down
145 changes: 87 additions & 58 deletions t/10json.t
Expand Up @@ -6,107 +6,136 @@ use Data::Dumper;

eval "use JSON::XS";
plan skip_all => "JSON::XS required for testing JSON parser" if $@;
plan tests => 30;
plan tests => 33;

my $count = 100;
my $count = 100;
my $report_original = {
'ostext' => 'Linux',
'version' => '0.12',
'status' => 'PASS',
'dist' => undef,
'osvers' => '2.6.27.19-5-default',
'csspatch' => 'unp',
'state' => 'pass',
'distribution' => 'App-Maisha',
'perl' => '5.10.0',
'distversion' => 'App-Maisha-0.12',
'cssperl' => 'rel',
'osname' => 'linux',
'platform' => 's390x-linux',
'id' => '3702934'
'ostext' => 'Linux',
'version' => '0.12',
'status' => 'PASS',
'dist' => undef,
'osvers' => '2.6.27.19-5-default',
'csspatch' => 'unp',
'state' => 'pass',
'distribution' => 'App-Maisha',
'perl' => '5.10.0',
'distversion' => 'App-Maisha-0.12',
'cssperl' => 'rel',
'osname' => 'linux',
'platform' => 's390x-linux',
'id' => '3702934'
};
my $report_filtered = {
'version' => '0.12',
'grade' => 'PASS',
'distname' => 'App-Maisha'
'version' => '0.12',
'grade' => 'PASS',
'distname' => 'App-Maisha'
};
my $report_extended = {
'ostext' => 'Linux',
'version' => '0.12',
'status' => 'PASS',
'grade' => 'PASS',
'dist' => undef,
'osvers' => '2.6.27.19-5-default',
'csspatch' => 'unp',
'state' => 'pass',
'distribution' => 'App-Maisha',
'perl' => '5.10.0',
'distversion' => 'App-Maisha-0.12',
'cssperl' => 'rel',
'osname' => 'linux',
'platform' => 's390x-linux',
'id' => '3702934',
'distname' => 'App-Maisha'
'ostext' => 'Linux',
'version' => '0.12',
'status' => 'PASS',
'grade' => 'PASS',
'dist' => undef,
'osvers' => '2.6.27.19-5-default',
'csspatch' => 'unp',
'state' => 'pass',
'distribution' => 'App-Maisha',
'perl' => '5.10.0',
'distversion' => 'App-Maisha-0.12',
'cssperl' => 'rel',
'osname' => 'linux',
'platform' => 's390x-linux',
'id' => '3702934',
'distname' => 'App-Maisha'
};
my @fields = qw(distname version grade);
my @fields = qw(distname version grade);
my @all_fields = qw(
id distribution dist distname version distversion perl
state status grade action osname ostext osvers platform
archname url csspatch cssperl);

id distribution dist distname version distversion perl
state status grade action osname ostext osvers platform
archname url csspatch cssperl);

my $obj = CPAN::Testers::WWW::Reports::Parser->new(
'format' => 'JSON',
'file' => './t/samples/App-Maisha.json'
);
isa_ok($obj,'CPAN::Testers::WWW::Reports::Parser');
isa_ok( $obj, 'CPAN::Testers::WWW::Reports::Parser' );

my $data = $obj->reports();

#diag(Dumper($data->[0]));
is(scalar(@$data),$count,'.. report count correct');
is_deeply($data->[0],$report_original,'.. matches original report');
is( scalar(@$data), $count, '.. report count correct' );
is_deeply( $data->[0], $report_original, '.. matches original report' );

$data = $obj->reports(@fields);

#diag(Dumper($data->[0]));
is(scalar(@$data),$count,'.. report count correct');
is_deeply($data->[0],$report_filtered,'.. matches filtered report');
is( scalar(@$data), $count, '.. report count correct' );
is_deeply( $data->[0], $report_filtered, '.. matches filtered report' );

$data = $obj->reports( 'ALL', @fields );

$data = $obj->reports('ALL',@fields);
#diag(Dumper($data->[0]));
is(scalar(@$data),$count,'.. report count correct');
is_deeply($data->[0],$report_extended,'.. matches extended report');
is( scalar(@$data), $count, '.. report count correct' );
is_deeply( $data->[0], $report_extended, '.. matches extended report' );

$obj->filter();
$data = $obj->report();
is_deeply($data,$report_original,'.. matches original report');
is_deeply( $data, $report_original, '.. matches original report' );

$obj->{loaded} = 0;
$obj->filter(@fields);
$data = $obj->report();

#diag(Dumper($data));
is_deeply($data,$report_filtered,'.. matches filtered report');
is_deeply( $data, $report_filtered, '.. matches filtered report' );

$obj->{loaded} = 0;
$obj->filter('ALL',@fields);
$obj->filter( 'ALL', @fields );
$data = $obj->report();
is_deeply($data,$report_extended,'.. matches extended report');
is_deeply( $data, $report_extended, '.. matches extended report' );

$obj->{loaded} = 0;
$obj->filter();
my $reports = 0;
while( $data = $obj->report() ) { $reports++ };
is($reports,$count,'.. report count correct');
while ( $data = $obj->report() ) { $reports++ }
is( $reports, $count, '.. report count correct' );

{
$obj->{loaded} = 0;
$obj->filter(@all_fields);
$data = $obj->report();

no strict 'refs';
for (qw( id distribution dist distname version distversion perl
state status grade action osname ostext osvers platform
archname url csspatch cssperl )) {
is($obj->$_(),$data->{$_},".. field '$_' matches direct and indirect access");
for (
qw( id distribution dist distname version distversion perl
state status grade action osname ostext osvers platform
archname url csspatch cssperl )
)
{
is( $obj->$_(), $data->{$_},
".. field '$_' matches direct and indirect access" );
}
}

# Test object
my $obj_tester = CPAN::Testers::WWW::Reports::Parser->new(
'format' => 'JSON',
'file' => './t/samples/App-Maisha.json',
'report_objects' => 1,
);
isa_ok( $obj_tester, 'CPAN::Testers::WWW::Reports::Parser' );

my $report_obj = $obj_tester->report();
isa_ok( $report_obj, 'CPAN::Testers::WWW::Reports::Report' );
is($report_obj->version, '0.12', 'Got a version as expected');











0 comments on commit dded997

Please sign in to comment.