File: chomp.t

package info (click to toggle)
libperl6-slurp-perl 0.051000-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 144 kB
  • sloc: perl: 145; makefile: 2
file content (132 lines) | stat: -rwxr-xr-x 4,562 bytes parent folder | download | duplicates (4)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use Test::More "no_plan";
BEGIN {use_ok(Perl6::Slurp)};

my $desc;
sub TEST { $desc = $_[0] };

my $line1 = "line 1";
my $line2 = "line 2";
my $line3 = "line 3";
my $line4 = "line 4";

my $para1 = $line1."\n".$line2."\n\n";
my $para2 = $line3."\n".$line4."\n";

my $data = $para1.$para2;

TEST "scalar slurp no chomp";
$str = slurp \$data;
is $str, $data, $desc;

TEST "list slurp no chomp";
@str = slurp \$data;
is_deeply \@str, ["$line1\n", "$line2\n", "\n", "$line3\n", "$line4\n"], $desc;

# Regular chomp

TEST "scalar slurp with chomp";
$str = slurp \$data, {chomp=>1};
is $str, $line1.$line2.$line3.$line4, $desc;

TEST "list slurp with chomp";
@str = slurp \$data, {chomp=>1};
is_deeply \@str, [$line1, $line2, "", $line3, $line4], $desc;

TEST "scalar slurp with chomp and :irs(\"\\n\")";
$str = slurp \$data, {irs=>"\n", chomp=>1};
is $str, $line1.$line2.$line3.$line4, $desc;

TEST "list slurp with chomp :irs(\"\\n\")";
@str = slurp \$data, {irs=>"\n", chomp=>1};
is_deeply \@str, [$line1, $line2, "", $line3, $line4], $desc;

TEST "scalar slurp with chomp :irs(\"\\n\\n\")";
$str = slurp \$data, {chomp=>1, irs=>"\n\n"};
is $str, $line1."\n".$line2.$line3."\n".$line4."\n", $desc;

TEST "list slurp with chomp :irs(\"\\n\\n\")";
@str = slurp \$data, {chomp=>1, irs=>"\n\n"};
is_deeply \@str, [$line1."\n".$line2, $line3."\n".$line4."\n"], $desc;

TEST "scalar slurp with chomp :irs(undef)";
$str = slurp \$data, {chomp=>1, irs=>undef};
is $str, $data, $desc;

TEST "list slurp with chomp :irs(undef)";
@str = slurp \$data, {chomp=>1, irs=>undef};
is_deeply \@str, [$data], $desc;

TEST "scalar slurp with chomp :irs('ne')";
$str = slurp \$data, {chomp=>1, irs=>'ne'};
is $str, "li 1\nli 2\n\nli 3\nli 4\n", $desc;

TEST "list slurp with chomp :irs('ne')";
@str = slurp \$data, {chomp=>1, irs=>'ne'};
is_deeply \@str, [split /ne/, $data], $desc;

TEST "scalar slurp with chomp :irs(qr/\\n+|3/)";
$str = slurp \$data, {irs=>qr/\n+|3/, chomp=>1};
is $str, "line 1line 2line line 4", $desc;

TEST "list slurp with chomp :irs(qr/\\n+|3/)";
@str = slurp \$data, {irs=>qr/\n+|3/, chomp=>1};
is_deeply \@str, ["line 1","line 2","line ","","line 4"], $desc;

# Chomp with substitution

TEST "scalar slurp with substitution chomp";
$str = slurp \$data, {chomp=>"foo"};
is $str, $line1."foo".$line2."foofoo".$line3."foo".$line4."foo", $desc;

TEST "list slurp with substitution chomp";
@str = slurp \$data, {chomp=>"foo"};
is_deeply \@str, [$line1."foo", $line2."foo", "foo", $line3."foo", $line4."foo"], $desc;

TEST "scalar slurp with substitution chomp of '1'";
$str = slurp \$data, {chomp=>"1"};
is $str, $line1."1".$line2."11".$line3."1".$line4."1", $desc;

TEST "list slurp with substitution chomp of '1'";
@str = slurp \$data, {chomp=>"1"};
is_deeply \@str, [$line1."1", $line2."1", "1", $line3."1", $line4."1"], $desc;

TEST "scalar slurp with substitution chomp and :irs(\"\\n\")";
$str = slurp \$data, {irs=>"\n", chomp=>"foo"};
is $str, $line1."foo".$line2."foofoo".$line3."foo".$line4."foo", $desc;

TEST "list slurp with substitution chomp :irs(\"\\n\")";
@str = slurp \$data, {irs=>"\n", chomp=>"foo"};
is_deeply \@str, [$line1."foo", $line2."foo", "foo", $line3."foo", $line4."foo"], $desc;

TEST "scalar slurp with substitution chomp :irs(\"\\n\\n\")";
$str = slurp \$data, {chomp=>"foo", irs=>"\n\n"};
is $str, $line1."\n".$line2."foo".$line3."\n".$line4."\n", $desc;

TEST "list slurp with substitution chomp :irs(\"\\n\\n\")";
@str = slurp \$data, {chomp=>"foo", irs=>"\n\n"};
is_deeply \@str, [$line1."\n".$line2."foo", $line3."\n".$line4."\n"], $desc;

TEST "scalar slurp with substitution chomp :irs(undef)";
$str = slurp \$data, {chomp=>"foo", irs=>undef};
is $str, $data, $desc;

TEST "list slurp with substitution chomp :irs(undef)";
@str = slurp \$data, {chomp=>"foo", irs=>undef};
is_deeply \@str, [$data], $desc;

TEST "scalar slurp with substitution chomp :irs('ne')";
$str = slurp \$data, {chomp=>"foo", irs=>'ne'};
is $str, "lifoo 1\nlifoo 2\n\nlifoo 3\nlifoo 4\n", $desc;

TEST "list slurp with substitution chomp :irs('ne')";
@str = slurp \$data, {chomp=>"foo", irs=>'ne'};
is_deeply \@str, ["lifoo", " 1\nlifoo", " 2\n\nlifoo", " 3\nlifoo", " 4\n"], $desc;

TEST "scalar slurp with substitution chomp :irs(qr/\\n+|3/)";
$str = slurp \$data, {irs=>qr/\n+|3/, chomp=>"foo"};
is $str, "line 1fooline 2fooline foofooline 4foo", $desc;

TEST "list slurp with substitution chomp :irs(qr/\\n+|3/)";
@str = slurp \$data, {irs=>qr/\n+|3/, chomp=>"foo"};
is_deeply \@str, ["line 1foo","line 2foo","line foo","foo","line 4foo"], $desc;