[BACK]Return to response.t CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo-REST-API / t

Annotation of todotxt/Text-Todo-REST-API/t/response.t, Revision 1.4

1.1       andrew      1: #===============================================================================
                      2: #
                      3: #         FILE:  response.t
                      4: #
                      5: #  DESCRIPTION:  Test Text::Todo::REST::API::Response
                      6: #
                      7: #       AUTHOR:  Andrew Fresh (AAF), andrew@cpan.org
                      8: #      COMPANY:  Red River Communications
                      9: #      CREATED:  01/07/10 19:11
1.4     ! andrew     10: #     REVISION:  $AFresh1: response.t,v 1.3 2010/02/13 21:46:13 andrew Exp $
1.1       andrew     11: #===============================================================================
                     12:
                     13: use strict;
                     14: use warnings;
                     15:
1.4     ! andrew     16: use Test::More tests => 101;
1.1       andrew     17:
                     18: my $class;
                     19:
                     20: BEGIN {
                     21:     $class = 'Text::Todo::REST::API::Response';
                     22:     use_ok( $class, "use $class" );
                     23: }
                     24:
                     25: diag("Testing $class $Text::Todo::REST::API::Response::VERSION");
                     26:
                     27: my %data = (
                     28:     files => ['todo.txt'],
1.2       andrew     29:     list  => [
                     30:         { text => 'uno', md5 => 'XXX md5 of uno XXX' },
                     31:         { text => 'dos', md5 => 'XXX md5 of dos XXX' },
                     32:         { text => 'tre', md5 => 'XXX md5 of tre XXX' },
1.1       andrew     33:     ],
1.3       andrew     34:
                     35:     tags  => [ 'delete', 'dos', 'uno' ],
1.2       andrew     36:     entry => { text => 'ety', md5 => 'XXX md5 of ety XXX' },
1.1       andrew     37: );
                     38:
                     39: my %TESTS = (
                     40:     undef => {
                     41:         undef => {},
                     42:         text  => {},
                     43:         md5   => {},
                     44:     },
                     45:
                     46:     files => {
                     47:         undef => {
                     48:             result => '/^Unable to handle \[render_files\] for format \[\]/',
                     49:         },
                     50:         text => {
1.2       andrew     51:             result       => undef,
                     52:             data_result  => "todo.txt\n",
                     53:             content_type => 'text/plain',
1.1       andrew     54:         },
                     55:         md5 => {
                     56:             result =>
                     57:                 '/^Unable to handle \[render_files\] for format \[md5\]/',
1.2       andrew     58:             content_type => 'text/plain',
1.1       andrew     59:         },
1.4     ! andrew     60:         json => {
        !            61:             result       => '[]',
        !            62:             data_result  => '["todo.txt"]',
        !            63:             content_type => 'application/json',
        !            64:         },
1.1       andrew     65:     },
                     66:
                     67:     list => {
                     68:         undef => {
                     69:             result => '/^Unable to handle \[render_list\] for format \[\]/',
                     70:         },
                     71:         text => {
                     72:             result      => undef,
                     73:             data_result => ( join q{}, map "$_->{text}\n", @{ $data{list} } ),
1.2       andrew     74:             content_type => 'text/plain',
1.1       andrew     75:         },
                     76:         md5 => {
1.2       andrew     77:             result      => undef,
                     78:             data_result => (
                     79:                 join q{},
                     80:                 map "MD5 ($_->{text}) = $_->{md5}\n",
                     81:                 @{ $data{list} }
                     82:             ),
1.3       andrew     83:             content_type => 'text/plain',
                     84:         },
1.4     ! andrew     85:         json => {
        !            86:             result       => '[]',
        !            87:             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"}]',
        !            88:             content_type => 'application/json',
        !            89:         },
1.3       andrew     90:     },
                     91:
                     92:     tags => {
                     93:         undef => {
                     94:             result => '/^Unable to handle \[render_tags\] for format \[\]/',
                     95:         },
                     96:         text => {
                     97:             result       => undef,
                     98:             data_result  => ( join q{}, map "$_\n", @{ $data{tags} } ),
                     99:             content_type => 'text/plain',
                    100:         },
                    101:         md5 => {
                    102:             result      => undef,
                    103:             result => '/^Unable to handle \[render_tags\] for format \[md5\]/',
1.2       andrew    104:             content_type => 'text/plain',
1.1       andrew    105:         },
1.4     ! andrew    106:         json => {
        !           107:             result       => '[]',
        !           108:             data_result  => '["delete","dos","uno"]',
        !           109:             content_type => 'application/json',
        !           110:         },
1.1       andrew    111:     },
                    112:
                    113:     entry => {
                    114:         undef => {
                    115:             result => '/^Unable to handle \[render_entry\] for format \[\]/',
                    116:         },
                    117:         text => {
1.2       andrew    118:             result       => undef,
                    119:             data_result  => ( join q{}, map "$_->{text}\n", $data{entry} ),
                    120:             content_type => 'text/plain',
1.1       andrew    121:         },
                    122:         md5 => {
1.2       andrew    123:             result      => undef,
                    124:             data_result => (
                    125:                 join q{}, map "MD5 ($_->{text}) = $_->{md5}\n",
                    126:                 $data{entry}
                    127:             ),
                    128:             content_type => 'text/plain',
1.4     ! andrew    129:         },
        !           130:         json => {
        !           131:             result       => '[]',
        !           132:             data_result  => '{"text":"ety","md5":"XXX md5 of ety XXX"}',
        !           133:             content_type => 'application/json',
1.1       andrew    134:         },
                    135:     },
                    136: );
                    137:
                    138: foreach my $type ( sort keys %TESTS ) {
                    139:     foreach my $format ( sort keys %{ $TESTS{$type} } ) {
                    140:         foreach my $data ( undef, $data{$type} ) {
                    141:             my $args;
                    142:             if ( $type ne 'undef' ) {
                    143:                 $args->{type} = $type;
                    144:             }
                    145:             if ( $format ne 'undef' ) {
                    146:                 $args->{format} = $format;
                    147:             }
                    148:             if ($data) {
                    149:                 $args->{data} = $data;
                    150:             }
                    151:
                    152:             my $diag = "type [$type] format [$format]";
                    153:             if ($data) {
                    154:                 $diag .= ' with data';
                    155:             }
                    156:
                    157:             test( $TESTS{$type}{$format}, $args, $diag );
                    158:         }
                    159:     }
                    160: }
                    161:
                    162: sub test {
                    163:     my ( $test, $args, $diag ) = @_;
                    164:
                    165:     if ( !exists $args->{type} ) {
                    166:         ok( !eval { $class->new($args) }, "Create $class without type" );
                    167:         like( $@, '/^option \[type\] required/', 'Got expected error' );
                    168:         return 1;
                    169:     }
                    170:
                    171:     my $response = new_ok( $class, [$args] );
                    172:
1.2       andrew    173:     if ( exists $test->{content_type} ) {
                    174:         is( $response->content_type, $test->{content_type},
                    175:             'Got content_type - ' . $diag );
                    176:     }
                    177:
1.1       andrew    178:     my $expected = $test->{result};
                    179:     if ( $args->{data} && exists $test->{data_result} ) {
                    180:         $expected = $test->{data_result};
                    181:     }
                    182:
                    183:     my $result;
                    184:     eval { $result = $response->render() };
                    185:     if ($@) {
1.2       andrew    186:         if ( index $expected, '/' ) {
1.1       andrew    187:             $expected = '/^$/';
                    188:         }
                    189:
                    190:         like( $@, $expected, 'Got expected eval_error - ' . $diag );
                    191:     }
                    192:     else {
                    193:         is( $result, $expected, 'Got expected result - ' . $diag );
                    194:     }
1.2       andrew    195:
1.1       andrew    196:     return 1;
                    197: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>