-
Notifications
You must be signed in to change notification settings - Fork 135
/
chomp.t
183 lines (149 loc) · 4.73 KB
/
chomp.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use v6;
use Test;
plan 39;
=begin pod
Basic tests for the chomp() builtin
=end pod
# L<S32::Str/Str/=item chomp>
# Also see L<"http://use.perl.org/~autrijus/journal/25351">
# &chomp and &wrap are now nondestructive; chomp returns the chomped part,
# which can be defined by the filehandle that obtains the default string at
# the first place. To get destructive behaviour, use the .= form.
# testing \n newlines
{
my $foo = "foo\n";
chomp($foo);
is($foo, "foo\n", 'our variable was not yet chomped');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again with no effect');
}
{
my $foo = "foo\n\n";
$foo .= chomp;
is($foo, "foo\n", 'our variable is chomped correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again with no effect');
}
{
my $foo = "foo\nbar\n";
$foo .= chomp;
is($foo, "foo\nbar", 'our variable is chomped correctly');
$foo .= chomp;
is($foo, "foo\nbar", 'our variable is chomped again with no effect');
}
{
my $foo = "foo\n ";
$foo .= chomp;
is($foo, "foo\n ", 'our variable is chomped with no effect');
}
{
my $foo = "foo\n\n";
my $chomped = $foo.chomp;
is($foo, "foo\n\n", ".chomp has no effect on the original string");
is($chomped, "foo\n", ".chomp returns correctly chomped value");
$chomped = $chomped.chomp;
is($chomped, "foo", ".chomp returns correctly chomped value again");
}
# testing \r newlines
{
my $foo = "foo\r";
chomp($foo);
is($foo, "foo\r", 'our variable was not yet chomped');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again with no effect');
}
{
my $foo = "foo\r\r";
$foo .= chomp;
is($foo, "foo\r", 'our variable is chomped correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again with no effect');
}
{
my $foo = "foo\rbar\r";
$foo .= chomp;
is($foo, "foo\rbar", 'our variable is chomped correctly');
$foo .= chomp;
is($foo, "foo\rbar", 'our variable is chomped again with no effect');
}
{
my $foo = "foo\r ";
$foo .= chomp;
is($foo, "foo\r ", 'our variable is chomped with no effect');
}
{
my $foo = "foo\r\r";
my $chomped = $foo.chomp;
is($foo, "foo\r\r", ".chomp has no effect on the original string");
is($chomped, "foo\r", ".chomp returns correctly chomped value");
$chomped = $chomped.chomp;
is($chomped, "foo", ".chomp returns correctly chomped value again");
}
# testing \r\n newlines
{
my $foo = "foo\r\n";
chomp($foo);
is($foo, "foo\r\n", 'our variable was not yet chomped');
$foo .= chomp;
#?rakudo.jvm todo '\r\n not yet handled as grapheme'
is($foo, 'foo', 'our variable is chomped correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again with no effect');
}
#?rakudo.jvm todo '\r\n not yet handled as grapheme'
{
my $foo = "foo\r\n\r\n";
$foo .= chomp;
is($foo, "foo\r\n", 'our variable is chomped correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again correctly');
$foo .= chomp;
is($foo, 'foo', 'our variable is chomped again with no effect');
}
{
my $foo = "foo\r\nbar\r\n";
$foo .= chomp;
#?rakudo.jvm todo '\r\n not yet handled as grapheme'
is($foo, "foo\r\nbar", 'our variable is chomped correctly');
$foo .= chomp;
is($foo, "foo\r\nbar", 'our variable is chomped again with no effect');
}
{
my $foo = "foo\r\n ";
$foo .= chomp;
is($foo, "foo\r\n ", 'our variable is chomped with no effect');
}
{
my $foo = "foo\r\n\r\n";
my $chomped = $foo.chomp;
is($foo, "foo\r\n\r\n", ".chomp has no effect on the original string");
#?rakudo.jvm 2 todo '\r\n not yet handled as grapheme'
is($chomped, "foo\r\n", ".chomp returns correctly chomped value");
$chomped = $chomped.chomp;
is($chomped, "foo", ".chomp returns correctly chomped value again");
}
#testing strings with less than 2 characters
{
my $foo = "\n";
my $bar = "\r";
my $baz = "";
my $chomped = $foo.chomp;
is($chomped, "", ".chomp works on string with just a newline");
$chomped = $bar.chomp;
is($chomped, "", ".chomp works on string with just a carriage return");
$chomped = $baz.chomp;
is($chomped, "", ".chomp doesn't affect empty string");
# \r\n newlines not tested because that's never less than 2 characters.
}
=begin pod
Basic tests for the chomp() builtin working on an array of strings
=end pod
# vim: ft=perl6