DateTime-Lite

 view release on metacpan or  search on metacpan

t/18.resolve_abbreviation_ordering.t  view on Meta::CPAN

{
    my $results = DateTime::Lite::TimeZone->resolve_abbreviation( 'WET' );
    SKIP:
    {
        if( !ok( defined( $results ), 'WET resolved' ) )
        {
            skip( 'WET resolution failed', 3 );
        }

        my @active   = grep{ $_->{is_active} } @$results;
        my @inactive = grep{ !$_->{is_active} } @$results;

        ok( scalar( @active ) > 0,   'At least one still-active WET zone' );
        ok( scalar( @inactive ) > 0, 'At least one WET zone marked inactive' );

        # The last active must come before the first inactive.
        if( @active && @inactive )
        {
            my $last_active_idx  = -1;
            my $first_inactive_idx = scalar( @$results );
            for my $i ( 0 .. $#{ $results } )
            {
                $last_active_idx    = $i if( $results->[ $i ]->{is_active} );
                $first_inactive_idx = $i if( !$results->[ $i ]->{is_active}
                                          && $first_inactive_idx == scalar( @$results ) );
            }
            cmp_ok( $last_active_idx, '<', $first_inactive_idx,
                'All active WET zones appear before any inactive WET zone' );
        }
        else
        {
            pass( 'No mixed active/inactive case to check' );
        }
    };
};

# NOTE: EEST - Eastern European Summer Time.
#       Among still-active zones, Asia/Beirut adopted EEST earliest (1920).
#       v0.6.2 returned Asia/Gaza first because of MAX(trans_time).
subtest 'EEST puts earliest still-active adopter first' => sub
{
    my $results = DateTime::Lite::TimeZone->resolve_abbreviation( 'EEST' );
    SKIP:
    {
        if( !ok( defined( $results ), 'EEST resolved' ) )
        {
            skip( 'EEST resolution failed', 2 );
        }
        is( $results->[0]->{zone_name}, 'Asia/Beirut',
            'Asia/Beirut is first for EEST (earliest still-active adopter)' );
        is( $results->[0]->{is_active}, 1,
            'Asia/Beirut is_active=1 for EEST' );
    };
};

# NOTE: is_active regex correctness - the word-boundary must not trigger
#       false positives. EST must NOT match because of the EEST substring,
#       and CST must NOT match because of the CEST substring.
subtest 'is_active regex avoids substring false positives' => sub
{
    # A zone that has CET and CEST but not CST in its footer
    # (e.g. Europe/Paris: "CET-1CEST,M3.5.0,M10.5.0/3")
    # should have is_active=1 for CEST but is_active=0 for any substring
    # that isn't a proper token of the footer.
    #
    # We verify indirectly: for CEST we expect most Central European zones
    # to be is_active=1; for a nonsense abbreviation like "ZZZ" we expect
    # no results at all (method returns undef).
    my $cest = DateTime::Lite::TimeZone->resolve_abbreviation( 'CEST' );
    ok( defined( $cest ), 'CEST resolved' );
    my @active = grep{ $_->{is_active} } @$cest;
    ok( scalar( @active ) >= 10,
        'CEST has at least 10 still-active zones (Europe + pockets)' );

    # Non-existent abbreviation: no match at all.
    my $zzz = DateTime::Lite::TimeZone->resolve_abbreviation( 'ZZZ' );
    ok( !defined( $zzz ), 'ZZZ returns undef (no match)' );
};

# NOTE: Numeric abbreviations must also work. "-03" is a real abbreviation
#       for several South American zones that wraps as "<-03>" in the
#       POSIX footer.
subtest 'Numeric abbreviations (-03) are handled correctly' => sub
{
    my $results = DateTime::Lite::TimeZone->resolve_abbreviation( '-03' );
    SKIP:
    {
        if( !ok( defined( $results ), '-03 resolved' ) )
        {
            skip( '-03 resolution failed', 2 );
        }
        ok( scalar( @$results ) > 5, 'Multiple zones on -03' );

        # At least one zone (e.g. America/Sao_Paulo) must be is_active=1 for -03,
        # since their footer is "<-03>3".
        my @active = grep{ $_->{is_active} } @$results;
        ok( scalar( @active ) > 0,
            'At least one -03 zone is still active (footer contains <-03>)' );
    };
};

done_testing;

__END__



( run in 1.465 second using v1.01-cache-2.11-cpan-98e64b0badf )