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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use String::Tagged;
my $str = String::Tagged->new( "Here is %s with %d" )
->apply_tag( 3, 7, tag => "value" );
my @subs = map {
[ $_->str, $_->get_tags_at( 0 ) ]
} $str->matches( qr/\S+/ );
is( \@subs,
[ [ "Here", {} ],
[ "is", { tag => "value" } ],
[ "%s", { tag => "value" } ],
[ "with", {} ],
[ "%d", {} ] ],
'Result of ->matches' );
{
my @extents = $str->match_extents( qr/\S+/ );
is( scalar @extents, 5, '->match_extents yields 5 extents' );
my $e = $extents[0];
can_ok( $e, [qw( string start length end substr )], 'First extent is right class' );
ref_is( $e->string, $str, '$e->string' );
is( $e->start, 0, '$e->start' );
is( $e->length, 4, '$e->length' );
is( $e->substr, "Here", '$e->substr' );
}
{
$str = String::Tagged->new( "1 23 456 7890" );
foreach my $e ( reverse $str->match_extents( qr/\d+/ ) ) {
$str->set_substr( $e->start, $e->length, "digits" );
}
is( "$str", "digits digits digits digits",
'match_extents in reverse can be safely used for set_substr' );
}
done_testing;
|