File: unit_core_script_test.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (55 lines) | stat: -rw-r--r-- 1,267 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use strict;
use warnings;

use Carp qw(croak);
use FindBin qw/$Bin/;
use lib "$Bin/lib";

use Test::More;
use Test::Fatal;

use Catalyst::Script::Test;
use File::Temp qw/tempfile/;
use IO::Handle;

is run_test('/'), "root index\n", 'correct content printed';
is run_test('/moose/get_attribute'), "42\n", 'Correct content printed for non root action';

done_testing;

sub run_test {
    my $url = shift;

    my ($fh, $fn) = tempfile();

    binmode( $fh );
    binmode( STDOUT );

    {
        local @ARGV = ($url);
        my $i;
        is exception {
            $i = Catalyst::Script::Test->new_with_options(application_name => 'TestApp');
        }, undef, "new_with_options";
        ok $i;
        my $saved;
        open( $saved, '>&'. STDOUT->fileno )
            or croak("Can't dup stdout: $!");
        open( STDOUT, '>&='. $fh->fileno )
            or croak("Can't open stdout: $!");
        eval { $i->run };
        ok !$@, 'Ran ok';

        STDOUT->flush
            or croak("Can't flush stdout: $!");

        open( STDOUT, '>&'. fileno($saved) )
            or croak("Can't restore stdout: $!");
    }

    my $data = do { my $fh; open($fh, '<', $fn) or die $!; local $/; <$fh>; };
    $fh = undef;
    unlink $fn if -r $fn;

    return $data;
}