File: mergecb.t

package info (click to toggle)
libdatetime-format-builder-perl 0.8300-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 504 kB
  • sloc: perl: 1,236; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,325 bytes parent folder | download
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
use strict;
use warnings;

use Test::More;

use DateTime::Format::Builder::Parser;

{
    my $new_sub = sub {
        my $x = shift;
        sub { $_[1] . $x }
    };
    my @cbs = ( map { $new_sub->($_) } qw( a b c d e f g ) );
    my $cb  = DateTime::Format::Builder::Parser->merge_callbacks(@cbs);
    is( $cb->( input => "x" ) => "xabcdefg", "Callback chaining works." );

    my $cbr = DateTime::Format::Builder::Parser->merge_callbacks( \@cbs );
    is(
        $cbr->( input => "x" ) => "xabcdefg",
        "Callback chaining works on ref."
    );
}

{
    my $inout = sub { $_[0] . "foo" };
    my $cb    = DateTime::Format::Builder::Parser->merge_callbacks($inout);
    is( $cb->("foo") => "foofoo", "Single callback works." );
}

{
    my $empty = DateTime::Format::Builder::Parser->merge_callbacks(undef);
    ok( !defined $empty, "Given undef, do bugger all." );

    $empty = DateTime::Format::Builder::Parser->merge_callbacks();
    ok( !defined $empty, "Given nothing, do bugger all." );

    $empty = DateTime::Format::Builder::Parser->merge_callbacks( [] );
    ok( !defined $empty, "Given empty arrayref, do bugger all." );
}

{
    my $error = eval {
        DateTime::Format::Builder::Parser->merge_callbacks( { foo => 4 } );
    };
    ok( $@, "Correctly faulted on bad arguments." );
}

done_testing();