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
|
#!/usr/bin/perl
use strict;
use Test::More( tests => 22 );
BEGIN {
use_ok( 'String::Escape', qw( backslash unbackslash qqbackslash unqqbackslash ) )
}
{
# Backslash escapes for newline and tab characters
my $original = "\tNow is the time\nfor all good folk\nto party.\n";
my $expected = '\\tNow is the time\\nfor all good folk\\nto party.\\n';
is( backslash( $original ) => $expected );
is( unbackslash( $expected ) => $original );
is( eval( qq{"$expected"} ) => $original );
}
{
# Backslash escapes for newline and tab characters
my $original = "\tNow is the time\nfor all good folk\nto party.\n";
my $expected = '"\\tNow is the time\\nfor all good folk\\nto party.\\n"';
is( qqbackslash( $original ) => $expected );
is( unqqbackslash( $expected ) => $original );
is( eval( $expected ) => $original );
}
{
# Handles empty strings
my $original = "";
is( backslash( $original ) => $original );
is( unbackslash( $original ) => $original );
}
{
# Handles backslashes in strings
my $original = "four \\ three";
my $expected = '"four \\\\ three"';
is( eval( $expected ) => $original );
is( qqbackslash( $original ) => $expected );
is( unqqbackslash( $expected ) => $original );
is( eval( qqbackslash( $original ) ) => $original );
}
{
# Support for octal and hex escapes
my $original = "this\tis\ta\011string\x09with some text\r\n";
my $expected = '"this\\tis\\ta\\tstring\\twith some text\\r\\n"';
is( qqbackslash( $original ) => $expected );
is( unqqbackslash( $expected ) => $original );
is( eval( $expected ) => $original );
}
{
# Handles undef
my $original = undef;
my $expected = "";
is( backslash( $original ) => $expected );
is( unbackslash( $original ) => $expected );
}
{
# Should work for high-bit characters as well
my $original = " this\nis a test. \\quote\\ endquote.";
my $expected = '" this\\nis a\xbc \\xaatest.\\xba \\\\quote\\\\ endquote."';
is( qqbackslash( $original ) => $expected );
is( unqqbackslash( $expected ) => $original );
is( unbackslash( backslash( $original ) ) => $original );
is( eval( $expected ) => $original );
}
|