Show
Ignore:
Timestamp:
06/16/08 21:25:31 (18 months ago)
Author:
mpaschal
Message:

Move basic searching tests into a test class
Move is_object(), are_object(), and table resetting code into MT::Test routines
Export is_object and are_object from MT::Test for Test::More-like behavior
BugzID: 79953

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-40/t/lib/MT/Test.pm

    r2578 r2584  
    11package MT::Test; 
     2use base qw( Exporter ); 
     3 
     4our @EXPORT = qw( is_object are_objects ); 
    25 
    36use strict; 
     
    811 
    912use Test::More; 
     13use Test::Deep qw( eq_deeply ); 
    1014 
    1115BEGIN { 
     
    4347sub import { 
    4448    my $pkg = shift; 
     49    my @to_export; 
     50    # TODO: only use these init_* calls as calls, not as import args, now that we have real functions to export. 
    4551    foreach my $opt (@_) { 
    4652        if ($opt =~ m{ \A : (.+) \z }xms) { 
     
    4854            $pkg->$command() if $pkg->can($command); 
    4955        } 
    50     } 
     56        else { 
     57            push @to_export, $opt; 
     58        } 
     59    } 
     60    # Export requested or all exportable functions. 
     61    $pkg->export_to_level(1, @to_export || qw( :DEFAULT )); 
    5162} 
    5263 
     
    926937} 
    927938 
     939sub _is_object { 
     940    my ($got, $expected, $name) = @_; 
     941 
     942    if (!defined $got) { 
     943        fail($name); 
     944        diag('    got undef, not an object'); 
     945        return; 
     946    } 
     947 
     948    if (!$got->isa(ref $expected)) { 
     949        fail($name); 
     950        diag('    got a ', ref($got), ' but expected a ', ref $expected); 
     951        return; 
     952    } 
     953 
     954    if ($got == $expected) { 
     955        fail($name); 
     956        diag('    got the exact same instance as expected, when really expected a different but equivalent object'); 
     957        return; 
     958    } 
     959 
     960    # Ignore object columns that have undefined values. 
     961    my (%got_values, %expected_values); 
     962    while (my ($field, $value) = each %{ $got->{column_values} }) { 
     963        $got_values{$field} = $value if defined $value; 
     964    } 
     965    while (my ($field, $value) = each %{ $expected->{column_values} }) { 
     966        $expected_values{$field} = $value if defined $value; 
     967    } 
     968 
     969    if (!eq_deeply(\%got_values, \%expected_values)) { 
     970        # 'Test' again so the helpful failure diagnostics are output. 
     971        is_deeply(\%got_values, \%expected_values, $name); 
     972        return; 
     973    } 
     974 
     975    return 1; 
     976} 
     977 
     978sub is_object { 
     979    my ($got, $expected, $name) = @_; 
     980    pass($name) if _is_object(@_); 
     981} 
     982 
     983sub are_objects { 
     984    my ($got, $expected, $name) = @_; 
     985 
     986    my $count = scalar @$expected; 
     987    if ($count != scalar @$got) { 
     988        fail($name); 
     989        diag('    got ', scalar(@$got), ' objects but expected ', $count); 
     990        return; 
     991    } 
     992 
     993    for my $i (0..$count-1) { 
     994        return if !_is_object($$got[$i], $$expected[$i], "$name (#$i)"); 
     995    } 
     996    pass($name); 
     997} 
     998 
     999sub reset_table_for { 
     1000    my $self = shift; 
     1001    for my $class (@_) { 
     1002        my $driver    = $class->dbi_driver; 
     1003        my $dbh       = $driver->rw_handle; 
     1004        my $ddl_class = $driver->dbd->ddl_class; 
     1005 
     1006        $dbh->do($ddl_class->drop_table_sql($class)) or die $dbh->errstr; 
     1007        $dbh->do($ddl_class->create_table_sql($class)) or die $dbh->errstr; 
     1008        $dbh->do($_) or die $dbh->errstr for $ddl_class->index_table_sql($class); 
     1009        $ddl_class->create_sequence($class);  # may do nothing         
     1010    } 
     1011} 
     1012 
    92810131;