Ancient
view release on metacpan or search on metacpan
t/1034-util-functional-valid.t view on Meta::CPAN
# ============================================
# dig - safe hash traversal
# ============================================
subtest 'dig basic' => sub {
my $data = {
a => {
b => {
c => 42
}
}
};
is(dig($data, 'a', 'b', 'c'), 42, 'dig: nested access');
is(dig($data, 'a', 'b'), $data->{a}{b}, 'dig: partial path');
is(dig($data, 'a'), $data->{a}, 'dig: single key');
};
subtest 'dig missing keys' => sub {
my $data = {a => {b => 1}};
is(dig($data, 'x'), undef, 'dig: missing top-level');
is(dig($data, 'a', 'x'), undef, 'dig: missing nested');
is(dig($data, 'a', 'b', 'c'), undef, 'dig: path too deep');
};
subtest 'dig edge cases' => sub {
# Undef at some level
my $with_undef = {a => {b => undef}};
is(dig($with_undef, 'a', 'b'), undef, 'dig: undef value');
is(dig($with_undef, 'a', 'b', 'c'), undef, 'dig: path through undef');
# Deeply nested
my $deep = {a => {b => {c => {d => {e => 'deep'}}}}};
is(dig($deep, 'a', 'b', 'c', 'd', 'e'), 'deep', 'dig: deeply nested');
};
# ============================================
# tap - side effects
# ============================================
subtest 'tap basic' => sub {
my $captured;
my $result = tap(sub { $captured = $_[0] }, 42);
is($result, 42, 'tap: returns original value');
is($captured, 42, 'tap: side effect executed');
};
subtest 'tap with $_ ' => sub {
my $captured;
my $result = tap(sub { $captured = $_ }, 'hello');
is($result, 'hello', 'tap: returns string');
is($captured, 'hello', 'tap: $_ set correctly');
};
subtest 'tap edge cases' => sub {
# With undef
my $captured = 'not_set';
my $result = tap(sub { $captured = $_[0] // 'was_undef' }, undef);
is($result, undef, 'tap: returns undef');
is($captured, 'was_undef', 'tap: captured undef');
# With reference
my $arr = [1, 2, 3];
$result = tap(sub { push @{$_[0]}, 4 }, $arr);
is_deeply($result, [1, 2, 3, 4], 'tap: modified ref returned');
};
# ============================================
# nvl - null value logic
# ============================================
subtest 'nvl basic' => sub {
is(nvl(42, 0), 42, 'nvl: defined value');
is(nvl(0, 42), 0, 'nvl: zero is defined');
is(nvl('', 42), '', 'nvl: empty string is defined');
is(nvl(undef, 42), 42, 'nvl: undef uses default');
};
subtest 'nvl edge cases' => sub {
is(nvl(undef, undef), undef, 'nvl: both undef');
is(nvl(undef, 0), 0, 'nvl: default is zero');
is(nvl(undef, ''), '', 'nvl: default is empty');
# With refs
my $default = [1, 2, 3];
is_deeply(nvl(undef, $default), $default, 'nvl: ref default');
is_deeply(nvl([], $default), [], 'nvl: defined empty array');
};
# ============================================
# coalesce - first defined
# ============================================
subtest 'coalesce basic' => sub {
is(coalesce(1, 2, 3), 1, 'coalesce: first');
is(coalesce(undef, 2, 3), 2, 'coalesce: skip first undef');
is(coalesce(undef, undef, 3), 3, 'coalesce: skip two undefs');
is(coalesce(undef, undef, undef), undef, 'coalesce: all undef');
};
subtest 'coalesce edge cases' => sub {
is(coalesce(0, 1), 0, 'coalesce: zero is defined');
is(coalesce('', 'default'), '', 'coalesce: empty is defined');
is(coalesce(undef), undef, 'coalesce: single undef');
};
# ============================================
# identity
# ============================================
subtest 'identity basic' => sub {
is(identity(42), 42, 'identity: number');
is(identity('hello'), 'hello', 'identity: string');
is(identity(undef), undef, 'identity: undef');
my $arr = [1, 2, 3];
is(identity($arr), $arr, 'identity: same ref');
( run in 1.337 second using v1.01-cache-2.11-cpan-df04353d9ac )