Ancient
view release on metacpan or search on metacpan
t/8002-file-custom-ops.t view on Meta::CPAN
my $file = "$tmpdir/touch_test.txt";
ok(file_touch($file), 'file_touch new file returns true');
ok(file_exists($file), 'touched file exists');
is(file_size($file), 0, 'touched file is empty');
};
# ============================================
# Test path manipulation
# ============================================
subtest 'file_basename' => sub {
is(file_basename('/path/to/file.txt'), 'file.txt', 'basename extracts filename');
is(file_basename('/path/to/dir/'), 'dir', 'basename handles trailing slash');
is(file_basename('filename.txt'), 'filename.txt', 'basename with no path');
};
subtest 'file_dirname' => sub {
is(file_dirname('/path/to/file.txt'), '/path/to', 'dirname extracts directory');
is(file_dirname('file.txt'), '.', 'dirname defaults to .');
is(file_dirname('/file.txt'), '/', 'dirname of root-level file');
};
subtest 'file_extname' => sub {
is(file_extname('/path/to/file.txt'), '.txt', 'extname extracts extension');
is(file_extname('file.tar.gz'), '.gz', 'extname gets last extension');
is(file_extname('noext'), '', 'extname with no extension');
};
# ============================================
# Test custom ops vs method calls
# ============================================
subtest 'custom ops match method calls' => sub {
my $file = "$tmpdir/compare.txt";
my $content = "comparison test\nline 2";
# Using method calls
file::spew($file, $content);
my $method_slurp = file::slurp($file);
my $method_exists = file::exists($file);
my $method_size = file::size($file);
my $method_is_file = file::is_file($file);
my $method_basename = file::basename($file);
# Using custom ops
my $op_slurp = file_slurp($file);
my $op_exists = file_exists($file);
my $op_size = file_size($file);
my $op_is_file = file_is_file($file);
my $op_basename = file_basename($file);
is($op_slurp, $method_slurp, 'slurp results match');
is($op_exists, $method_exists, 'exists results match');
is($op_size, $method_size, 'size results match');
is($op_is_file, $method_is_file, 'is_file results match');
is($op_basename, $method_basename, 'basename results match');
};
# ============================================
# Performance sanity check (just verify they work, not actual speed)
# ============================================
subtest 'custom ops in loops' => sub {
my $file = "$tmpdir/loop_test.txt";
file_spew($file, "loop content");
# Verify custom ops work in tight loops
my $count = 0;
for (1..100) {
if (file_exists($file)) {
$count++;
}
}
is($count, 100, 'custom ops work in loops');
# Multiple reads
my @contents;
for (1..10) {
push @contents, file_slurp($file);
}
is(scalar(@contents), 10, 'multiple slurps work');
ok((grep { $_ eq 'loop content' } @contents) == 10, 'all slurps correct');
};
# ============================================
# Edge cases
# ============================================
subtest 'empty file handling' => sub {
my $file = "$tmpdir/empty_custom.txt";
file_spew($file, "");
is(file_slurp($file), "", 'slurp empty file');
is(file_size($file), 0, 'size of empty file');
my $lines = file_lines($file);
is(scalar(@$lines), 0, 'lines of empty file');
};
subtest 'binary data' => sub {
my $file = "$tmpdir/binary_custom.dat";
my $binary = join('', map { chr($_) } 0..255);
file_spew($file, $binary);
my $read = file_slurp($file);
is(length($read), 256, 'binary data length');
is($read, $binary, 'binary data content');
};
subtest 'special characters in content' => sub {
my $file = "$tmpdir/special_custom.txt";
my $content = "line with \t tab\nline with \0 null\n\$var \@array %hash";
file_spew($file, $content);
my $read = file_slurp($file);
is($read, $content, 'special characters preserved');
};
( run in 0.930 second using v1.01-cache-2.11-cpan-13bb782fe5a )