forked from evalEmpire/perl5i
/
wrap.t
41 lines (31 loc) · 1015 Bytes
/
wrap.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl
use perl5i::latest;
use Test::More;
foreach my $word ("hello", "goodbye!") {
foreach my $separator ("\n", '!!', '@') {
test_wrap($word, $separator);
}
};
done_testing();
sub test_wrap {
my ($word, $sep) = @_;
my $txt = ($word . ' ') x 14;
is num_lines($txt), 1, "Unmodified string is one line long";
is num_lines($txt->wrap(separator => $sep), $sep), 2, "Default wrapping gives two lines";
is num_lines(
$txt->wrap(width => length($word) + 1, separator => $sep),
$sep
), 14, "One word per line";
is num_lines(
$txt->wrap(width => length($txt) + 1, separator => $sep),
$sep
), 1, "Excessive wrap length";
is $txt->wrap(width => 0, separator => $sep), $txt, "Zero wrap length";
is $txt->wrap(width => -1, separator => $sep), $txt, "Negative wrap length";
}
sub num_lines {
my ($txt, $separator) = @_;
$separator //= "\n";
my @lines = split($separator, $txt);
return scalar @lines;
}