Skip to content

Commit

Permalink
Run each test independently
Browse files Browse the repository at this point in the history
  • Loading branch information
briandfoy committed Jun 21, 2023
1 parent 9f84e08 commit 94afb94
Showing 1 changed file with 31 additions and 66 deletions.
97 changes: 31 additions & 66 deletions t/bc/bc.t
Expand Up @@ -14,30 +14,27 @@ use warnings;
use Test::More;
use File::Temp qw/tempfile/;

require './t/lib/common.pl';

# we expect to run this from the PerlPowerTools directory
my $Script = './bin/bc';
my $Script = program_name();

run_tests();

sub run_tests
{
subtest sanity => sub {
ok -e $Script, "$Script exists";
};
sub run_tests {
sanity_test($Script);

my @tables = (
operator_table(),
precedence_table(),
special_expr_table(),
statement_table(),
);
);

foreach my $table (@tables) {
run_table($table);
}
run_table($_) foreach @tables;

return;
}
}

done_testing();

Expand Down Expand Up @@ -81,8 +78,7 @@ sub operator_table {
[ '7 > 5', '1', 'greater than' ],
[ '7 >= 7', '1', 'greater than or equal' ],
];
return $table;
}
}

sub precedence_table {
my $table = [
Expand All @@ -97,8 +93,7 @@ sub precedence_table {
[ 'v = (4+5)*2', '18', 'PEDMAS' ],
[ 'v = -(4+5)+8', '-1', 'PEDMAS' ],
];
return $table;
}
}

sub special_expr_table {
my $sqrt2 = '1.4142135623731';
Expand All @@ -108,14 +103,14 @@ sub special_expr_table {
[ 'length(sqrt(2))', '14', 'significant digits' ],
[ 'scale(sqrt(2))', '13', 'precision digits' ],
];
return $table;
}
}

sub statement_table {
## no critic [ValuesAndExpressions::RequireInterpolationOfMetachars]
my $table = [
'statements',
[ q("a string"),
[
q("a string"),
'a string',
'string literal',
],
Expand All @@ -128,24 +123,27 @@ sub statement_table {
'print statement',
],

[ '{ print "a"; print "b"; print "\n" }',
[
'{ print "a"; print "b"; print "\n" }',
'ab',
'compound statement',
],

[ 'v=5; while (v--) { print v }; print "\n"',
[
'v=5; while (v--) { print v }; print "\n"',
"5\n43210",
'while statement',
],

## no critic [ValuesAndExpressions::ProhibitInterpolationOfLiterals]
[ 'for (v=0;v<5;v++) { print v }; print "\n"',
[
'for (v=0;v<5;v++) { print v }; print "\n"',
"01234",
'for statement',
],

[ 'for ( v=0; v<5; v++) { print v; if (v>2) break }; print "\n"',
"01230",
[
'for ( v=0; v<5; v++) { print v; if (v>2) break }; print "\n"',
"01230\n",
'for with break statement',
],

Expand All @@ -161,56 +159,23 @@ sub statement_table {
# Not testing this, for obvious reasons
# - quit
];
return $table;
}

sub run_table {
my ($table) = @_;
my( $table ) = @_;

my $label = shift @$table;

subtest $label => sub {

# To avoid the overhead of executing bc on every test, which
# greatly increases the runtime, we instead write a bunch
# of expressions to a tempfile and then have bc process them.
# Then we compare the output lines to the expected results
# in our table.
# Note that some expressions could generate multiple lines,
# so the expected result must account for that with newlines.

my ( $fh, $input ) = tempfile();
foreach my $tuple (@$table) {
my ( $expr, $expected, $desc ) = @$tuple;
print {$fh} "$expr\n" or die;
}
close $fh or die;

## no critic [InputOutput::ProhibitBacktickOperators]
my $output = `"$^X" $Script $input`;
my @got = split /\n/xms, $output;

# get expected results
my @expected;
my @message;
foreach my $t_ar (@$table) {
my ( $expr, $exp, $desc ) = @{$t_ar};

my @lines = split /\n/, $exp;
foreach my $ln (@lines) {
push @expected, $ln;
push @message, $desc . ' : ' . $expr;
my( $input, $expected, $description ) = @$tuple;
$expected .= "\n";
my ( $fh, $temp_filename ) = tempfile();
print {$fh} $input, "\n";
my $output = `"$^X" $Script $temp_filename`;
is $output, $expected, $description;
}
}

# This is sometimes useful, but generally not needed
# is @got, @expected, 'count of results';
};

foreach my $got (@got) {
my $exp = shift @expected;
my $msg = shift @message;
is $got, $exp, $msg;
}
};
return;
}
}

0 comments on commit 94afb94

Please sign in to comment.