File: 32split.t

package info (click to toggle)
libstring-tagged-perl 0.24-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 304 kB
  • sloc: perl: 2,105; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,511 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use String::Tagged;

{
   my $str = String::Tagged->new( "A message with\nlinefeeds" );

   my @lines = $str->split( qr/\n/ );

   is( scalar @lines, 2, '->split returns 2 elements' );
   isa_ok( $lines[0], [ "String::Tagged" ], '->split returns String::Tagged instances' );

   is( [ map { $_->str } @lines ], [ "A message with", "linefeeds" ],
      '->split returns correct strings' );
}

# split preserves tags (RT100409)
{
   my $str = String::Tagged->new
      ->append       ( "one " )
      ->append_tagged( "two", tag => 1 )
      ->append       ( " three\nfour" );

   my @lines = $str->split( qr/\n/ );

   my $e = $lines[0]->get_tag_extent( index( $str->str, "two" ), "tag" );
   is( $e->start,  4, '$e->start of copied tag' );
   is( $e->length, 3, '$e->length of copied tag' );
}

# split with limit
{
   my $str = String::Tagged->new( "command with some arguments" );

   my @parts = $str->split( qr/\s+/, 2 );

   is( scalar @parts, 2, '->split with limit returns only that limit' );
   is( [ map { $_->str } @parts ], [ "command", "with some arguments" ],
      '->split with limit returns correct strings' );
}

# split with captures
{
   my $str = String::Tagged->new( "abc12def345" );

   my @parts = $str->split( qr/(\d+)/ );

   is( scalar @parts, 4, '->split with capture returns captures too' );
   is( [ map { $_->str } @parts ], [qw( abc 12 def 345 )],
      '->split with capture returns correct strings' );
}

done_testing;