[BACK]Return to network_tests.t CVS log [TXT][DIR] Up to [local] / HOPE / Net-OpenAMD / t

File: [local] / HOPE / Net-OpenAMD / t / network_tests.t (download)

Revision 1.9, Tue Jul 13 02:27:06 2010 UTC (13 years, 11 months ago) by andrew
Branch: MAIN
Changes since 1.8: +76 -248 lines

Switch testing to Test::Deep because the flexibility is required.  Unfortunately, api.hope.net/api is 500 errors at the moment.

#!perl
package NetworkTests;

# $AFresh1: network_tests.t,v 1.9 2010/07/13 02:27:06 andrew Exp $

use strict;
use warnings;

use Test::More;
use Test::Deep;
use Net::OpenAMD;
use Data::Dumper;

if ( !caller() ) {
    if ( $ENV{'NETWORK_TESTS'} ) {
        plan tests => 13;
    }
    else {
        plan skip_all =>
            'Network test.  Set $ENV{NETWORK_TESTS} to a true value to run.';
    }

    my $amd
        = Net::OpenAMD->new( { base_uri => 'http://api.hope.net/api/', } );
    run_tests($amd);

    #done_testing();
}

1;

sub run_tests {
    my ($amd) = @_;

    my @interests = (
        "new tech",           "activism",
        "radio",              "lockpicking",
        "crypto",             "privacy",
        "ethics",             "telephones",
        "social engineering", "hacker spaces",
        "hardware hacking",   "nostalgia",
        "communities",        "science",
        "government",         "network security",
        "malicious software", "pen testing",
        "web",                "niche hacks",
        "media"
    );
    my %cmp = (
        single_line   => re('^[^\n]+$'),
        multi_line    => re('^.*$'),
        digits        => re('^\d+$'),
        track         => any( 'Lovelace', 'Tesla', 'Bell', 'Hooper' ),
        area          => any('Engressia'),
        interests     => any(@interests),
        all_interests => bag(@interests),
        coordinate    => re('^\d\d\.\d+$'),
        boolean => any( 'True', 'False' ),
    );

    #$cmp{speaker} = $cmp{single_line};

    my %tests = (
        location => [
            {   args   => undef,
                expect => array_each(
                    {   area   => $cmp{area},
                        user   => $cmp{digits},
                        button => $cmp{boolean},
                        x      => $cmp{coordinate},
                        y      => $cmp{coordinate},
                        time   => $cmp{single_line},
                    }
                ),
            },
            { args => { user => 'user0' },
                    expect => array_each(),
            },
        ],
        speakers => [
            {   args   => undef,
                expect => array_each(
                    {   name => $cmp{single_line},
                        bio  => $cmp{multi_line},
                    }
                ),
            },
        ],
        talks => [
            {   args   => undef,
                expect => array_each(
                    {   abstract  => $cmp{single_line},
                        interests => array_each( $cmp{interests} ),
                        speakers  => array_each( $cmp{single_line} ),
                        time      => $cmp{single_line},
                        title     => $cmp{multi_line},
                        track     => $cmp{track},
                    }
                ),
            },
        ],
        interests => [
            {   args   => undef,
                expect => $cmp{all_interests},
            },
        ],
        users => [
            {   args   => undef,
                expect => array_each(
                    {   interests => array_each( $cmp{interests} ),
                        name      => $cmp{single_line},
                        x         => $cmp{coordinate},
                        y         => $cmp{coordinate},
                    }
                ),
            },
        ],
        stats => [
            {   args   => undef,
                expect => qr/^Unused \s feature/xms,
            },
        ],
    );

    foreach my $method ( keys %tests ) {
        foreach my $test ( @{ $tests{$method} } ) {
            no warnings 'uninitialized';
            my $result;
            eval { $result = $amd->$method( $test->{args} ) };
            if ( ref $test->{expect} eq 'Regexp' ) {
                like( $@, $test->{expect}, "AMD->$method($test->{args})" );
            }
            elsif ( ref $test->{expect} ) {
                is( $@, '', "AMD->$method($test->{args})" );
                cmp_deeply( $result, $test->{expect},
                          "AMD->$method($test->{args}) - "
                        . 'got expected result' );
            }
            else {
                is( $@, $test->{expect}, "AMD->$method($test->{args})" );
            }
        }
    }
}