File: Catmandu-Fix-import_from_string.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 (50 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Fix::import_from_string';
    use_ok $pkg;
}

is_deeply $pkg->new('record', 'JSON')
    ->fix({record => qq({"name":"Nicolas"}\n)}),
    {record => [{"name" => "Nicolas"}]},
    "convert single JSON object to array of hashes";

is_deeply $pkg->new('record', 'JSON')
    ->fix({record => qq([{"name":"Nicolas"}]\n)}),
    {record => [{"name" => "Nicolas"}]},
    "convert JSON array to array of hashes";

is_deeply $pkg->new('record', 'YAML')
    ->fix({record => qq(---\nname: Nicolas\n...\n)}),
    {record => [{"name" => "Nicolas"}]},
    "convert single YAML object to array of hashes";

is_deeply $pkg->new('record', 'YAML')
    ->fix({record => qq(---\nname: Nicolas\n...\n---\nname: Patrick\n...\n)}),
    {record => [{"name" => "Nicolas"}, {"name" => "Patrick"}]},
    "convert YAML array to array of hashes";

is_deeply $pkg->new('record', 'CSV')->fix({record => qq(name\nNicolas\n)}),
    {record => [{"name" => "Nicolas"}]},
    "convert single CSV line to array of hashes";

is_deeply $pkg->new('record', 'CSV', sep_char => ';')
    ->fix(
    {record => qq(first_name;name\nNicolas;Franck\nPatrick;Hochstenbach\n)}),
    {
    record => [
        {"first_name" => "Nicolas", name => "Franck"},
        {"first_name" => "Patrick", name => "Hochstenbach"}
    ]
    },
    "convert CSV array to array of hashes";

done_testing;