Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests by removing EVAL and refactoring #659

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rewrite test to avoid use of a file handle
Do everything on a in-memory object. After all, the file content is
`slurp`ed into one anyway.
vrurg committed Jul 27, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 3ea66087e590bc9d39804784299da91da94da6da
188 changes: 87 additions & 101 deletions S26-documentation/12-non-breaking-space.t
Original file line number Diff line number Diff line change
@@ -3,27 +3,21 @@ use v6;
use MONKEY-SEE-NO-EVAL;
use Test;

my $f = './.tmp-test-file2';
#unlink $f if $f.IO.e;

my $code = gen-test($f);

say "See file '$f'";

my $code = gen-test();
EVAL $code;
#END { unlink $f }

##### subroutines #####
sub gen-test($f) {
my $fh = open $f, :w;
sub gen-test {
my $code = "";
# my $fh = open $f, :w;
#LEAVE try close $fh;

$fh.print: q:to/HERE/;
use v6;
$code ~= q:to/HERE/;
use v6;

# two-column table with various whitespace chars
=begin table
HERE
# two-column table with various whitespace chars
=begin table
HERE

# non-breaking ws chars
my @nbchars = [
@@ -64,114 +58,106 @@ sub gen-test($f) {
# make one testing row per non-breaking space char
for @nbchars -> $nbc {
# first column
my $row = "Raku";
$row ~= $nbc.chr;
$row ~= "Language";
$code ~= "Raku";
$code ~= $nbc.chr;
$code ~= "Language";
for @bchars -> $c {
$row ~= $c.chr;
$code ~= $c.chr;
}
$row ~= "is the best!";
$code ~= "is the best!";

# second column
$row ~= ' | ';
$row ~= "Raku ";
$row ~= $nbc.chr;
$row ~= " Language";
$code ~= ' | ';
$code ~= "Raku ";
$code ~= $nbc.chr;
$code ~= " Language";
for @bchars -> $c {
$row ~= $c.chr;
$code ~= $c.chr;
}
$row ~= "is the best!";

$fh.say: $row;
$code ~= "is the best!\n";
}
$fh.say: "=end table";

$fh.say;
$code ~= "=end table\n";

my $n = +@nbchars;
# the planned num of tests is 1 + $n
$fh.say: "plan 1 + $n * 2;";
$fh.say: "my \$n = $n;";
$fh.say: "my \$SPACE = ' ';";
$code ~= "plan 1 + $n * 2;\n";
$code ~= "my \$n = $n;\n";
$code ~= "my \$SPACE = ' ';\n";

$fh.print: q:to/HERE/;
my $r = $=pod[0];
is $r.contents.elems, $n, "table has $n elements (rows)";
HERE
$code ~= q:to/HERE/;
my $r = $=pod[0];
is $r.contents.elems, $n, "table has $n elements (rows)";
HERE

# create separate sub-tests for each non-breaking space char
for 0..^$n -> $i {
$fh.say;
$fh.say: '{';
$fh.say: "my \$i = $i;";
$fh.say: "my \$row = $i + 1;";
$fh.say: "my \$nbspc = \"{@nbchars[$i].chr}\";";

$fh.print: q:to/HERE/;
my $res0 = "Raku{$nbspc}Language is the best!";
my $res1 = "Raku {$nbspc} Language is the best!";
# show cell chars separated by pipes
=begin comment
my $c0 = $r.contents[$i][0].comb.join('|');
my $r0 = $res0.comb.join('|');
my $c1 = $r.contents[$i][1].comb.join('|');
my $r1 = $res1.comb.join('|');
=end comment
my $c0 = show-space-chars($r.contents[$i][0]);
my $r0 = show-space-chars($res0);
my $c1 = show-space-chars($r.contents[$i][1]);
my $r1 = show-space-chars($res1);

is $r.contents[$i][0], $res0, "row $row, col 1: '$c0' vs '$r0'";
is $r.contents[$i][1], $res1, "row $row, col 2: '$c1' vs '$r1'";

HERE

$fh.say: '}';
$code ~= "\n\{\n";
$code ~= "my \$i = $i;\n";
$code ~= "my \$row = $i + 1;\n";
$code ~= "my \$nbspc = \"{@nbchars[$i].chr}\";\n";

$code ~= q:to/HERE/;
my $res0 = "Raku{$nbspc}Language is the best!";
my $res1 = "Raku {$nbspc} Language is the best!";
# show cell chars separated by pipes
=begin comment
my $c0 = $r.contents[$i][0].comb.join('|');
my $r0 = $res0.comb.join('|');
my $c1 = $r.contents[$i][1].comb.join('|');
my $r1 = $res1.comb.join('|');
=end comment
my $c0 = show-space-chars($r.contents[$i][0]);
my $r0 = show-space-chars($res0);
my $c1 = show-space-chars($r.contents[$i][1]);
my $r1 = show-space-chars($res1);

is $r.contents[$i][0], $res0, "row $row, col 1: '$c0' vs '$r0'";
is $r.contents[$i][1], $res1, "row $row, col 2: '$c1' vs '$r1'";
HERE

$code ~= "}\n";
}

$fh.print: q:to/HERE/;
##### some subroutines for the EVALed file #####
sub int2hexstr($int, :$plain) {
# given an int, convert it to a hex string
if !$plain {
return sprintf("0x%04X", $int);
}
else {
return sprintf("%X", $int);
}
}

sub show-space-chars($str) {
# given a string with space chars, return a version with the
# hex code shown for them and place a pipe separating all
# chars in the original string
my @c = $str.comb;
my $new-str = '';
for @c -> $c {
$new-str ~= '|' if $new-str;
if $c ~~ /\s/ {
my $int = $c.ord;
my $hex-str = int2hexstr($int);
$new-str ~= $hex-str;
$code ~= q:to/HERE/;
##### some subroutines for the EVALed file #####
sub int2hexstr($int, :$plain) {
# given an int, convert it to a hex string
if !$plain {
return sprintf("0x%04X", $int);
}
else {
=begin comment
$new-str ~= $c;
=end comment
my $int = $c.ord;
my $hex-str = int2hexstr($int, :plain);
$new-str ~= $hex-str;
return sprintf("%X", $int);
}
}
return $new-str;
}
HERE

sub show-space-chars($str) {
# given a string with space chars, return a version with the
# hex code shown for them and place a pipe separating all
# chars in the original string
my @c = $str.comb;
my $new-str = '';
for @c -> $c {
$new-str ~= '|' if $new-str;
if $c ~~ /\s/ {
my $int = $c.ord;
my $hex-str = int2hexstr($int);
$new-str ~= $hex-str;
}
else {
=begin comment
$new-str ~= $c;
=end comment
my $int = $c.ord;
my $hex-str = int2hexstr($int, :plain);
$new-str ~= $hex-str;
}
}
return $new-str;
}
HERE

$fh.close;

return slurp $f;
$code
}

done-testing;
# vim: expandtab shiftwidth=4