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

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.2     ! andrew     10: #     REVISION:  $AFresh1: response.t,v 1.1 2010/01/23 07:04:43 andrew Exp $
1.1       andrew     11: #===============================================================================
                     12:
                     13: use strict;
                     14: use warnings;
                     15:
1.2     ! andrew     16: use Test::More tests => 61;
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.2     ! andrew     34:     entry => { text => 'ety', md5 => 'XXX md5 of ety XXX' },
1.1       andrew     35: );
                     36:
                     37: my %TESTS = (
                     38:     undef => {
                     39:         undef => {},
                     40:         text  => {},
                     41:         md5   => {},
                     42:     },
                     43:
                     44:     files => {
                     45:         undef => {
                     46:             result => '/^Unable to handle \[render_files\] for format \[\]/',
                     47:         },
                     48:         text => {
1.2     ! andrew     49:             result       => undef,
        !            50:             data_result  => "todo.txt\n",
        !            51:             content_type => 'text/plain',
1.1       andrew     52:         },
                     53:         md5 => {
                     54:             result =>
                     55:                 '/^Unable to handle \[render_files\] for format \[md5\]/',
1.2     ! andrew     56:             content_type => 'text/plain',
1.1       andrew     57:         },
                     58:     },
                     59:
                     60:     list => {
                     61:         undef => {
                     62:             result => '/^Unable to handle \[render_list\] for format \[\]/',
                     63:         },
                     64:         text => {
                     65:             result      => undef,
                     66:             data_result => ( join q{}, map "$_->{text}\n", @{ $data{list} } ),
1.2     ! andrew     67:             content_type => 'text/plain',
1.1       andrew     68:         },
                     69:         md5 => {
1.2     ! andrew     70:             result      => undef,
        !            71:             data_result => (
        !            72:                 join q{},
        !            73:                 map "MD5 ($_->{text}) = $_->{md5}\n",
        !            74:                 @{ $data{list} }
        !            75:             ),
        !            76:             content_type => 'text/plain',
1.1       andrew     77:         },
                     78:     },
                     79:
                     80:     entry => {
                     81:         undef => {
                     82:             result => '/^Unable to handle \[render_entry\] for format \[\]/',
                     83:         },
                     84:         text => {
1.2     ! andrew     85:             result       => undef,
        !            86:             data_result  => ( join q{}, map "$_->{text}\n", $data{entry} ),
        !            87:             content_type => 'text/plain',
1.1       andrew     88:         },
                     89:         md5 => {
1.2     ! andrew     90:             result      => undef,
        !            91:             data_result => (
        !            92:                 join q{}, map "MD5 ($_->{text}) = $_->{md5}\n",
        !            93:                 $data{entry}
        !            94:             ),
        !            95:             content_type => 'text/plain',
1.1       andrew     96:         },
                     97:     },
                     98: );
                     99:
                    100: foreach my $type ( sort keys %TESTS ) {
                    101:     foreach my $format ( sort keys %{ $TESTS{$type} } ) {
                    102:         foreach my $data ( undef, $data{$type} ) {
                    103:             my $args;
                    104:             if ( $type ne 'undef' ) {
                    105:                 $args->{type} = $type;
                    106:             }
                    107:             if ( $format ne 'undef' ) {
                    108:                 $args->{format} = $format;
                    109:             }
                    110:             if ($data) {
                    111:                 $args->{data} = $data;
                    112:             }
                    113:
                    114:             my $diag = "type [$type] format [$format]";
                    115:             if ($data) {
                    116:                 $diag .= ' with data';
                    117:             }
                    118:
                    119:             test( $TESTS{$type}{$format}, $args, $diag );
                    120:         }
                    121:     }
                    122: }
                    123:
                    124: sub test {
                    125:     my ( $test, $args, $diag ) = @_;
                    126:
                    127:     if ( !exists $args->{type} ) {
                    128:         ok( !eval { $class->new($args) }, "Create $class without type" );
                    129:         like( $@, '/^option \[type\] required/', 'Got expected error' );
                    130:         return 1;
                    131:     }
                    132:
                    133:     my $response = new_ok( $class, [$args] );
                    134:
1.2     ! andrew    135:     if ( exists $test->{content_type} ) {
        !           136:         is( $response->content_type, $test->{content_type},
        !           137:             'Got content_type - ' . $diag );
        !           138:     }
        !           139:
1.1       andrew    140:     my $expected = $test->{result};
                    141:     if ( $args->{data} && exists $test->{data_result} ) {
                    142:         $expected = $test->{data_result};
                    143:     }
                    144:
                    145:     my $result;
                    146:     eval { $result = $response->render() };
                    147:     if ($@) {
1.2     ! andrew    148:         if ( index $expected, '/' ) {
1.1       andrew    149:             $expected = '/^$/';
                    150:         }
                    151:
                    152:         like( $@, $expected, 'Got expected eval_error - ' . $diag );
                    153:     }
                    154:     else {
                    155:         is( $result, $expected, 'Got expected result - ' . $diag );
                    156:     }
1.2     ! andrew    157:
1.1       andrew    158:     return 1;
                    159: }

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