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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use String::Tagged;
# base case
{
my $str = String::Tagged->from_sprintf( "%s and %d", "strings", 123 );
is( $str->str, "strings and 123", 'base case' );
$str = String::Tagged->from_sprintf( "Can print literal %% mark" );
is( $str->str, "Can print literal % mark", 'literal %' );
$str = String::Tagged->from_sprintf( "%d and %s", 456, "order" );
is( $str->str, "456 and order", 'base case preserves order' );
}
# a tagged %s argument
{
my $str = String::Tagged->from_sprintf( "A %s here",
String::Tagged->new_tagged( "string", tagged => 1 ) );
is( $str->str, "A string here", 'tagged argument value' );
ok( $str->get_tag_extent( 2, "tagged" ),
'tagged argument has tag in result' );
}
# %s padding
{
is( String::Tagged->from_sprintf( "%20s", "value" )->str,
' value',
'%s padding right-aligned' );
is( String::Tagged->from_sprintf( "%-20s", "value" )->str,
'value ',
'%s padding left-aligned' );
is( String::Tagged->from_sprintf( "%5s", "long value" )->str,
'long value',
'%s padding excess' );
is( String::Tagged->from_sprintf( "%-*s", 10, "value" )->str,
'value ',
'%s padding dynamic size' );
}
# %s truncation
{
is( String::Tagged->from_sprintf( "%.3s", "value" )->str,
'val',
'%s truncation' );
is( String::Tagged->from_sprintf( "%.*s", 2, "value" )->str,
'va',
'%s truncation dynamic size' );
}
# tagged format
{
my $str = String::Tagged->from_sprintf(
String::Tagged->new_tagged( "A tagged format", tagged => 1 ) );
is( $str->str, "A tagged format", 'tagged format value' );
ok( $str->get_tag_extent( 2, "tagged" ),
'tagged format has tag in result' );
$str = String::Tagged->new_tagged( "Single %s here", span => 1 )
->sprintf( "tag" );
is( $str->str, "Single tag here", 'tagged format with conversion' );
my $e;
ok( $e = $str->get_tag_extent( 0, "span" ),
'tagged format with conversion has tag in result' ) and do {
is( $e->end, length $str, 'tag from format covers the entire result' );
};
}
done_testing;
|