Ancient
view release on metacpan or search on metacpan
t/1030-util-predicates-valid.t view on Meta::CPAN
# ============================================
# is_string
# ============================================
subtest 'is_string basic' => sub {
# Not strings (undefined or references)
ok(!is_string(undef), 'is_string: undef');
ok(!is_string([]), 'is_string: array');
ok(!is_string({}), 'is_string: hash');
ok(!is_string(sub {}), 'is_string: code');
ok(!is_string(\my $x), 'is_string: scalar ref');
# Strings (defined non-refs)
ok(is_string(''), 'is_string: empty string');
ok(is_string('hello'), 'is_string: string');
ok(is_string(0), 'is_string: 0');
ok(is_string(42), 'is_string: integer');
ok(is_string(3.14), 'is_string: float');
ok(is_string('0'), 'is_string: string 0');
};
subtest 'is_string edge cases' => sub {
# Stringified number
my $num = 42;
my $str = "$num";
ok(is_string($str), 'is_string: stringified number');
# Blessed reference is not a string
my $obj = bless {}, 'Class';
ok(!is_string($obj), 'is_string: blessed ref');
# Large number
ok(is_string(1e100), 'is_string: large number');
# Negative
ok(is_string(-42), 'is_string: negative');
};
# ============================================
# is_num
# ============================================
subtest 'is_num basic' => sub {
# Not numbers
ok(!is_num(undef), 'is_num: undef');
ok(!is_num('hello'), 'is_num: non-numeric string');
ok(!is_num(''), 'is_num: empty string');
ok(!is_num([]), 'is_num: array');
ok(!is_num({}), 'is_num: hash');
# Numbers
ok(is_num(0), 'is_num: 0');
ok(is_num(42), 'is_num: positive int');
ok(is_num(-42), 'is_num: negative int');
ok(is_num(3.14), 'is_num: float');
ok(is_num(-3.14), 'is_num: negative float');
ok(is_num(1e10), 'is_num: scientific notation');
};
subtest 'is_num edge cases' => sub {
# String that looks like number
ok(is_num('42'), 'is_num: string 42');
ok(is_num('3.14'), 'is_num: string 3.14');
ok(is_num('-5'), 'is_num: string -5');
ok(is_num('1e10'), 'is_num: string scientific');
# Special values (Perl's looks_like_number considers these numeric)
ok(is_num(0.0), 'is_num: 0.0');
ok(is_num('NaN'), 'is_num: string NaN (Perl considers numeric)');
ok(is_num('inf'), 'is_num: string inf (Perl considers numeric)');
# Whitespace (Perl's looks_like_number accepts leading/trailing spaces)
ok(is_num(' 42 '), 'is_num: number with spaces (Perl accepts)');
ok(!is_num('42abc'), 'is_num: number with trailing text');
};
# ============================================
# is_int
# ============================================
subtest 'is_int basic' => sub {
# Not integers
ok(!is_int(undef), 'is_int: undef');
ok(!is_int('hello'), 'is_int: string');
ok(!is_int(3.14), 'is_int: float');
ok(!is_int(-3.14), 'is_int: negative float');
ok(!is_int([]), 'is_int: array');
# Integers
ok(is_int(0), 'is_int: 0');
ok(is_int(42), 'is_int: positive');
ok(is_int(-42), 'is_int: negative');
ok(is_int(1000000), 'is_int: large');
};
subtest 'is_int edge cases' => sub {
# Whole number floats
ok(is_int(5.0), 'is_int: 5.0');
ok(is_int(-10.0), 'is_int: -10.0');
# Very close to integer
ok(!is_int(5.0001), 'is_int: 5.0001');
ok(!is_int(4.9999), 'is_int: 4.9999');
# String integers
ok(is_int('42'), 'is_int: string 42');
ok(is_int('-5'), 'is_int: string -5');
};
# ============================================
# is_blessed
# ============================================
subtest 'is_blessed basic' => sub {
# Not blessed
ok(!is_blessed(undef), 'is_blessed: undef');
ok(!is_blessed(42), 'is_blessed: number');
ok(!is_blessed('string'), 'is_blessed: string');
ok(!is_blessed([]), 'is_blessed: unblessed array');
ok(!is_blessed({}), 'is_blessed: unblessed hash');
ok(!is_blessed(sub {}), 'is_blessed: unblessed code');
# Blessed
my $obj = bless {}, 'MyClass';
ok(is_blessed($obj), 'is_blessed: blessed hash');
my $arr = bless [], 'MyArray';
ok(is_blessed($arr), 'is_blessed: blessed array');
my $code = bless sub {}, 'MyCode';
ok(is_blessed($code), 'is_blessed: blessed code');
};
t/1030-util-predicates-valid.t view on Meta::CPAN
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');
ok(!is_true(-0), 'is_true: -0 is false');
# Whitespace
ok(is_true(' '), 'is_true: space is true');
ok(is_true("\t"), 'is_true: tab is true');
ok(is_true("\n"), 'is_true: newline is true');
};
done_testing;
( run in 0.663 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )