Permalink
Browse files

Factor out the test file code into a module

  • Loading branch information...
1 parent 05f2a2c commit b674b5ad39602973acc3a65fcd3fcff86737f0ce @flussence flussence committed Feb 25, 2011
Showing with 176 additions and 187 deletions.
  1. +20 −0 lib/TestFiles.pm
  2. +21 −14 t/37000.t
  3. +10 −9 t/39548.t
  4. +3 −5 t/Jacobson.t
  5. +4 −2 t/belg4mit.t
  6. +4 −3 t/dandv.t
  7. +4 −16 t/expand.t
  8. +4 −16 t/fill.t
  9. +37 −0 t/overflow.t
  10. +20 −33 t/sep.t
  11. +20 −34 t/sep2.t
  12. +4 −16 t/unexpand.t
  13. +25 −39 t/wrap.t
View
20 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);
+ }
+}
View
35 t/37000.t
@@ -1,6 +1,4 @@
#!/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 Test;
use Text::Wrap;
@@ -9,19 +7,28 @@ BEGIN {
@*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";
-my $good = "(1) Category\t(2 or greater) New Category\n";
+plan +@steps;
-$Text::Wrap::break = rx{\s};
-$toPrint = wrap('', '', $toPrint);
-is $toPrint, $good;
+my Str $current = $input;
-$Text::Wrap::break = rx{\d};
-$toPrint = wrap('', '', $toPrint);
-is $toPrint, $good;
+for @steps.kv -> $number, $regex {
+ $Text::Wrap::break = $regex;
+ $current = wrap('', '', $current);
-$Text::Wrap::break = rx{a};
-$toPrint = wrap('', '', $toPrint);
-is $toPrint, $good;
+ is $current,
+ $output,
+ "Short line \$break test ({1+$number} of {+@steps})";
+}
View
19 t/39548.t
@@ -7,18 +7,19 @@ BEGIN {
@*INC.push('lib');
}
+# original bug: https://rt.perl.org/rt3/Ticket/Display.html?id=39548
+
plan 2;
-# original bug: https://rt.perl.org/rt3/Ticket/Display.html?id=39548
-my $VAR1 = " (Karl-Bonhoeffer-Nervenklinik zwischen Hermann-Piper-Str. und U-Bahnhof) ";
-my $VAR2 = " ";
-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 $leading-indent = " (Karl-Bonhoeffer-Nervenklinik zwischen Hermann-Piper-Str. und U-Bahnhof) ";
+my $paragraph-indent = " ";
+my $main-text = "(5079,19635 5124,19634 5228,19320 5246,19244)\n";
+my $got;
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";
View
8 t/Jacobson.t
@@ -12,11 +12,9 @@ plan +@input;
$Text::Wrap::huge = 'overflow';
$Text::Wrap::columns = 9;
-$Text::Wrap::break = '(?<=[,.])';
+$Text::Wrap::break = rx{<?after <[,.]>>};
-skip_rest 'Need to translate the $break into p6-regex';
-exit;
-
-for @input -> $str {
+for @input.kv -> $num, $str {
+ todo (1+$num) => '<?after> NYI in Rakudo';
lives_ok { wrap('', '', $str) }
}
View
6 t/belg4mit.t
@@ -7,13 +7,15 @@ BEGIN {
@*INC.push('lib');
}
+# Test that columns=1 works correctly
+
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;
lives_ok {
- die;
+ die "TODO";
wrap('', '',
'H4sICNoBwDoAA3NpZwA9jbsNwDAIRHumuC4NklvXTOD0KSJEnwU8fHz4Q8M9i3sGzkS7BBrm
OkCTwsycb4S3DloZuMIYeXpLFqw5LaMhXC2ymhreVXNWMw9YGuAYdfmAbwomoPSyFJuFn2x8
View
7 t/dandv.t
@@ -7,18 +7,19 @@ BEGIN {
@*INC.push('lib');
}
+# Test that $columns set too short is expanded automatically and emits a warning
+
plan 2;
+todo 1 => 'lives_ok declares a warn() as test failure, we only want to know about fatal errors';
$Text::Wrap::columns = 4;
my $x;
-
-todo 1 => 'lives_ok declares a warn() as test failure, we only want to know about fatal errors';
lives_ok {
$x = wrap('', '123', 'some text');
}
-# Set it anyway
+# Set it anyway because of the above TODO
$x = wrap('', '123', 'some text');
is $x,
View
20 t/expand.t
@@ -1,25 +1,13 @@
#!/usr/bin/env perl6
use v6;
+use TestFiles;
use Test;
use Text::Tabs;
BEGIN {
@*INC.push('lib');
}
-my @tests = dir("$*PROGRAM_NAME.output");
-
-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;
-}
+TestFiles::run(sub ($in, $out, $filename) {
+ is expand($in.slurp), $out.slurp, $filename;
+});
View
20 t/fill.t
@@ -1,25 +1,13 @@
#!/usr/bin/env perl6
use v6;
+use TestFiles;
use Test;
use Text::Wrap;
BEGIN {
@*INC.push('lib');
}
-my @tests = dir("$*PROGRAM_NAME.output");
-
-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;
-}
+TestFiles::run(sub ($in, $out, $filename) {
+ is fill(' ' x 4, ' ', $in.slurp), $out.slurp, $filename;
+});
View
37 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>;
View
53 t/sep.t
@@ -1,44 +1,31 @@
#!/usr/bin/env perl6
use v6;
+use TestFiles;
use Test;
use Text::Wrap;
BEGIN {
@*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 = '=';
-for @tests -> $filename {
- my $in = open("$*PROGRAM_NAME.input/$filename").slurp;
- my $out = open("$*PROGRAM_NAME.output/$filename").slurp;
-
- is wrap(' ', ' ', $in),
- $out,
- "$filename (as one string)";
-
- my @in = $in.split(/\n/);
-
- # append "\n" to all entries but the last
- @in[0 ..^ @in-1] >>~=>> "\n";
-
- is wrap(' ', ' ', @in),
- $out,
- "$filename (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";
+TestFiles::run(
+ tests-per-block => 2,
+ test-block => sub ($in, $out, $filename) {
+ my $in-str = $in.slurp;
+ my $out-str = $out.slurp;
+
+ is wrap(' ', ' ', $in-str),
+ $out-str,
+ "$filename - sep.t (as one string)";
+
+ # append "\n" to all lines but the last
+ my @in = $in-str.split(/\n/);
+ @in[0 ..^ @in-1] >>~=>> "\n";
+
+ is wrap(' ', ' ', @in),
+ $out-str,
+ "$filename - sep.t (array of lines)";
+ }
+);
View
54 t/sep2.t
@@ -1,45 +1,31 @@
#!/usr/bin/env perl6
use v6;
+use TestFiles;
use Test;
use Text::Wrap;
BEGIN {
@*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 = '=';
-for @tests -> $filename {
- my $in = open("$*PROGRAM_NAME.input/$filename").slurp;
- my $out = open("$*PROGRAM_NAME.output/$filename").slurp;
-
- is wrap(' ', ' ', $in),
- $out,
- "$filename (as one string)";
-
- # Test multiple string usage
- my @in = $in.split(/\n/);
-
- # append "\n" to all entries but the last
- @in[0 ..^ @in-1] >>~=>> "\n";
-
- is wrap(' ', ' ', @in),
- $out,
- "$filename (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";
+TestFiles::run(
+ tests-per-block => 2,
+ test-block => sub ($in, $out, $filename) {
+ my $in-str = $in.slurp;
+ my $out-str = $out.slurp;
+
+ is wrap(' ', ' ', $in-str),
+ $out-str,
+ "$filename - sep.t (as one string)";
+
+ # append "\n" to all lines but the last
+ my @in = $in-str.split(/\n/);
+ @in[0 ..^ @in-1] >>~=>> "\n";
+
+ is wrap(' ', ' ', @in),
+ $out-str,
+ "$filename - sep.t (array of lines)";
+ }
+);
View
20 t/unexpand.t
@@ -1,25 +1,13 @@
#!/usr/bin/env perl6
use v6;
+use TestFiles;
use Test;
use Text::Tabs;
BEGIN {
@*INC.push('lib');
}
-my @tests = dir("$*PROGRAM_NAME.output");
-
-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 unexpand($in),
- $out,
- $filename;
-}
+TestFiles::run(sub ($in, $out, $filename) {
+ is unexpand($in.slurp), $out.slurp, $filename;
+});
View
64 t/wrap.t
@@ -1,55 +1,41 @@
#!/usr/bin/env perl6
use v6;
+use TestFiles;
use Test;
use Text::Wrap;
BEGIN {
@*INC.push('lib');
}
-my @tests = dir("$*PROGRAM_NAME.output");
-
-plan 3 + @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").lines;
- my @out = open("$*PROGRAM_NAME.output/$filename").lines;
-
- # Scan output file for formatting instructions -
- # the only one currently used is a "### break=<$regex>" line
- if @out[0] ~~ / ^'###' / {
- for @out.shift.words {
- when /'break=' (\N+)/ {
- my $regex = $0;
- $Text::Wrap::break = rx/<$regex>/;
+TestFiles::run(
+ add-to-plan => 1,
+ test-block => sub ($in, $out, $filename) {
+ my @in = $in.lines;
+ my @out = $out.lines;
+
+ # Scan output file for formatting instructions -
+ # the only one currently used is a "### break=<$regex>" line
+ if @out[0] ~~ / ^'###' / {
+ for @out.shift.words {
+ when /'break=' (\N+)/ {
+ my $regex = $0;
+ $Text::Wrap::break = rx/<$regex>/;
+ }
}
}
- }
- else {
- $Text::Wrap::break = rx{\s};
- }
-
- is wrap(' ', ' ', @in.join("\n")),
- @out.join("\n"),
- $filename;
-}
-
-# Overflow test
-$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('_');
+ else {
+ $Text::Wrap::break = rx{\s};
+ }
-is wrap('zzz', 'yyy', $tw),
- "zzz$tw";
+ is wrap(' ', ' ', @in.join("\n")),
+ @out.join("\n"),
+ "$filename - wrap.t";
+ }
+);
-# Word-wrap test
$Text::Wrap::columns = 10;
$Text::Wrap::huge = "wrap";
-
is wrap("verylongindent", "", "foo"),
- "verylongindent\nfoo";
+ "verylongindent\nfoo",
+ 'Words wrap to next line correctly when the first-line indent is bigger than $columns';

0 comments on commit b674b5a

Please sign in to comment.