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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use String::Tagged;
my $str = String::Tagged->new( "A string with BOLD and ITAL tags" );
$str->apply_tag( -1, -1, message => 1 );
$str->apply_tag( 14, 4, bold => 1 );
$str->apply_tag( 23, 4, italic => 1 );
my @tags;
undef @tags;
$str->iter_tags( sub { push @tags, [ @_ ] }, start => 20 );
is( \@tags,
[
[ 0, 32, message => 1 ],
[ 23, 4, italic => 1 ],
],
'tags list with start offset' );
undef @tags;
$str->iter_tags( sub { push @tags, [ @_ ] }, end => 20 );
is( \@tags,
[
[ 0, 32, message => 1 ],
[ 14, 4, bold => 1 ],
],
'tags list with end limit' );
undef @tags;
$str->iter_tags( sub { push @tags, [ @_ ] }, only => [qw( message )] );
is( \@tags,
[
[ 0, 32, message => 1 ],
],
'tags list with only (message)' );
undef @tags;
$str->iter_tags( sub { push @tags, [ @_ ] }, except => [qw( message )] );
is( \@tags,
[
[ 14, 4, bold => 1 ],
[ 23, 4, italic => 1 ],
],
'tags list with except (message)' );
sub fetch_tags
{
my ( $start, $len, %tags ) = @_;
push @tags, [ $start, $len, map { $_ => $tags{$_} } sort keys %tags ]
}
undef @tags;
$str->iter_tags_nooverlap( \&fetch_tags, start => 20 );
is( \@tags,
[
[ 20, 3, message => 1 ],
[ 23, 4, italic => 1, message => 1 ],
[ 27, 5, message => 1 ],
],
'tags list non-overlapping with start offset' );
undef @tags;
$str->iter_tags_nooverlap( \&fetch_tags, end => 20 );
is( \@tags,
[
[ 0, 14, message => 1 ],
[ 14, 4, bold => 1, message => 1 ],
[ 18, 2, message => 1 ],
],
'tags list non-overlapping with end limit' );
undef @tags;
$str->iter_tags_nooverlap( \&fetch_tags, only => [qw( message )] );
is( \@tags,
[
[ 0, 32, message => 1 ],
],
'tags list non-overlapping with only limit' );
undef @tags;
$str->iter_tags_nooverlap( \&fetch_tags, except => [qw( message )] );
is( \@tags,
[
[ 0, 14 ],
[ 14, 4, bold => 1 ],
[ 18, 5 ],
[ 23, 4, italic => 1 ],
[ 27, 5 ],
],
'tags list non-overlapping with except limit' );
my @substrs;
sub fetch_substrs
{
my ( $substr, %tags ) = @_;
push @substrs, [ $substr, map { $_ => $tags{$_} } sort keys %tags ]
}
undef @substrs;
$str->iter_substr_nooverlap( \&fetch_substrs, start => 20 );
is( \@substrs,
[
[ "nd ", message => 1 ],
[ "ITAL", italic => 1, message => 1 ],
[ " tags", message => 1 ],
],
'substrs non-overlapping with start offset' );
undef @substrs;
$str->iter_substr_nooverlap( \&fetch_substrs, end => 20 );
is( \@substrs,
[
[ "A string with ", message => 1 ],
[ "BOLD", bold => 1, message => 1 ],
[ " a", message => 1 ],
],
'substrs non-overlapping with start offset' );
done_testing;
|