File: 15-scope.t

package info (click to toggle)
libsystem-command-perl 1.07-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 144 kB
  • sloc: perl: 170; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 3,889 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use strict;
use warnings;
use Test::More;
use System::Command;
use File::Spec;
use Scalar::Util 'refaddr';

plan tests => my $tests;

my @cmd = ( $^X, File::Spec->catfile( t => 'lines.pl' ) );

# record destruction
my @destroyed;
{
    no strict 'refs';
        my $class   = "System::Command";
        my $destroy = *{"$class\::DESTROY"}{CODE};
        *{"$class\::DESTROY"} = sub {
            diag "DESTROY $_[0]";
            push @destroyed, refaddr $_[0];
            $destroy->(@_) if $destroy;
        };
}

# test various scope situations and object destruction time
my ( $cmd_addr );

# test 1
BEGIN { $tests += 5 }
{
    my $cmd = System::Command->new(@cmd);
    $cmd_addr  = refaddr $cmd;
    my ( $out, $err ) = ( $cmd->stdout, $cmd->stderr );
    ok( eof $out, 'No output' );
    ok( eof $err, 'No errput' );
    is( scalar @destroyed, 0, "Destroyed no object yet" );
}
is( scalar @destroyed, 1,          "Destroyed 1 object" );
is( shift @destroyed,  $cmd_addr,  "... command object was destroyed" );
@destroyed = ();

# test 2
BEGIN { $tests += 5 }
{
    my $cmd = System::Command->new( @cmd, 1, 1, 1 );
    $cmd_addr  = refaddr $cmd;

    {
        my $fh = $cmd->stdout;
        my $ln = <$fh>;
        is( $ln, "STDOUT line 1\n", 'scope: { $cmd { $fh } { $fh } }' );
    }
    {
        my $fh = $cmd->stdout;
        my $ln = <$fh>;
        is( $ln, "STDOUT line 2\n", 'scope: { $cmd { $fh } { $fh } }' );
    }
    is( scalar @destroyed, 0, "Destroyed no object yet" );
}
is( scalar @destroyed, 1,          "Destroyed 1 objects" );
is( shift @destroyed,  $cmd_addr,  "... command object was destroyed" );
@destroyed = ();

# test 3
BEGIN { $tests += 3 }
{
    my $fh = System::Command->new( @cmd, 1 )->stdout;
    is( scalar @destroyed, 1, "Destroyed 1 object" );
    @destroyed = ();
    my $ln = <$fh>;
    is( $ln, "STDOUT line 1\n", 'scope: { $fh = cmd->fh }' );
}
is( scalar @destroyed, 0, "Destroyed no object" );
@destroyed = ();

# test 4
BEGIN { $tests += 1 }
System::Command->new(@cmd);
is( scalar @destroyed, 1, "Destroyed 1 object (command)" );
@destroyed = ();

# test 5
BEGIN { $tests += 4 }
{
    my $fh;
    {
        my $cmd = System::Command->new( @cmd, 2 );
        $cmd_addr  = refaddr $cmd;
        $fh        = $cmd->stdout;
    }
    is( scalar @destroyed, 1,         "Destroyed 1 object (command)" );
    is( shift @destroyed,  $cmd_addr, "... command object was destroyed" );
    @destroyed = ();
    my $out = join '', <$fh>;
    is( $out, << 'OUT', 'scope: { $fh = $cmd->fh }; $fh }' );
STDOUT line 1
STDOUT line 2
OUT
}
is( scalar @destroyed, 0,          "Destroyed no objects (reaper)" );
@destroyed = ();

# test 6
BEGIN { $tests += 5 }
{
    my $cmd = System::Command->new( @cmd, 1, 2, 2, 1 );
    $cmd_addr  = refaddr $cmd;

    {
        my $fh = $cmd->stdout;
        my $out = join '', <$fh>;
        is( $out, << 'OUT', 'scope: { $cmd { $fh } { $fh } }' );
STDOUT line 1
STDOUT line 2
STDOUT line 3
OUT
    }
    {
        my $fh = $cmd->stderr;
        my $err = join '', <$fh>;
        is( $err, << 'ERR', 'scope: { $cmd { $fh } { $fh } }' );
STDERR line 1
STDERR line 2
STDERR line 3
ERR
    }
    is( scalar @destroyed, 0, "Destroyed no object yet" );
}
is( scalar @destroyed, 1,          "Destroyed 1 objects" );
is( shift @destroyed,  $cmd_addr,  "... command object was destroyed" );
@destroyed = ();

# test 7
BEGIN { $tests += 4 }
{
    my ( $pid, $in, $out, $err ) = System::Command->spawn( @cmd, 1, 2, 2, 1 );
    is( scalar @destroyed, 1, "Destroyed command object" );
    shift @destroyed;
    my $errput = join '', <$err>;
    my $output = join '', <$out>;
    is( $output, << 'OUT', 'scope: spawn()' );
STDOUT line 1
STDOUT line 2
STDOUT line 3
OUT
    is( $errput, << 'ERR', 'scope: spawn()' );
STDERR line 1
STDERR line 2
STDERR line 3
ERR
}
is( scalar @destroyed, 0, "Destroyed neaper object" );
@destroyed = ();