[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.3

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.3     ! andrew     10: #     REVISION:  $AFresh1: response.t,v 1.2 2010/01/26 05:47:30 andrew Exp $
1.1       andrew     11: #===============================================================================
                     12:
                     13: use strict;
                     14: use warnings;
                     15:
1.3     ! andrew     16: use Test::More tests => 77;
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:         },
                     60:     },
                     61:
                     62:     list => {
                     63:         undef => {
                     64:             result => '/^Unable to handle \[render_list\] for format \[\]/',
                     65:         },
                     66:         text => {
                     67:             result      => undef,
                     68:             data_result => ( join q{}, map "$_->{text}\n", @{ $data{list} } ),
1.2       andrew     69:             content_type => 'text/plain',
1.1       andrew     70:         },
                     71:         md5 => {
1.2       andrew     72:             result      => undef,
                     73:             data_result => (
                     74:                 join q{},
                     75:                 map "MD5 ($_->{text}) = $_->{md5}\n",
                     76:                 @{ $data{list} }
                     77:             ),
1.3     ! andrew     78:             content_type => 'text/plain',
        !            79:         },
        !            80:     },
        !            81:
        !            82:     tags => {
        !            83:         undef => {
        !            84:             result => '/^Unable to handle \[render_tags\] for format \[\]/',
        !            85:         },
        !            86:         text => {
        !            87:             result       => undef,
        !            88:             data_result  => ( join q{}, map "$_\n", @{ $data{tags} } ),
        !            89:             content_type => 'text/plain',
        !            90:         },
        !            91:         md5 => {
        !            92:             result      => undef,
        !            93:             result => '/^Unable to handle \[render_tags\] for format \[md5\]/',
1.2       andrew     94:             content_type => 'text/plain',
1.1       andrew     95:         },
                     96:     },
                     97:
                     98:     entry => {
                     99:         undef => {
                    100:             result => '/^Unable to handle \[render_entry\] for format \[\]/',
                    101:         },
                    102:         text => {
1.2       andrew    103:             result       => undef,
                    104:             data_result  => ( join q{}, map "$_->{text}\n", $data{entry} ),
                    105:             content_type => 'text/plain',
1.1       andrew    106:         },
                    107:         md5 => {
1.2       andrew    108:             result      => undef,
                    109:             data_result => (
                    110:                 join q{}, map "MD5 ($_->{text}) = $_->{md5}\n",
                    111:                 $data{entry}
                    112:             ),
                    113:             content_type => 'text/plain',
1.1       andrew    114:         },
                    115:     },
                    116: );
                    117:
                    118: foreach my $type ( sort keys %TESTS ) {
                    119:     foreach my $format ( sort keys %{ $TESTS{$type} } ) {
                    120:         foreach my $data ( undef, $data{$type} ) {
                    121:             my $args;
                    122:             if ( $type ne 'undef' ) {
                    123:                 $args->{type} = $type;
                    124:             }
                    125:             if ( $format ne 'undef' ) {
                    126:                 $args->{format} = $format;
                    127:             }
                    128:             if ($data) {
                    129:                 $args->{data} = $data;
                    130:             }
                    131:
                    132:             my $diag = "type [$type] format [$format]";
                    133:             if ($data) {
                    134:                 $diag .= ' with data';
                    135:             }
                    136:
                    137:             test( $TESTS{$type}{$format}, $args, $diag );
                    138:         }
                    139:     }
                    140: }
                    141:
                    142: sub test {
                    143:     my ( $test, $args, $diag ) = @_;
                    144:
                    145:     if ( !exists $args->{type} ) {
                    146:         ok( !eval { $class->new($args) }, "Create $class without type" );
                    147:         like( $@, '/^option \[type\] required/', 'Got expected error' );
                    148:         return 1;
                    149:     }
                    150:
                    151:     my $response = new_ok( $class, [$args] );
                    152:
1.2       andrew    153:     if ( exists $test->{content_type} ) {
                    154:         is( $response->content_type, $test->{content_type},
                    155:             'Got content_type - ' . $diag );
                    156:     }
                    157:
1.1       andrew    158:     my $expected = $test->{result};
                    159:     if ( $args->{data} && exists $test->{data_result} ) {
                    160:         $expected = $test->{data_result};
                    161:     }
                    162:
                    163:     my $result;
                    164:     eval { $result = $response->render() };
                    165:     if ($@) {
1.2       andrew    166:         if ( index $expected, '/' ) {
1.1       andrew    167:             $expected = '/^$/';
                    168:         }
                    169:
                    170:         like( $@, $expected, 'Got expected eval_error - ' . $diag );
                    171:     }
                    172:     else {
                    173:         is( $result, $expected, 'Got expected result - ' . $diag );
                    174:     }
1.2       andrew    175:
1.1       andrew    176:     return 1;
                    177: }

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