Skip to content

Commit

Permalink
Factor out the test file code into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Parsons committed Feb 25, 2011
1 parent 05f2a2c commit b674b5a
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 187 deletions.
20 changes: 20 additions & 0 deletions lib/TestFiles.pm
@@ -0,0 +1,20 @@
module TestFiles;
use Test;

our sub run(
Callable $test-block,
Str $input-dir = "$*PROGRAM_NAME.input",
Str $output-dir = "$*PROGRAM_NAME.output",
Int $tests-per-block = 1,
Int $add-to-plan = 0
) {
my @files = dir($output-dir);
plan $tests-per-block * @files + $add-to-plan;

for @files -> $filename {
my $in = open("$input-dir/$filename");
my $out = open("$output-dir/$filename");

$test-block(:$in, :$out, :$filename);
}
}
35 changes: 21 additions & 14 deletions t/37000.t
@@ -1,6 +1,4 @@
#!/usr/bin/env perl6 #!/usr/bin/env perl6
# FIXME: Is this from RT#37000? There's an external link there that seems to be dead, but other than
# that I can't find any mention of this there
use v6; use v6;
use Test; use Test;
use Text::Wrap; use Text::Wrap;
Expand All @@ -9,19 +7,28 @@ BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


plan 3; # This test re-wraps a short string repeatedly with different settings for $break - the output
# should be the same each time
# FIXME: Is this from RT#37000? There's an external link there that seems to be dead, but other than
# that I can't find any mention of this there

my Str $input = "(1) Category\t(2 or greater) New Category\n\n";
my Str $output = "(1) Category\t(2 or greater) New Category\n";
my @steps = (
rx{\s},
rx{\d},
rx{a},
);


my $toPrint = "(1) Category\t(2 or greater) New Category\n\n"; plan +@steps;
my $good = "(1) Category\t(2 or greater) New Category\n";


$Text::Wrap::break = rx{\s}; my Str $current = $input;
$toPrint = wrap('', '', $toPrint);
is $toPrint, $good;


$Text::Wrap::break = rx{\d}; for @steps.kv -> $number, $regex {
$toPrint = wrap('', '', $toPrint); $Text::Wrap::break = $regex;
is $toPrint, $good; $current = wrap('', '', $current);


$Text::Wrap::break = rx{a}; is $current,
$toPrint = wrap('', '', $toPrint); $output,
is $toPrint, $good; "Short line \$break test ({1+$number} of {+@steps})";
}
19 changes: 10 additions & 9 deletions t/39548.t
Expand Up @@ -7,18 +7,19 @@ BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


# original bug: https://rt.perl.org/rt3/Ticket/Display.html?id=39548

plan 2; plan 2;


# original bug: https://rt.perl.org/rt3/Ticket/Display.html?id=39548 my $leading-indent = " (Karl-Bonhoeffer-Nervenklinik zwischen Hermann-Piper-Str. und U-Bahnhof) ";
my $VAR1 = " (Karl-Bonhoeffer-Nervenklinik zwischen Hermann-Piper-Str. und U-Bahnhof) "; my $paragraph-indent = " ";
my $VAR2 = " "; my $main-text = "(5079,19635 5124,19634 5228,19320 5246,19244)\n";
my $VAR3 = "(5079,19635 5124,19634 5228,19320 5246,19244)\n";
my $got;
my $answer = " (Karl-Bonhoeffer-Nervenklinik zwischen Hermann-Piper-Str. und U-Bahnhof) (
5079,19635 5124,19634 5228,19320 5246,19244)\n";


my $got;
lives_ok { lives_ok {
$got = wrap($VAR1, $VAR2, $VAR3); $got = wrap($leading-indent, $paragraph-indent, $main-text);
} }


is $got, $answer; is $got,
" (Karl-Bonhoeffer-Nervenklinik zwischen Hermann-Piper-Str. und U-Bahnhof) (\n"
~ " 5079,19635 5124,19634 5228,19320 5246,19244)\n";
8 changes: 3 additions & 5 deletions t/Jacobson.t
Expand Up @@ -12,11 +12,9 @@ plan +@input;


$Text::Wrap::huge = 'overflow'; $Text::Wrap::huge = 'overflow';
$Text::Wrap::columns = 9; $Text::Wrap::columns = 9;
$Text::Wrap::break = '(?<=[,.])'; $Text::Wrap::break = rx{<?after <[,.]>>};


skip_rest 'Need to translate the $break into p6-regex'; for @input.kv -> $num, $str {
exit; todo (1+$num) => '<?after> NYI in Rakudo';

for @input -> $str {
lives_ok { wrap('', '', $str) } lives_ok { wrap('', '', $str) }
} }
6 changes: 4 additions & 2 deletions t/belg4mit.t
Expand Up @@ -7,13 +7,15 @@ BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


# Test that columns=1 works correctly

plan 1; plan 1;
todo 1 => 'known bug - this one goes into an infinite loop'; todo 1 => 'columns=1 goes into an infinite loop';


$Text::Wrap::columns = 1; $Text::Wrap::columns = 1;


lives_ok { lives_ok {
die; die "TODO";
wrap('', '', wrap('', '',
'H4sICNoBwDoAA3NpZwA9jbsNwDAIRHumuC4NklvXTOD0KSJEnwU8fHz4Q8M9i3sGzkS7BBrm 'H4sICNoBwDoAA3NpZwA9jbsNwDAIRHumuC4NklvXTOD0KSJEnwU8fHz4Q8M9i3sGzkS7BBrm
OkCTwsycb4S3DloZuMIYeXpLFqw5LaMhXC2ymhreVXNWMw9YGuAYdfmAbwomoPSyFJuFn2x8 OkCTwsycb4S3DloZuMIYeXpLFqw5LaMhXC2ymhreVXNWMw9YGuAYdfmAbwomoPSyFJuFn2x8
Expand Down
7 changes: 4 additions & 3 deletions t/dandv.t
Expand Up @@ -7,18 +7,19 @@ BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


# Test that $columns set too short is expanded automatically and emits a warning

plan 2; plan 2;
todo 1 => 'lives_ok declares a warn() as test failure, we only want to know about fatal errors';


$Text::Wrap::columns = 4; $Text::Wrap::columns = 4;


my $x; my $x;

todo 1 => 'lives_ok declares a warn() as test failure, we only want to know about fatal errors';
lives_ok { lives_ok {
$x = wrap('', '123', 'some text'); $x = wrap('', '123', 'some text');
} }


# Set it anyway # Set it anyway because of the above TODO
$x = wrap('', '123', 'some text'); $x = wrap('', '123', 'some text');


is $x, is $x,
Expand Down
20 changes: 4 additions & 16 deletions t/expand.t
@@ -1,25 +1,13 @@
#!/usr/bin/env perl6 #!/usr/bin/env perl6
use v6; use v6;
use TestFiles;
use Test; use Test;
use Text::Tabs; use Text::Tabs;


BEGIN { BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


my @tests = dir("$*PROGRAM_NAME.output"); TestFiles::run(sub ($in, $out, $filename) {

is expand($in.slurp), $out.slurp, $filename;
plan 1 + @tests; });

is +dir("$*PROGRAM_NAME.input"),
+@tests,
'Sanity check: number of input files = output files';

for @tests -> $filename {
my $in = open("$*PROGRAM_NAME.input/$filename").slurp;
my $out = open("$*PROGRAM_NAME.output/$filename").slurp;

is expand($in),
$out,
$filename;
}
20 changes: 4 additions & 16 deletions t/fill.t
@@ -1,25 +1,13 @@
#!/usr/bin/env perl6 #!/usr/bin/env perl6
use v6; use v6;
use TestFiles;
use Test; use Test;
use Text::Wrap; use Text::Wrap;


BEGIN { BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


my @tests = dir("$*PROGRAM_NAME.output"); TestFiles::run(sub ($in, $out, $filename) {

is fill(' ' x 4, ' ', $in.slurp), $out.slurp, $filename;
plan 1 + @tests; });

is +dir("$*PROGRAM_NAME.input"),
+@tests,
'Sanity check: number of input files = output files';

for @tests -> $filename {
my $in = open("$*PROGRAM_NAME.input/$filename").slurp;
my $out = open("$*PROGRAM_NAME.output/$filename").slurp;

is fill(' ', ' ', $in),
$out,
$filename;
}
37 changes: 37 additions & 0 deletions t/overflow.t
@@ -0,0 +1,37 @@
#!/usr/bin/env perl6
use v6;
use Test;
use Text::Wrap;

BEGIN {
@*INC.push('lib');
}

plan 3;

$Text::Wrap::huge = 'overflow';
my %default = (
separator => $Text::Wrap::separator,
separator2 => $Text::Wrap::separator2,
);

my $input = <
This is a word that is too long to wrap to make sure that the program does not crash and burn
>.join('_');
my $output = "zzz$input";

is wrap('zzz', 'yyy', $input),
$output,
'Overflow handling';

$Text::Wrap::separator = '=';
is wrap('zzz', 'yyy', $input),
$output,
'Overflow handling with $separator';
$Text::Wrap::separator = %default<separator>;

$Text::Wrap::separator2 = '=';
is wrap('zzz', 'yyy', $input),
$output,
'Overflow handling with $separator2';
$Text::Wrap::separator2 = %default<separator2>;
53 changes: 20 additions & 33 deletions t/sep.t
@@ -1,44 +1,31 @@
#!/usr/bin/env perl6 #!/usr/bin/env perl6
use v6; use v6;
use TestFiles;
use Test; use Test;
use Text::Wrap; use Text::Wrap;


BEGIN { BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


my @tests = dir("$*PROGRAM_NAME.output");

plan 2 + @tests * 2;

is +dir("$*PROGRAM_NAME.input"),
+@tests,
'Sanity check: number of input files = output files';

$Text::Wrap::separator = '='; $Text::Wrap::separator = '=';


for @tests -> $filename { TestFiles::run(
my $in = open("$*PROGRAM_NAME.input/$filename").slurp; tests-per-block => 2,
my $out = open("$*PROGRAM_NAME.output/$filename").slurp; test-block => sub ($in, $out, $filename) {

my $in-str = $in.slurp;
is wrap(' ', ' ', $in), my $out-str = $out.slurp;
$out,
"$filename (as one string)"; is wrap(' ', ' ', $in-str),

$out-str,
my @in = $in.split(/\n/); "$filename - sep.t (as one string)";


# append "\n" to all entries but the last # append "\n" to all lines but the last
@in[0 ..^ @in-1] >>~=>> "\n"; my @in = $in-str.split(/\n/);

@in[0 ..^ @in-1] >>~=>> "\n";
is wrap(' ', ' ', @in),
$out, is wrap(' ', ' ', @in),
"$filename (array of lines)"; $out-str,
} "$filename - sep.t (array of lines)";

}
$Text::Wrap::huge = 'overflow'; );
my $tw = <
This is a word that is too long to wrap to make sure that the program does not crash and burn
>.join('_');

is wrap('zzz', 'yyy', $tw),
"zzz$tw";
54 changes: 20 additions & 34 deletions t/sep2.t
@@ -1,45 +1,31 @@
#!/usr/bin/env perl6 #!/usr/bin/env perl6
use v6; use v6;
use TestFiles;
use Test; use Test;
use Text::Wrap; use Text::Wrap;


BEGIN { BEGIN {
@*INC.push('lib'); @*INC.push('lib');
} }


my @tests = dir("$*PROGRAM_NAME.output");

plan 2 + @tests * 2;

is +dir("$*PROGRAM_NAME.input"),
+@tests,
'Sanity check: number of input files = output files';

$Text::Wrap::separator2 = '='; $Text::Wrap::separator2 = '=';


for @tests -> $filename { TestFiles::run(
my $in = open("$*PROGRAM_NAME.input/$filename").slurp; tests-per-block => 2,
my $out = open("$*PROGRAM_NAME.output/$filename").slurp; test-block => sub ($in, $out, $filename) {

my $in-str = $in.slurp;
is wrap(' ', ' ', $in), my $out-str = $out.slurp;
$out,
"$filename (as one string)"; is wrap(' ', ' ', $in-str),

$out-str,
# Test multiple string usage "$filename - sep.t (as one string)";
my @in = $in.split(/\n/);

# append "\n" to all lines but the last
# append "\n" to all entries but the last my @in = $in-str.split(/\n/);
@in[0 ..^ @in-1] >>~=>> "\n"; @in[0 ..^ @in-1] >>~=>> "\n";


is wrap(' ', ' ', @in), is wrap(' ', ' ', @in),
$out, $out-str,
"$filename (array of lines)"; "$filename - sep.t (array of lines)";
} }

);
$Text::Wrap::huge = 'overflow';
my $tw = <
This is a word that is too long to wrap to make sure that the program does not crash and burn
>.join('_');

is wrap('zzz', 'yyy', $tw),
"zzz$tw";

0 comments on commit b674b5a

Please sign in to comment.