Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added tests to S32-str/chomp.t for other newline styles (\r, \r\n), a…
…s well as for strings with less than 2 characters.
  • Loading branch information
ShimmerFairy committed Jul 5, 2011
1 parent 7a1f6e3 commit 48e879e
Showing 1 changed file with 109 additions and 1 deletion.
110 changes: 109 additions & 1 deletion S32-str/chomp.t
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 12;
plan 39;

=begin pod
Expand All @@ -17,6 +17,7 @@ Basic tests for the chomp() builtin
# 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);
Expand Down Expand Up @@ -61,6 +62,113 @@ Basic tests for the chomp() builtin
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;
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\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;
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");
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
Expand Down

0 comments on commit 48e879e

Please sign in to comment.