Ancient

 view release on metacpan or  search on metacpan

t/1023-util-callbacks.t  view on Meta::CPAN

is_deeply([util::grep_cb([], ':is_positive')], [], 'grep_cb empty list');

# ======================
# count_cb tests
# ======================
is(util::count_cb(\@nums, ':is_positive'), 4, 'count_cb :is_positive = 4');
is(util::count_cb(\@nums, ':is_negative'), 2, 'count_cb :is_negative = 2');
is(util::count_cb(\@nums, ':is_zero'), 1, 'count_cb :is_zero = 1');
is(util::count_cb(\@nums, ':is_even'), 2, 'count_cb :is_even = 2');
is(util::count_cb([], ':is_positive'), 0, 'count_cb empty list = 0');

# ======================
# Type predicate callbacks
# ======================
ok(util::any_cb(\@refs, ':is_array'), 'any_cb :is_array');
ok(util::any_cb(\@refs, ':is_hash'), 'any_cb :is_hash');
ok(util::any_cb(\@refs, ':is_code'), 'any_cb :is_code');
ok(util::any_cb(\@refs, ':is_ref'), 'any_cb :is_ref');

is(util::count_cb(\@refs, ':is_array'), 1, 'count_cb :is_array = 1');
is(util::count_cb(\@refs, ':is_hash'), 1, 'count_cb :is_hash = 1');
is(util::count_cb(\@refs, ':is_code'), 1, 'count_cb :is_code = 1');
is(util::count_cb(\@refs, ':is_ref'), 4, 'count_cb :is_ref = 4');

# ======================
# Boolean predicates
# ======================
is(util::count_cb(\@mixed, ':is_true'), 3, 'count_cb :is_true = 3');
is(util::count_cb(\@mixed, ':is_false'), 3, 'count_cb :is_false = 3');
is(util::count_cb(\@mixed, ':is_defined'), 5, 'count_cb :is_defined = 5');

# ======================
# Empty checks
# ======================
my @empties = ('', [], {}, undef, 'x', [1], {a=>1});
is(util::count_cb(\@empties, ':is_empty'), 4, 'count_cb :is_empty = 4');
is(util::count_cb(\@empties, ':is_nonempty'), 3, 'count_cb :is_nonempty = 3');

# ======================
# Custom Perl callback registration
# ======================
util::register_callback('divisible_by_3', sub { $_[0] % 3 == 0 });
ok(util::has_callback('divisible_by_3'), 'custom callback registered');
is(util::count_cb([1..10], 'divisible_by_3'), 3, 'custom callback works');
is_deeply([util::grep_cb([1..10], 'divisible_by_3')], [3, 6, 9], 'grep_cb with custom callback');

# ======================
# list_callbacks
# ======================
my $callbacks = util::list_callbacks();
ok(ref $callbacks eq 'ARRAY', 'list_callbacks returns arrayref');
ok(grep({ $_ eq ':is_positive' } @$callbacks), 'list_callbacks includes :is_positive');
ok(grep({ $_ eq 'divisible_by_3' } @$callbacks), 'list_callbacks includes custom');

# ======================
# Error handling
# ======================
eval { util::any_cb(\@nums, 'nonexistent') };
like($@, qr/unknown callback/, 'unknown callback croaks');

eval { util::any_cb('not_array', ':is_positive') };
like($@, qr/arrayref/, 'non-arrayref croaks');

eval { util::register_callback(':is_positive', sub {}) };
like($@, qr/already registered/, 'cannot re-register');

done_testing();



( run in 2.040 seconds using v1.01-cache-2.11-cpan-df04353d9ac )