aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-287/ppentchev/tests/lib/PWCTest/Ch287.pm91
1 files changed, 29 insertions, 62 deletions
diff --git a/challenge-287/ppentchev/tests/lib/PWCTest/Ch287.pm b/challenge-287/ppentchev/tests/lib/PWCTest/Ch287.pm
index 943543f9b8..0b27145904 100644
--- a/challenge-287/ppentchev/tests/lib/PWCTest/Ch287.pm
+++ b/challenge-287/ppentchev/tests/lib/PWCTest/Ch287.pm
@@ -47,32 +47,12 @@ my @TEST_NUMBERS = (
['.44', 'true'],
);
-sub test_strong_password_default($) {
- my ($cmd) = @_;
+sub test_run_program($ $ $) {
+ my ($cmd, $input, $expected) = @_;
my $prog = "\`@{$cmd}\`";
plan tests => 2;
- my $auto_cmd = Test::Command->new(cmd => ['env', 'PWC_FROM_STDIN=', @{$cmd}]);
- $auto_cmd->exit_is_num(0, "$prog exited with code 0");
- $auto_cmd->stdout_is_eq(
- "5\n3\n0\n1\n2\n",
- "$prog produced the correct output in autotest mode",
- );
-}
-
-sub test_strong_password_count() {
- scalar @TEST_PASSWORDS
-}
-
-sub test_strong_password($ $) {
- my ($cmd, $idx) = @_;
- my $prog = "\`@{$cmd}\`";
-
- plan tests => 2;
-
- my ($password, $expected) = @{$TEST_PASSWORDS[$idx]};
-
# OK, so Test::Command cannot handle this one; let's do it ourselves
socketpair(my $parent_in, my $child_in, AF_UNIX, SOCK_STREAM, 0) or
die "Could not create the stdin socket pair: $!\n";
@@ -95,7 +75,7 @@ sub test_strong_password($ $) {
close $child_in or die "Parent: could not close child_in: $!\n";
close $child_out or die "Parent: could not close child_out: $!\n";
- say $parent_in $password or die "Parent: could not write to the child: $!\n";
+ say $parent_in $input or die "Parent: could not write to the child: $!\n";
close $parent_in or die "Parent: could not close parent_in: $!\n";
my $line = <$parent_out>;
@@ -110,7 +90,7 @@ sub test_strong_password($ $) {
is $?, 0, "$prog exited with code 0";
}
-sub test_valid_number_default($) {
+sub test_strong_password_default($) {
my ($cmd) = @_;
my $prog = "\`@{$cmd}\`";
@@ -119,58 +99,45 @@ sub test_valid_number_default($) {
my $auto_cmd = Test::Command->new(cmd => ['env', 'PWC_FROM_STDIN=', @{$cmd}]);
$auto_cmd->exit_is_num(0, "$prog exited with code 0");
$auto_cmd->stdout_is_eq(
- "true\nfalse\nfalse\nfalse\ntrue\ntrue\ntrue\n",
+ "5\n3\n0\n1\n2\n",
"$prog produced the correct output in autotest mode",
);
}
-sub test_valid_number_count() {
- scalar @TEST_NUMBERS
+sub test_strong_password_count() {
+ scalar @TEST_PASSWORDS
}
-sub test_valid_number($ $) {
+sub test_strong_password($ $) {
my ($cmd, $idx) = @_;
- my $prog = "\`@{$cmd}\`";
+ my ($password, $expected) = @{$TEST_PASSWORDS[$idx]};
- plan tests => 2;
+ test_run_program $cmd, $password, $expected;
+}
- my ($str, $expected) = @{$TEST_NUMBERS[$idx]};
+sub test_valid_number_default($) {
+ my ($cmd) = @_;
+ my $prog = "\`@{$cmd}\`";
- # OK, so Test::Command cannot handle this one; let's do it ourselves
- socketpair(my $parent_in, my $child_in, AF_UNIX, SOCK_STREAM, 0) or
- die "Could not create the stdin socket pair: $!\n";
- socketpair(my $child_out, my $parent_out, AF_UNIX, SOCK_STREAM, 0) or
- die "Could not create the stdout socket pair: $!\n";
- my $pid = fork();
- if (!defined $pid) {
- die "Could not fork for $prog: $!\n";
- } elsif ($pid == 0) {
- close $parent_in or die "Child: could not close parent_in: $!\n";
- close $parent_out or die "Child: could not close parent_out: $!\n";
- dup2(fileno $child_in, 0) or die "Child: could not dup2 child_in onto stdin: $!\n";
- dup2(fileno $child_out, 1) or die "Child: could not dup2 child_out onto stdout: $!\n";
+ plan tests => 2;
- $ENV{PWC_FROM_STDIN} = '1';
- exec { $cmd->[0] } @{$cmd};
- die "Child: could not execute $prog: $!\n";
- }
+ my $auto_cmd = Test::Command->new(cmd => ['env', 'PWC_FROM_STDIN=', @{$cmd}]);
+ $auto_cmd->exit_is_num(0, "$prog exited with code 0");
+ $auto_cmd->stdout_is_eq(
+ "true\nfalse\nfalse\nfalse\ntrue\ntrue\ntrue\n",
+ "$prog produced the correct output in autotest mode",
+ );
+}
- close $child_in or die "Parent: could not close child_in: $!\n";
- close $child_out or die "Parent: could not close child_out: $!\n";
+sub test_valid_number_count() {
+ scalar @TEST_NUMBERS
+}
- say $parent_in $str or die "Parent: could not write to the child: $!\n";
- close $parent_in or die "Parent: could not close parent_in: $!\n";
+sub test_valid_number($ $) {
+ my ($cmd, $idx) = @_;
+ my ($str, $expected) = @{$TEST_NUMBERS[$idx]};
- my $line = <$parent_out>;
- close $parent_out or die "Parent: could not close parent_out: $!\n";
- is $line, "$expected\n", "$prog produced the correct output";
- my $awaited_pid = waitpid $pid, 0;
- if (!defined $awaited_pid) {
- die "Parent: could not wait for child pid $pid: $!\n";
- } elsif ($awaited_pid != $pid) {
- die "Parent: waited for pid $pid, yet got status $? for pid $awaited_pid\n";
- }
- is $?, 0, "$prog exited with code 0";
+ test_run_program $cmd, $str, $expected;
}
1;