App-Pod

 view release on metacpan or  search on metacpan

t/01-usage-simple.t  view on Meta::CPAN

#!perl
use v5.24;    # Postfix defef.
use strict;
use warnings;
use Test::More tests => 35;
use Term::ANSIColor       qw( colorstrip );
use File::Spec::Functions qw( catfile catdir );
use open                  qw( :std :utf8 );
use FindBin               qw( $RealDir );
use lib catdir( $RealDir, "cpan" );

sub _dumper {
    require Data::Dumper;
    my $data = Data::Dumper
      ->new( [@_] )
      ->Indent( 1 )
      ->Sortkeys( 1 )
      ->Terse( 1 )
      ->Useqq( 1 )
      ->Dump;
    return $data if defined wantarray;
    say $data;
}

BEGIN {
    use_ok( 'App::Pod' ) || print "Bail out!\n";
}

diag( "Testing App::Pod $App::Pod::VERSION, Perl $], $^X" );

{
    no warnings qw( redefine once );

    # Make sure this is already defined a a number.
    like( Pod::Query::get_term_width(),
        qr/^\d+$/, "get_term_width returns a number" );

    *Pod::Query::get_term_width = sub { 55 };    # Match android.
}


my $sample_pod        = catfile( $RealDir, qw( cpan Mojo2 UserAgent.pm ) );
my $windows_safe_path = $sample_pod =~ s&(\\)&\\$1&gr;

ok( -f $sample_pod, "pod file exists: $sample_pod" );

my @cases = (

    # --help
    {
        name            => "No Input shows help",
        input           => [],
        expected_output => [
            "",
            "Syntax:",
            "  pod module_name [method_name] [options]",
            "",
            "Options:",
            "  --help, -h            - Show this help section.",
            "  --version, -v         - Show this tool version.",
            "  --tool_options, --to  - List tool options.",
            "  --class_options, --co - Class events and methods.",
            "  --doc, -d             - View class documentation.",
            "  --edit, -e            - Edit the source code.",
            "  --query, -q           - Run a pod query.",
            "  --dump, --dd          - Dump extra info (adds up).",
            "  --all, -a             - Show all class functions.",
            "  --no_colors           - Do not output colors.",
            '  --no_error            - Suppress some error message.',
            "  --flush_cache, -f     - Flush cache file(s).",
            "",
            "Examples:",
            "  # All or a method",
            "  pod Mojo::UserAgent",
            "  pod Mojo::UserAgent prepare",
            "",
            "  # Documentation",
            "  pod Mojo::UserAgent -d",
            "",
            "  # Edit class or method",
            "  pod Mojo::UserAgent -e",
            "  pod Mojo::UserAgent prepare -e",

t/01-usage-simple.t  view on Meta::CPAN

            '',
            'Methods (36):',
            ' build_tx           - Generate Mojo::Transaction::H ...',
            ' build_websocket_tx - Generate Mojo::Transaction::H ...',
            ' ca                 - Path to TLS certificate autho ...',
            ' cert               - Path to TLS certificate file, ...',
            ' connect_timeout    - Maximum amount of time in sec ...',
            ' cookie_jar         - Cookie jar to use for request ...',
            ' delete             - Perform blocking DELETE reque ...',
            ' delete_p           - Same as "delete", but perform ...',
            ' get                - Perform blocking GET request  ...',
            ' get_p              - Same as "get", but performs a ...',
            ' head               - Perform blocking HEAD request ...',
            ' head_p             - Same as "head", but performs  ...',
            ' inactivity_timeout - Maximum amount of time in sec ...',
            ' insecure           - Do not require a valid TLS ce ...',
            ' ioloop             - Event loop object to use for  ...',
            ' key                - Path to TLS key file, default ...',
            ' max_connections    - Maximum number of keep-alive  ...',
            ' max_redirects      - Maximum number of redirects t ...',
            ' max_response_size  - Maximum response size in byte ...',
            ' options            - Perform blocking OPTIONS requ ...',
            ' options_p          - Same as "options", but perfor ...',
            ' patch              - Perform blocking PATCH reques ...',
            ' patch_p            - Same as "patch", but performs ...',
            ' post               - Perform blocking POST request ...',
            ' post_p             - Same as "post", but performs  ...',
            ' proxy              - Proxy manager, defaults to a  ...',
            ' put                - Perform blocking PUT request  ...',
            ' put_p              - Same as "put", but performs a ...',
            ' request_timeout    - Maximum amount of time in sec ...',
            ' server             - Application server relative U ...',
            ' socket_options     - Additional options for IO::So ...',
            ' start              - Emitted whenever a new transa ...',
            ' start_p            - Same as "start", but performs ...',
            ' transactor         - Transaction builder, defaults ...',
            ' websocket          - Open a non-blocking WebSocket ...',
            ' websocket_p        - Same as "websocket", but retu ...',
            '',
            'Use --all (or -a) to see all methods.',
        ],
    },

    # Class method.
    {
        name            => "Module - ojo x",
        input           => [qw( ojo x )],
        expected_output => [
            "",
            "Package: ojo",
            "Path:    <PATH>",
            "",
            "ojo - Fun one-liners with Mojo",
            "",
            "x:",
            "",
            "  my \$dom = x('<div>Hello!</div>');",
            "",
            "  Turn HTML/XML input into Mojo::DOM object.",
            "",
"  \$ perl -Mojo -E 'say x(f(\"test.html\")->slurp)->at(\"title\")->text'",
            "",
            "  [UnicodeTest: I ♥ Mojolicious!]",
        ]
    },

    # --query bad
    {
        name            => "query with no class",
        input           => [qw( --query head1[0]/Para )],
        expected_output => [ "", "Class name not provided!" ],
    },
    {
        name            => "query with no class",
        input           => [qw( --query head1[0]/Para )],
        expected_output => [ "", "Class name not provided!" ],
    },
    {
        name            => "query with bad class",
        input           => [qw( ojo2 --query head1[0]/Para )],
        expected_output => [ "", "Class not found: ojo2" ],
    },

    # --query good
    {
        name            => "query",
        input           => [qw( Mojo2::UserAgent --query head1[0]/Para )],
        expected_output =>
          ["Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent"],
    },
    {
        name            => "query TOC",
        input           => [qw( Mojo2::UserAgent --query head1 )],
        expected_output => [
            "NAME",       "SYNOPSIS", "DESCRIPTION", "EVENTS",
            "ATTRIBUTES", "METHODS",  "DEBUGGING",   "SEE ALSO"
        ]
    },
    {
        name            => "query with class at end",
        input           => [qw( --query head1[0]/Para Mojo2::UserAgent )],
        expected_output =>
          ["Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent"],
    },
    {
        name            => "query with class at end and method",
        input           => [qw( --query head1[0]/Para Mojo2::UserAgent get )],
        expected_output =>
          ["Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent"],
    },
    {
        name  => "query_dump",
        input => [qw( Mojo2::UserAgent --query head1[0]/Para --dump )],
        expected_output => [
            "_process_non_main()",
            "Processing: query",
            "DEBUG_FIND_DUMP: [",
            "  {",
            "    \"keep\" => 1,",
            "    \"prev\" => [],",
            "    \"tag\" => \"Para\",",

t/01-usage-simple.t  view on Meta::CPAN

            q( new                   - Construct a new Mojo::File ...),
            q( open                  - Open file with IO::File.),
            q( path                  - Construct a new scalar-bas ...),
            q( realpath              - Resolve the path with Cwd  ...),
            q( rel2abs),
            q( remove                - Delete file.),
            q( remove_tree           - Delete this directory and  ...),
            q( sibling               - Return a new Mojo::File ob ...),
            q( slurp                 - Read all data at once from ...),
            q( splitdir),
            q( spurt                 - Write all data at once to  ...),
            q( stat                  - Return a File::stat object ...),
            q( tap                   - Alias for "tap" in Mojo::Base.),
            q( tempdir               - Construct a new scalar-bas ...),
            q( tempfile              - Construct a new scalar-bas ...),
            q( to_abs                - Return absolute path as a  ...),
            q( to_array              - Split the path on director ...),
            q( to_rel                - Return a relative path fro ...),
            q( to_string             - Stringify the path.),
            q( touch                 - Create file if it does not ...),
            q( with_roles            - Alias for "with_roles" in  ...),
        ],
    },
);

my $is_path       = qr/ ^ Path: \s* \K (.*) $ /x;
my $is_version    = qr/ \b \d+\.\d+  $ /x;
my $is_cache_path = qr/ "_cache_path" \s+ => \K \s+ ".*" /x;

for my $case ( @cases ) {
    local @ARGV = ( $case->{input}->@* );
    my $input = "@ARGV";
    my $out   = "";

    # Capture output.
    {
        local *STDOUT;
        local *STDERR;
        open STDOUT, ">",  \$out or die $!;
        open STDERR, ">>", \$out or die $!;
        eval { App::Pod->run };
        if ( $@ ) {
            $out = $@;
            chomp $out;
        }
    }

    my @lines = split /\n/, colorstrip( $out // '' );

    # Normalize PATHs
    for ( @lines ) {
        s/$is_path/<PATH>/;
        s/$is_cache_path/ "PATH"/g;
    }

    # Normalize Version
    if ( "$input" eq "--version" ) {
        $lines[0] =~ s/$is_version/<VERSION>/;
    }

    say STDERR _dumper \@lines
      and last
      unless is_deeply( \@lines, $case->{expected_output}, $case->{name} );
}



( run in 1.283 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )