File: Catmandu-Addable.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 (76 lines) | stat: -rw-r--r-- 1,499 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env perl

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

my $pkg;

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

my $data = [];

{

    package T::AddableWithoutAdd;
    use Moo;

    package T::Addable;
    use Moo;
    with $pkg;

    sub add {
        push @$data, $_[1];
    }

    package T::WithoutGenerator;
    use Moo;

    package T::WithGenerator;
    use Moo;

    sub generator {
        sub { }
    }
}

throws_ok {Role::Tiny->apply_role_to_package('T::AddableWithoutAdd', $pkg)}
qr/missing add/;

my $a = T::Addable->new;
can_ok $a, 'add_many';

is_deeply $a->add({a => 'pony'}), {a => 'pony'}, 'add returns data added';

$data = [];
$a->add(undef);
is_deeply $data, [], 'undef gets rejected';

lives_ok {$a->add_many({})} 'add_many takes a single hash ref';
lives_ok {$a->add_many([])} 'add_many takes an array ref';
lives_ok {
    $a->add_many(sub { })
}
'add_many takes a generator code ref';
lives_ok {$a->add_many(T::WithGenerator->new)}
'add_many takes an object with a generator method';
throws_ok {$a->add_many(T::WithoutGenerator->new)}
qr/should be able to generator/;

$data = [];
is $a->add_many([1, 2, 3]), 3, 'add_many returns count of data added';
is_deeply $data, [1, 2, 3], 'add_many passes all data to add';

require Catmandu::ArrayIterator;
$data = [];
$a    = T::Addable->new;
Catmandu::ArrayIterator->new([0, 1])->add_to($a);
is_deeply $data, [0, 1], 'add_to on Iterable';

done_testing;