File: Catmandu-Importer-Text.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (69 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download | duplicates (5)
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
use strict;
use warnings;
use Test::More;

use_ok 'Catmandu::Importer::Text';
require_ok 'Catmandu::Importer::Text';

my $text = <<EOF;
Roses are red,
Violets are blue,
Sugar is sweet,
And so| are you.
EOF

sub text {
    Catmandu::Importer::Text->new(file => \$text, @_)->to_array;
}

is_deeply text(),
    [
    {_id => 1, text => "Roses are red,"},
    {_id => 2, text => "Violets are blue,"},
    {_id => 3, text => "Sugar is sweet,"},
    {_id => 4, text => "And so| are you."},
    ],
    'simple text import';

is_deeply text(pattern => 'are'),
    [
    {_id => 1, text => "Roses are red,"},
    {_id => 2, text => "Violets are blue,"},
    {_id => 3, text => "And so| are you."},
    ],
    'simple pattern match';

is_deeply text(pattern => '(\w+)(.).*\.$'),
    [{_id => 1, match => ["And", " "]},], 'numbered capturing groups';

my $items = [
    {_id => 1, match => {first => "Roses",   second => "are"}},
    {_id => 2, match => {first => "Violets", second => "are"}}
];

is_deeply text(pattern => '^(?<first>\w+) (?<second>are).*\,$'), $items,
    'named capturing groups';

my $pattern = <<'PAT';
    ^(?<first>   \w+)   # first word
    \                   # space
    (?<second>   are )  # second word = 'are'
PAT

is_deeply text(pattern => $pattern), $items, 'multiline pattern';

is_deeply [map {$_->{text}} @{text(split => ' ')}],
    [map {[split ' ', $_]} split "\n", $text], 'split by character';

is_deeply [map {$_->{text}} @{text(split => '|')}],
    [map {[split '\\|', $_]} split "\n", $text],
    'split by character (no regexp)';

is_deeply [map {$_->{text}} @{text(split => 'is|are')}],
    [map {[split /is|are/, $_]} split "\n", $text], 'split by regexp';

is_deeply text(split => ' is | are ', pattern => '^And so. (.*)'),
    [{_id => 1, text => ['And so|', 'you.'], match => ['are you.']}],
    'split and pattern';

done_testing;