File: Catmandu.t

package info (click to toggle)
libcatmandu-perl 1.1000-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,440 kB
  • sloc: perl: 15,726; makefile: 25; sh: 5
file content (109 lines) | stat: -rw-r--r-- 3,563 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Log::Any::Adapter;
use Data::Dumper;
use Cwd;
use File::Spec;

my $pkg;

BEGIN {
    $pkg = 'Catmandu';
    use_ok $pkg;
}
require_ok $pkg;

# logging
Log::Any::Adapter->set('Test');

isa_ok(Catmandu->log,          'Log::Any::Proxy',         'logger test');
isa_ok(Catmandu->log->adapter, 'Log::Any::Adapter::Test', 'logger test');

Catmandu->log->debug('debug test');
Catmandu->log->info('info test');
Catmandu->log->warn('warn test');

Catmandu->log->adapter->contains_ok(qr/debug test/, 'debug log');
Catmandu->log->adapter->contains_ok(qr/info test/,  'info log');
Catmandu->log->adapter->contains_ok(qr/warn test/,  'info log');

# default load path
ok(my $curr_path = Catmandu->default_load_path, 'get current path');

Catmandu->default_load_path('/tmp');
is(Catmandu->default_load_path, '/tmp', 'got default_load_path');

Catmandu->default_load_path($curr_path);
is(Catmandu->default_load_path, $curr_path, 'got default_load_path 2');

# load
Catmandu->load;
is(Catmandu->config->{test}, 'ok', 'load and conf test');

is(Catmandu->default_store,    'default', 'default store');
is(Catmandu->default_fixer,    'default', 'default fixer');
is(Catmandu->default_importer, 'default', 'default importer');
is(Catmandu->default_exporter, 'default', 'default exporter');

isa_ok(Catmandu->importer, 'Catmandu::Importer::YAML', 'importer test');
isa_ok(Catmandu->importer('mock'),
    'Catmandu::Importer::Mock', 'importer test');
isa_ok(Catmandu->exporter, 'Catmandu::Exporter::YAML', 'exporter test');
isa_ok(Catmandu->exporter('csv'), 'Catmandu::Exporter::CSV', 'exporter test');
isa_ok(Catmandu->store,           'Catmandu::Store::Hash',   'store test');
isa_ok(Catmandu->store('hash'),   'Catmandu::Store::Hash',   'store test');
isa_ok(Catmandu->fixer,           'Catmandu::Fix',           'fixer test');
isa_ok(Catmandu->validator('test'),
    'Catmandu::Validator::Mock', 'validator test');

# store caching
{
    my $s1 = Catmandu->store('hash');
    my $s2 = Catmandu->store('hash');
    ok($s1 == $s2);
    $s2 = Catmandu->store('hash', foo => 'bar');
    ok($s1 != $s2);
}

like(Catmandu->export_to_string({foo => 'bar'}, 'JSON'),
    qr/{"foo":"bar"}/, 'export_to_string');

my ($root_vol, $root_path, $root_file)
    = File::Spec->splitpath(File::Spec->catfile(getcwd(), 't'));
my $root = File::Spec->catfile($root_path, $root_file);

is(Catmandu->root, $root, 'root');
is_deeply(Catmandu->roots, [$root], 'roots');

is(Catmandu->default_importer_package, 'JSON', 'default_importer_package');
is(Catmandu->default_exporter_package, 'JSON', 'default_exporter_package');

my $exporter = Catmandu->exporter('Mock');
Catmandu->export({n => 1}, $exporter);
is_deeply($exporter->as_arrayref, [{n => 1}]);

# set config
Catmandu->config({test => 'reload'});
is(Catmandu->config->{test}, 'reload', 'reload config');

# define
Catmandu->define_importer(foo => JSON => line_delimited => 1);
is_deeply(Catmandu->config->{importer}{foo},
    {package => 'JSON', options => {line_delimited => 1}});

Catmandu->define_exporter(foo => JSON => line_delimited => 1);
is_deeply(Catmandu->config->{exporter}{foo},
    {package => 'JSON', options => {line_delimited => 1}});

Catmandu->define_store(foo => HASH => init_data => [{_id => 1}]);
is_deeply(Catmandu->config->{store}{foo},
    {package => 'HASH', options => {init_data => [{_id => 1}]}});

Catmandu->define_fixer(foo => ['capitalize(foo)']);
is_deeply(Catmandu->config->{fixer}{foo}, ['capitalize(foo)']);

done_testing;