Ancient

 view release on metacpan or  search on metacpan

t/1065-map-grep-context.t  view on Meta::CPAN

    is(scalar(@codes), 1, 'is_code in grep finds 1 coderef');
};

subtest 'is_defined in map/grep' => sub {
    my @defined = grep { util::is_defined($_) } @mixed;
    is(scalar(@defined), 7, 'is_defined in grep finds 7 defined values');
    
    my @results = map { util::is_defined($_) ? 1 : 0 } (1, undef, 'a', undef);
    is_deeply(\@results, [1, 0, 1, 0], 'is_defined in map');
};

subtest 'is_empty in map/grep' => sub {
    my @empties = grep { util::is_empty($_) } @strings;
    is(scalar(@empties), 1, 'is_empty in grep finds 1 empty string');
};

subtest 'is_true in map/grep' => sub {
    my @trues = grep { util::is_true($_) } @nums;
    is(scalar(@trues), 4, 'is_true in grep finds 4 true values');
};

subtest 'is_false in map/grep' => sub {
    my @falses = grep { util::is_false($_) } @nums;
    is(scalar(@falses), 1, 'is_false in grep finds 1 false value (0)');
};

subtest 'bool in map' => sub {
    my @results = map { util::bool($_) ? 1 : 0 } (0, 1, '', 'a', undef);
    is_deeply(\@results, [0, 1, 0, 1, 0], 'bool in map');
};

subtest 'is_num in map/grep' => sub {
    my @numbers = grep { util::is_num($_) } @mixed;
    is(scalar(@numbers), 1, 'is_num in grep finds 1 number');
};

subtest 'is_int in map/grep' => sub {
    my @ints = grep { util::is_int($_) } (1, 1.5, 2, 2.5, 3);
    is(scalar(@ints), 3, 'is_int in grep finds 3 integers');
};

subtest 'is_blessed in map/grep' => sub {
    my $obj = bless {}, 'Foo';
    my @items = ($obj, {}, [], 'str');
    my @blessed = grep { util::is_blessed($_) } @items;
    is(scalar(@blessed), 1, 'is_blessed in grep finds 1 blessed ref');
};

subtest 'is_scalar_ref in map/grep' => sub {
    # Note: is_scalar_ref checks SvTYPE < SVt_PVAV, which includes regex (SVt_REGEXP)
    # So both \$scalar_ref and qr/foo/ are counted as scalar refs
    my @scalars = grep { util::is_scalar_ref($_) } @mixed;
    is(scalar(@scalars), 2, 'is_scalar_ref in grep finds 2 scalar refs (includes regex)');
};

subtest 'is_regex in map/grep' => sub {
    my @regexes = grep { util::is_regex($_) } @mixed;
    is(scalar(@regexes), 1, 'is_regex in grep finds 1 regex');
};

subtest 'is_glob in map/grep' => sub {
    my @items = (*STDOUT, 'str', 1);
    my @globs = grep { util::is_glob($_) } @items;
    is(scalar(@globs), 1, 'is_glob in grep finds 1 glob');
};

# ============================================
# Numeric predicates
# ============================================
subtest 'is_positive in map/grep' => sub {
    my @pos = grep { util::is_positive($_) } @nums;
    is(scalar(@pos), 3, 'is_positive in grep finds 3 positive nums');
};

subtest 'is_negative in map/grep' => sub {
    my @neg = grep { util::is_negative($_) } @nums;
    is(scalar(@neg), 1, 'is_negative in grep finds 1 negative num');
};

subtest 'is_zero in map/grep' => sub {
    my @zeros = grep { util::is_zero($_) } @nums;
    is(scalar(@zeros), 1, 'is_zero in grep finds 1 zero');
};

subtest 'is_even in map/grep' => sub {
    my @evens = grep { util::is_even($_) } (1, 2, 3, 4, 5, 6);
    is(scalar(@evens), 3, 'is_even in grep finds 3 even nums');
};

subtest 'is_odd in map/grep' => sub {
    my @odds = grep { util::is_odd($_) } (1, 2, 3, 4, 5, 6);
    is(scalar(@odds), 3, 'is_odd in grep finds 3 odd nums');
};

# ============================================
# Two-arg string functions
# ============================================
subtest 'starts_with in map/grep' => sub {
    my @starts = grep { util::starts_with($_, 'he') } @strings;
    is(scalar(@starts), 1, 'starts_with in grep finds 1 match');
    
    my @results = map { util::starts_with($_, 'wo') ? 1 : 0 } @strings;
    is_deeply(\@results, [0, 1, 0, 0, 0], 'starts_with in map');
};

subtest 'ends_with in map/grep' => sub {
    my @ends = grep { util::ends_with($_, 'ld') } @strings;
    is(scalar(@ends), 1, 'ends_with in grep finds 1 match');
    
    my @results = map { util::ends_with($_, 'o') ? 1 : 0 } @strings;
    is_deeply(\@results, [1, 0, 0, 0, 0], 'ends_with in map');
};

# ============================================
# Collection functions
# ============================================
subtest 'array_len in map' => sub {
    my @arrays = ([1,2], [1,2,3], [], [1]);
    my @lens = map { util::array_len($_) } @arrays;
    is_deeply(\@lens, [2, 3, 0, 1], 'array_len in map');
};

subtest 'hash_size in map' => sub {
    my @hashes = ({a=>1}, {a=>1,b=>2}, {});



( run in 0.642 second using v1.01-cache-2.11-cpan-df04353d9ac )