Ancient

 view release on metacpan or  search on metacpan

t/1030-util-predicates-valid.t  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use lib 'blib/lib', 'blib/arch';

use_ok('util');
use util qw(
    is_ref is_array is_hash is_code is_defined is_string
    is_num is_int is_blessed is_scalar_ref is_regex is_glob
    is_true is_false bool
);

# ============================================
# is_ref - any reference type
# ============================================

subtest 'is_ref basic types' => sub {
    # Non-references
    ok(!is_ref(undef), 'is_ref: undef');
    ok(!is_ref(0), 'is_ref: 0');
    ok(!is_ref(1), 'is_ref: 1');
    ok(!is_ref(''), 'is_ref: empty string');
    ok(!is_ref('string'), 'is_ref: string');
    ok(!is_ref(3.14), 'is_ref: float');

    # References
    ok(is_ref([]), 'is_ref: empty array');
    ok(is_ref([1,2,3]), 'is_ref: array');
    ok(is_ref({}), 'is_ref: empty hash');
    ok(is_ref({a=>1}), 'is_ref: hash');
    ok(is_ref(sub {}), 'is_ref: code');
    ok(is_ref(\my $x), 'is_ref: scalar ref');
    ok(is_ref(qr/test/), 'is_ref: regex');
};

subtest 'is_ref edge cases' => sub {
    # Blessed references are still refs
    my $obj = bless {}, 'MyClass';
    ok(is_ref($obj), 'is_ref: blessed hash');

    my $arr_obj = bless [], 'MyArray';
    ok(is_ref($arr_obj), 'is_ref: blessed array');

    # Nested refs
    my $nested = \[1,2,3];
    ok(is_ref($nested), 'is_ref: nested ref');

    # Ref to ref
    my $val = 42;
    my $ref = \$val;
    my $refref = \$ref;
    ok(is_ref($refref), 'is_ref: ref to ref');
};

# ============================================
# is_array
# ============================================

subtest 'is_array basic' => sub {
    # Not arrays
    ok(!is_array(undef), 'is_array: undef');
    ok(!is_array(0), 'is_array: 0');
    ok(!is_array(''), 'is_array: empty string');
    ok(!is_array({}), 'is_array: hash');
    ok(!is_array(sub {}), 'is_array: code');
    ok(!is_array(\my $x), 'is_array: scalar ref');

    # Arrays
    ok(is_array([]), 'is_array: empty array');

t/1030-util-predicates-valid.t  view on Meta::CPAN


    my $code = bless sub {}, 'MyCode';
    ok(is_blessed($code), 'is_blessed: blessed code');
};

subtest 'is_blessed edge cases' => sub {
    # Blessed scalar ref
    my $val = 42;
    my $obj = bless \$val, 'ScalarClass';
    ok(is_blessed($obj), 'is_blessed: blessed scalar ref');

    # Blessed into main package
    my $main_obj = bless {}, 'main';
    ok(is_blessed($main_obj), 'is_blessed: blessed into main');
};

# ============================================
# is_scalar_ref
# ============================================

subtest 'is_scalar_ref basic' => sub {
    # Not scalar refs
    ok(!is_scalar_ref(undef), 'is_scalar_ref: undef');
    ok(!is_scalar_ref(42), 'is_scalar_ref: number');
    ok(!is_scalar_ref('string'), 'is_scalar_ref: string');
    ok(!is_scalar_ref([]), 'is_scalar_ref: array');
    ok(!is_scalar_ref({}), 'is_scalar_ref: hash');
    ok(!is_scalar_ref(sub {}), 'is_scalar_ref: code');

    # Scalar refs
    my $val = 42;
    ok(is_scalar_ref(\$val), 'is_scalar_ref: ref to number');

    my $str = 'hello';
    ok(is_scalar_ref(\$str), 'is_scalar_ref: ref to string');

    my $undef;
    ok(is_scalar_ref(\$undef), 'is_scalar_ref: ref to undef');
};

# ============================================
# is_regex
# ============================================

subtest 'is_regex basic' => sub {
    # Not regex
    ok(!is_regex(undef), 'is_regex: undef');
    ok(!is_regex(42), 'is_regex: number');
    ok(!is_regex('pattern'), 'is_regex: string pattern');
    ok(!is_regex([]), 'is_regex: array');
    ok(!is_regex({}), 'is_regex: hash');

    # Regex
    ok(is_regex(qr//), 'is_regex: empty regex');
    ok(is_regex(qr/test/), 'is_regex: simple regex');
    ok(is_regex(qr/test/i), 'is_regex: regex with flags');
    ok(is_regex(qr/^\d+$/), 'is_regex: complex regex');
};

# ============================================
# is_glob
# ============================================

subtest 'is_glob basic' => sub {
    # Not globs
    ok(!is_glob(undef), 'is_glob: undef');
    ok(!is_glob(42), 'is_glob: number');
    ok(!is_glob('*STDOUT'), 'is_glob: string');
    ok(!is_glob([]), 'is_glob: array');
    ok(!is_glob({}), 'is_glob: hash');

    # Globs
    ok(is_glob(*STDOUT), 'is_glob: STDOUT');
    ok(is_glob(*STDERR), 'is_glob: STDERR');
    ok(is_glob(*STDIN), 'is_glob: STDIN');
};

# ============================================
# is_true / is_false / bool
# ============================================

subtest 'is_true basic' => sub {
    # False values
    ok(!is_true(undef), 'is_true: undef');
    ok(!is_true(0), 'is_true: 0');
    ok(!is_true(''), 'is_true: empty string');
    ok(!is_true('0'), 'is_true: string 0');

    # True values
    ok(is_true(1), 'is_true: 1');
    ok(is_true(-1), 'is_true: -1');
    ok(is_true('1'), 'is_true: string 1');
    ok(is_true('hello'), 'is_true: string');
    ok(is_true([]), 'is_true: empty array (ref is true)');
    ok(is_true({}), 'is_true: empty hash (ref is true)');
    ok(is_true(sub {}), 'is_true: code ref');
};

subtest 'is_false basic' => sub {
    # False values
    ok(is_false(undef), 'is_false: undef');
    ok(is_false(0), 'is_false: 0');
    ok(is_false(''), 'is_false: empty string');
    ok(is_false('0'), 'is_false: string 0');

    # True values
    ok(!is_false(1), 'is_false: 1');
    ok(!is_false(-1), 'is_false: -1');
    ok(!is_false('hello'), 'is_false: string');
    ok(!is_false([]), 'is_false: array ref');
};

subtest 'bool basic' => sub {
    # Normalize to false (empty string)
    is(bool(undef), '', 'bool: undef -> empty');
    is(bool(0), '', 'bool: 0 -> empty');
    is(bool(''), '', 'bool: empty -> empty');
    is(bool('0'), '', 'bool: string 0 -> empty');

    # Normalize to true (1)
    is(bool(1), 1, 'bool: 1 -> 1');
    is(bool(-1), 1, 'bool: -1 -> 1');
    is(bool('hello'), 1, 'bool: string -> 1');
    is(bool([]), 1, 'bool: array -> 1');
    is(bool({}), 1, 'bool: hash -> 1');
};

subtest 'is_true/is_false edge cases' => sub {
    # String that looks numeric
    ok(is_true('00'), 'is_true: string 00 is true');
    ok(is_true('0.0'), 'is_true: string 0.0 is true');
    ok(is_true('0e0'), 'is_true: string 0e0 is true');

    # Numeric zero variations
    ok(!is_true(0.0), 'is_true: 0.0 is false');



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