Subversion Repositories zfs_utils

Rev

Blame | Last modification | View Log | Download | RSS feed

#!/usr/bin/env perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/..";

# Test the parseSnapshotDateTime helper function directly
# This demonstrates the improved maintainability from the refactoring

print "=" x 60 . "\n";
print "Testing parseSnapshotDateTime helper function\n";
print "=" x 60 . "\n\n";

# Access the helper function (not exported, so use full package name)
require ZFS_Utils;

my @test_cases = (
    # [snapshot_name, expected_result_description]
    ['2025-12-18', 'PASS - Simple date'],
    ['2025-12-18_10:30:00', 'PASS - Date with time (colon separator)'],
    ['2025-12-18_10.30.00', 'PASS - Date with time (dot separator)'],
    ['2025-12-18T10:30:00', 'PASS - ISO 8601 format'],
    ['prefix_2025-12-18_suffix', 'PASS - Date with prefix and suffix'],
    ['daily_2025-12-18_10:30:00_backup', 'PASS - Complex name with time'],
    ['no_date_here', 'SKIP - No date in name'],
    ['2025-13-45', 'SKIP - Invalid date'],
    ['', 'SKIP - Empty string'],
);

print "Test Cases:\n";
print "-" x 60 . "\n";

my $pass_count = 0;
my $skip_count = 0;
my $fail_count = 0;

foreach my $test (@test_cases) {
    my ($snapname, $expected) = @$test;
    my $epoch = ZFS_Utils::parseSnapshotDateTime($snapname);
    
    printf "%-40s => ", $snapname || '(empty)';
    
    if (defined $epoch) {
        # Convert epoch back to readable date for verification
        my $date = scalar localtime($epoch);
        print "epoch $epoch ($date)\n";
        if ($expected =~ /^PASS/) {
            print "  ✓ $expected\n";
            $pass_count++;
        } else {
            print "  ✗ UNEXPECTED: got epoch when expecting skip\n";
            $fail_count++;
        }
    } else {
        print "undef (no date parsed)\n";
        if ($expected =~ /^SKIP/) {
            print "  ✓ $expected\n";
            $skip_count++;
        } else {
            print "  ✗ UNEXPECTED: got undef when expecting parse\n";
            $fail_count++;
        }
    }
}

print "-" x 60 . "\n";
print "\nSummary:\n";
print "  Passed: $pass_count\n";
print "  Skipped (as expected): $skip_count\n";
print "  Failed: $fail_count\n";

print "\n";
print "=" x 60 . "\n";
if ($fail_count == 0) {
    print "ALL TESTS PASSED\n";
    print "Helper function is working correctly and can be reused!\n";
    exit 0;
} else {
    print "SOME TESTS FAILED\n";
    exit 1;
}
print "=" x 60 . "\n";