File-Raw

 view release on metacpan or  search on metacpan

t/002-file-callbacks.t  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use File::Temp qw(tempfile tempdir);

use_ok('File::Raw');

my $tmpdir = tempdir(CLEANUP => 1);

# Create test file with various line types
my $test_file = "$tmpdir/callback_test.txt";
# File content: 9 lines (trailing newline terminates last line, doesn't add extra)
# 1: apple
# 2: banana
# 3: (empty)
# 4: cherry
# 5:    (3 spaces - whitespace only, counts as blank)
# 6: # this is a comment
# 7: date
# 8: # another comment
# 9: elderberry
File::Raw::spew($test_file, "apple\nbanana\n\ncherry\n   \n# this is a comment\ndate\n# another comment\nelderberry\n");

# ============================================
# each_line tests
# ============================================

subtest 'each_line with $_' => sub {
    my @collected;
    File::Raw::each_line($test_file, sub {
        push @collected, $_;
    });

    is(scalar(@collected), 9, 'collected all lines');
    is($collected[0], 'apple', 'first line correct');
    is($collected[2], '', 'empty line preserved');
    is($collected[5], '# this is a comment', 'comment line preserved');
};

subtest 'each_line empty file' => sub {
    my $empty = "$tmpdir/empty.txt";
    File::Raw::spew($empty, "");

    my $count = 0;
    File::Raw::each_line($empty, sub { $count++ });
    is($count, 0, 'no iterations for empty file');
};

subtest 'each_line nonexistent file' => sub {
    my @collected;
    File::Raw::each_line("$tmpdir/nonexistent.txt", sub {
        push @collected, $_;
    });
    is(scalar(@collected), 0, 'no lines from nonexistent file');
};

# ============================================
# grep_lines tests
# ============================================

subtest 'grep_lines with coderef (shift)' => sub {
    my $result = File::Raw::grep_lines($test_file, sub {
        length(shift) > 5;
    });

    is(ref($result), 'ARRAY', 'returns arrayref');
    is(scalar(@$result), 5, 'correct count of lines > 5 chars');
    ok((grep { $_ eq 'banana' } @$result), 'banana included');
    ok((grep { $_ eq 'cherry' } @$result), 'cherry included');
    ok((grep { $_ eq 'elderberry' } @$result), 'elderberry included');
};

subtest 'grep_lines with coderef (regex)' => sub {
    my $result = File::Raw::grep_lines($test_file, sub {
        shift =~ /^[aeiou]/i;  # starts with vowel
    });

    is(scalar(@$result), 2, 'two lines start with vowel');
    is($result->[0], 'apple', 'apple matches');
    is($result->[1], 'elderberry', 'elderberry matches');
};

subtest 'grep_lines with builtin is_not_blank' => sub {
    my $result = File::Raw::grep_lines($test_file, 'is_not_blank');

    # Non-blank lines: apple, banana, cherry, # comment, date, # comment, elderberry
    is(scalar(@$result), 7, 'correct non-blank count');
    ok(!(grep { $_ eq '' } @$result), 'no empty lines');
    ok(!(grep { /^\s+$/ } @$result), 'no whitespace-only lines');
};

subtest 'grep_lines with builtin not_blank' => sub {
    my $result = File::Raw::grep_lines($test_file, 'not_blank');
    is(scalar(@$result), 7, 'not_blank alias works');
};

subtest 'grep_lines with builtin is_blank' => sub {
    my $result = File::Raw::grep_lines($test_file, 'is_blank');
    # Blank = empty or whitespace-only: line 3 ("") and line 5 ("   ")
    is(scalar(@$result), 2, 'two blank lines');
};

subtest 'grep_lines with builtin is_not_empty' => sub {
    my $result = File::Raw::grep_lines($test_file, 'is_not_empty');
    # Non-empty = has at least one char (includes whitespace-only): 8 lines
    is(scalar(@$result), 8, 'correct non-empty count');



( run in 0.702 second using v1.01-cache-2.11-cpan-71847e10f99 )