Skip to content

Commit 39d4f10

Browse files
author
Chris White
committed
Added persistent-state test
- PerlPPTest can now take a PerlPP instance to use - Added t/08-persistent-state to test a -D in one call to Main followed by a use of the defined symbol in another call to Main - Bugfixes in skipping in t/05-external-command.t - Bumped version number to 0.500.4
1 parent 88fbf59 commit 39d4f10

File tree

6 files changed

+76
-26
lines changed

6 files changed

+76
-26
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Revision history for Text-PerlPP
22
(Note: GH = GitHub issue; # = RT issue)
33

4+
0.500.4 2018/05/29
5+
Updated tests
6+
47
0.500.3 2018/05/28
58
Fixes to tests; updated documentation
69

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ t/04-include.t
1818
t/05-external-command.t
1919
t/06-macro.t
2020
t/07-invalid.t
21+
t/08-persistent-state.t
2122
t/a.txt
2223
t/b.txt
2324
t/c.txt

lib/Text/PerlPP.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package Text::PerlPP;
55

66
# Semantic versioning, packed per Perl rules. Must always be at least one
77
# digit left of the decimal, and six digits right of the decimal.
8-
our $VERSION = '0.500003';
8+
our $VERSION = '0.500004';
99

1010
use 5.010001;
1111
use strict;

t/05-external-command.t

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#!/usr/bin/env perl
2-
# Tests of perlpp <?!...?> external commands
2+
# Tests of perlpp <?!...?> external commands.
33
#
4-
# TODO: On non-Unix, test only `echo` with no parameters.
4+
# Note: On non-Unix, we test only `echo` with no parameters.
5+
# On non-Unix, non-Windows, we skip this because yours truly doesn't know
6+
# enough about what external commands are available! :)
57

68
use rlib 'lib';
79
use PerlPPTest qw(:DEFAULT quote_string);
810
use List::Util 'any';
911

10-
if(any { $_ eq $^O } 'VMS', 'os390', 'os400', 'riscos', 'amigaos') {
11-
plan skip_all => "I don't know how to run this test on $^O";
12-
exit;
12+
my $os = $^O; # so we can mock $^O to test this test code!
13+
14+
if(any { $_ eq $os } 'VMS', 'os390', 'os400', 'riscos', 'amigaos') {
15+
plan skip_all => "I don't know how to run this test on $os";
1316
}
1417

1518
(my $whereami = __FILE__) =~ s/macro\.t$//;
@@ -38,26 +41,27 @@ do {
3841
is($out, "howdy\n", "basic echo");
3942
};
4043

41-
if (any { $_ eq $^O } 'dos', 'os2', 'MSWin32') {
42-
skip "I don't know how to run the rest of the tests on $^O", $ntests-1;
43-
exit;
44-
}
44+
SKIP: {
45+
if (any { $_ eq $os } 'dos', 'os2', 'MSWin32') {
46+
skip "I don't know how to run the rest of the tests on $os", $ntests-1;
47+
}
4548

46-
for my $lrTest (@testcases) {
47-
my ($opts, $testin, $out_re, $err_re) = @$lrTest;
48-
my ($out, $err);
49+
for my $lrTest (@testcases) {
50+
my ($opts, $testin, $out_re, $err_re) = @$lrTest;
51+
my ($out, $err);
4952

50-
#diag "perlpp $opts <<<@{[quote_string $testin]}";
51-
run_perlpp $opts, \$testin, \$out, \$err;
53+
#diag "perlpp $opts <<<@{[quote_string $testin]}";
54+
run_perlpp $opts, \$testin, \$out, \$err;
5255

53-
if(defined $out_re) {
54-
like($out, $out_re);
55-
}
56-
if(defined $err_re) {
57-
like($err, $err_re);
58-
}
56+
if(defined $out_re) {
57+
like($out, $out_re);
58+
}
59+
if(defined $err_re) {
60+
like($err, $err_re);
61+
}
5962

60-
} # foreach test
63+
} # foreach test
64+
} #SKIP
6165

6266
# TODO test -o / --output, and processing input from files rather than stdin
6367

t/08-persistent-state.t

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!perl
2+
# PerlPP: test persistence of state across calls to Main
3+
use rlib 'lib';
4+
use PerlPPTest;
5+
6+
# When testing perlpp as an external command, there is by definition no state
7+
# persistence. Therefore, skip this test file.
8+
if($ENV{PERLPP_NOUSE}) {
9+
plan skip_all => 'Persistent state not tested in this configuration (PERLPP_NOUSE)';
10+
}
11+
12+
plan tests => 3;
13+
14+
my ($in, $out, $err);
15+
my @ioe=\($in, $out, $err);
16+
my $instance = Text::PerlPP->new;
17+
18+
$in = '';
19+
run_perlpp {instance=>$instance, args=>['-D','foo=42']}, @ioe;
20+
is($out, '', 'first call returns nothing');
21+
is($err, '', 'first call succeeds');
22+
23+
$in = 'foo';
24+
run_perlpp {instance=>$instance}, @ioe;
25+
is($out, '42', 'definition carries forward');
26+
27+
done_testing();
28+
# vi: set ts=4 sts=4 sw=4 et ai: #

t/lib/PerlPPTest.pm

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,29 @@ sub L {
3838
} #L
3939

4040
# run_perlpp: Run perlpp
41-
# Args: $lrArgs, $refStdin, $refStdout, $refStderr
41+
# Args: $args, $refStdin, $refStdout, $refStderr
42+
# $args can be: a string, in which case it is split with shellwords();
43+
# an arrayref, in which case it is used as the args; or
44+
# a hashref with (all optional):
45+
# {args} string or arrayref as above
46+
# {instance} an existing Text::PerlPP instance to use
4247
sub run_perlpp {
43-
#say STDERR "args ", Dumper(\@_);
48+
#say STDERR "run_perlpp: ", Dumper(\@_);
4449
my $lrArgs = shift;
50+
my $instance;
51+
if(ref $lrArgs eq 'HASH') {
52+
$instance = $lrArgs->{instance}; # nonexistent => falsy, so it's OK
53+
$lrArgs = $lrArgs->{args}; # or undef, which will become [] below.
54+
#say STDERR "args updated: ", Dumper($lrArgs);
55+
}
56+
4557
my $refStdin = shift // \(my $nullstdin);
4658
my $refStdout = shift // \(my $nullstdout);
4759
my $refStderr = shift // \(my $nullstderr);
4860

4961
my $retval;
5062

51-
$lrArgs = [shellwords($lrArgs)] if ref $lrArgs ne 'ARRAY';
63+
$lrArgs = [shellwords($lrArgs // '')] if ref $lrArgs ne 'ARRAY';
5264
#do { (my $args = Dumper($lrArgs)) =~ s/^/##/gm;
5365
#say STDERR "## args:\n$args"; };
5466

@@ -76,6 +88,8 @@ sub run_perlpp {
7688

7789
} else { # Run perl code under this perl
7890
#say STDERR "# running perlpp internal";
91+
$instance = Text::PerlPP->new unless $instance;
92+
7993
#say STDERR "# redirecting stdin";
8094
open local(*STDIN), '<', $refStdin or die $!;
8195
#say STDERR "# redirected stdin";
@@ -86,7 +100,7 @@ sub run_perlpp {
86100
($$refStdout, $$refStderr, @result) = capture {
87101
# Thanks to http://www.perlmonks.org/bare/?node_id=289391 by Zaxo
88102
#say STDERR "# running perlpp";
89-
my $result = Text::PerlPP->new->Main($lrArgs);
103+
my $result = $instance->Main($lrArgs);
90104
#say STDERR "# done running perlpp";
91105
$result;
92106
};

0 commit comments

Comments
 (0)