File: [local] / todotxt / Text-Todo-REST-API / t / response.t (download)
Revision 1.4, Sat Feb 13 22:15:29 2010 UTC (14 years, 4 months ago) by andrew
Branch: MAIN
CVS Tags: HEAD Changes since 1.3: +22 -2 lines
render_tags in JSON
simplify (a lot) Text::Todo::REST::API::Representations::json
make json representation return [] if passed in undef
and test
|
#===============================================================================
#
# FILE: response.t
#
# DESCRIPTION: Test Text::Todo::REST::API::Response
#
# AUTHOR: Andrew Fresh (AAF), andrew@cpan.org
# COMPANY: Red River Communications
# CREATED: 01/07/10 19:11
# REVISION: $AFresh1: response.t,v 1.4 2010/02/13 22:15:29 andrew Exp $
#===============================================================================
use strict;
use warnings;
use Test::More tests => 101;
my $class;
BEGIN {
$class = 'Text::Todo::REST::API::Response';
use_ok( $class, "use $class" );
}
diag("Testing $class $Text::Todo::REST::API::Response::VERSION");
my %data = (
files => ['todo.txt'],
list => [
{ text => 'uno', md5 => 'XXX md5 of uno XXX' },
{ text => 'dos', md5 => 'XXX md5 of dos XXX' },
{ text => 'tre', md5 => 'XXX md5 of tre XXX' },
],
tags => [ 'delete', 'dos', 'uno' ],
entry => { text => 'ety', md5 => 'XXX md5 of ety XXX' },
);
my %TESTS = (
undef => {
undef => {},
text => {},
md5 => {},
},
files => {
undef => {
result => '/^Unable to handle \[render_files\] for format \[\]/',
},
text => {
result => undef,
data_result => "todo.txt\n",
content_type => 'text/plain',
},
md5 => {
result =>
'/^Unable to handle \[render_files\] for format \[md5\]/',
content_type => 'text/plain',
},
json => {
result => '[]',
data_result => '["todo.txt"]',
content_type => 'application/json',
},
},
list => {
undef => {
result => '/^Unable to handle \[render_list\] for format \[\]/',
},
text => {
result => undef,
data_result => ( join q{}, map "$_->{text}\n", @{ $data{list} } ),
content_type => 'text/plain',
},
md5 => {
result => undef,
data_result => (
join q{},
map "MD5 ($_->{text}) = $_->{md5}\n",
@{ $data{list} }
),
content_type => 'text/plain',
},
json => {
result => '[]',
data_result => '[{"text":"uno","md5":"XXX md5 of uno XXX"},{"text":"dos","md5":"XXX md5 of dos XXX"},{"text":"tre","md5":"XXX md5 of tre XXX"}]',
content_type => 'application/json',
},
},
tags => {
undef => {
result => '/^Unable to handle \[render_tags\] for format \[\]/',
},
text => {
result => undef,
data_result => ( join q{}, map "$_\n", @{ $data{tags} } ),
content_type => 'text/plain',
},
md5 => {
result => undef,
result => '/^Unable to handle \[render_tags\] for format \[md5\]/',
content_type => 'text/plain',
},
json => {
result => '[]',
data_result => '["delete","dos","uno"]',
content_type => 'application/json',
},
},
entry => {
undef => {
result => '/^Unable to handle \[render_entry\] for format \[\]/',
},
text => {
result => undef,
data_result => ( join q{}, map "$_->{text}\n", $data{entry} ),
content_type => 'text/plain',
},
md5 => {
result => undef,
data_result => (
join q{}, map "MD5 ($_->{text}) = $_->{md5}\n",
$data{entry}
),
content_type => 'text/plain',
},
json => {
result => '[]',
data_result => '{"text":"ety","md5":"XXX md5 of ety XXX"}',
content_type => 'application/json',
},
},
);
foreach my $type ( sort keys %TESTS ) {
foreach my $format ( sort keys %{ $TESTS{$type} } ) {
foreach my $data ( undef, $data{$type} ) {
my $args;
if ( $type ne 'undef' ) {
$args->{type} = $type;
}
if ( $format ne 'undef' ) {
$args->{format} = $format;
}
if ($data) {
$args->{data} = $data;
}
my $diag = "type [$type] format [$format]";
if ($data) {
$diag .= ' with data';
}
test( $TESTS{$type}{$format}, $args, $diag );
}
}
}
sub test {
my ( $test, $args, $diag ) = @_;
if ( !exists $args->{type} ) {
ok( !eval { $class->new($args) }, "Create $class without type" );
like( $@, '/^option \[type\] required/', 'Got expected error' );
return 1;
}
my $response = new_ok( $class, [$args] );
if ( exists $test->{content_type} ) {
is( $response->content_type, $test->{content_type},
'Got content_type - ' . $diag );
}
my $expected = $test->{result};
if ( $args->{data} && exists $test->{data_result} ) {
$expected = $test->{data_result};
}
my $result;
eval { $result = $response->render() };
if ($@) {
if ( index $expected, '/' ) {
$expected = '/^$/';
}
like( $@, $expected, 'Got expected eval_error - ' . $diag );
}
else {
is( $result, $expected, 'Got expected result - ' . $diag );
}
return 1;
}