Author: jkeenan
Date: Sat Jul 26 09:54:20 2008
New Revision: 29761

Modified:
   branches/parallel/config/gen/platform.pm
   branches/parallel/t/steps/gen_platform-01.t

Log:
[configure] Refactor gen::platform::runstep() into smaller chunks.  Add tests
of internal subs where possible.


Modified: branches/parallel/config/gen/platform.pm
==============================================================================
--- branches/parallel/config/gen/platform.pm    (original)
+++ branches/parallel/config/gen/platform.pm    Sat Jul 26 09:54:20 2008
@@ -32,10 +32,255 @@
 sub runstep {
     my ( $self, $conf ) = @_;
 
-    my $verbose  = $conf->options->get('verbose');
+    my $verbose     = $conf->options->get('verbose');
+    my $platform    = _get_platform( $conf, $verbose );
+    my $generated   = _get_generated($conf, $verbose);
+
+    my $coda = <<'CODA';
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */
+CODA
+
+    # headers are merged into platform.h
+#    my @headers = qw/
+#        io.h
+#        math.h
+#        misc.h
+#        dl.h
+#        signal.h
+#        stat.h
+#        threads.h
+#        string.h
+#        /;
+#
+#    my $plat_h = q{include/parrot/platform.h};
+#    $conf->append_configure_log($plat_h);
+#    open my $PLATFORM_H, ">", $plat_h
+#        or die "Can't open $plat_h: $!";
+#
+#    print {$PLATFORM_H} <<"END_HERE";
+#/* ex: set ro:
+#** !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
+#**
+#** This file is generated automatically by config/gen/platform.pm
+#*/
+##ifndef PARROT_PLATFORM_H_GUARD
+##define PARROT_PLATFORM_H_GUARD
+#
+#/*
+#** platform.h [$platform version]
+#**
+#** DO NOT EDIT THIS FILE
+#**
+#** Generated by config/gen/platform.pm
+#*/
+#
+#END_HERE
+#
+#    foreach (@headers) {
+#        my $header_file = "config/gen/platform/generic/$_";
+#        if ( -e "config/gen/platform/$platform/$_" ) {
+#            $header_file = "config/gen/platform/$platform/$_";
+#        }
+#
+#        if ( -e $header_file ) {
+#            local $/ = undef;
+#            print("\t$header_file\n") if $verbose;
+#            open my $IN_H, "<", "$header_file"
+#                or die "Can't open $header_file: $!";
+#
+#            # slurp in the header file
+#            my $in_h = <$IN_H>;
+#
+#            # remove the (in this case) superfluous coda
+#            $in_h =~ s{\Q$coda\E\n*\z}{}xgs;
+#
+#            print {$PLATFORM_H} <<"END_HERE";
+#/*
+#** $header_file:
+#*/
+##line 1 "$header_file"
+#END_HERE
+#            print {$PLATFORM_H} $in_h, "\n\n";
+#            close $IN_H;
+#        }
+#
+#        # just fall through if file is missing; means neither this platform 
nor
+#        # generic has anything to contribute for this feature.  this might not
+#        # be desirable if porters don't see the appropriate file in generic/ 
and
+#        # shoehorn their function into the wrong file rather than creating the
+#        # correct one from the above list in their $platform/ dir (e.g. 
misc.c).
+#    }
+#
+#    # finally append generated
+#    @headers = grep { /\.h$/ } split( m/,/, $generated );
+#    for (@headers) {
+#        if ( -e $_ ) {
+#            local $/ = undef;
+#            print("\t$_\n") if $verbose;
+#            open my $IN_H, "<", "$_" or die "Can't open $_: $!";
+#            print {$PLATFORM_H} <<"END_HERE";
+#/*
+#** $_
+#*/
+##line 1 "$_"
+#END_HERE
+#            print {$PLATFORM_H} <$IN_H>, "\n\n";
+#            close $IN_H;
+#        }
+#        else {
+#            warn("Header file '$_' listed in TEMP_generated but not found\n");
+#        }
+#    }
+#
+#    # Add the C-coda
+#    print $PLATFORM_H <<"END_HERE";
+##endif /* PARROT_PLATFORM_H_GUARD */
+#
+#$coda
+#END_HERE
+#
+#    close $PLATFORM_H;
+    _set_headers($conf, $verbose, $platform, $generated, $coda);
+
+    # implementation files are merged into platform.c
+#    my @impls = qw/
+#        time.c
+#        env.c
+#        dl.c
+#        stat.c
+#        math.c
+#        memalign.c
+#        signal.c
+#        itimer.c
+#        memexec.c
+#        exec.c
+#        misc.c
+#        /;
+#
+#    my $plat_c = q{src/platform.c};
+#    $conf->append_configure_log($plat_c);
+#    open my $PLATFORM_C, ">", $plat_c
+#        or die "Can't open $plat_c: $!";
+#
+#    print {$PLATFORM_C} <<"END_HERE";
+#/*
+#** platform.c [$platform version]
+#**
+#** DO NOT EDIT THIS FILE
+#**
+#** Generated by config/gen/platform.pm
+#*/
+#
+#/* HEADERIZER HFILE: none */
+#/* HEADERIZER STOP */
+#END_HERE
+#
+#    # We need to put things from begin.c before the parrot.h include.
+#    if ( -e "config/gen/platform/$platform/begin.c" ) {
+#        local $/ = undef;
+#        open my $IN_C, "<", "config/gen/platform/$platform/begin.c" or die 
"Can't open begin.c: $!";
+#
+#        # slurp in the C file
+#        my $in_c = <$IN_C>;
+#
+#        # remove the (in this case) superfluous coda
+#        $in_c =~ s{\Q$coda\E\n*\z}{}xgs;
+#
+#        print {$PLATFORM_C} <<"END_HERE";
+#/*
+#** begin.c
+#*/
+##line 1 "config/gen/platform/$platform/begin.c"
+#END_HERE
+#        print {$PLATFORM_C} $in_c, "\n\n";
+#        close $IN_C;
+#    }
+#
+#    # Copy the rest.
+#    print {$PLATFORM_C} <<'END_HERE';
+##include "parrot/parrot.h"
+#
+#END_HERE
+#
+#    for (@impls) {
+#        my $impl_file = "config/gen/platform/generic/$_";
+#        if ( -e "config/gen/platform/$platform/$_" ) {
+#            $impl_file = "config/gen/platform/$platform/$_";
+#        }
+#
+#        if ( -e $impl_file ) {
+#            local $/ = undef;
+#            print("\t$impl_file\n") if $verbose;
+#            open my $IN_C, "<", "$impl_file" or die "Can't open $impl_file: 
$!";
+#
+#            # slurp in the C file
+#            my $in_c = <$IN_C>;
+#
+#            # remove the (in this case) superfluous coda
+#            $in_c =~ s{\Q$coda\E\n*\z}{}xgs;
+#
+#            print {$PLATFORM_C} <<"END_HERE";
+#/*
+#** $impl_file:
+#*/
+##line 1 "$impl_file"
+#END_HERE
+#            print {$PLATFORM_C} $in_c, "\n\n";
+#            close $IN_C;
+#        }
+#    }
+#
+#    # append generated c files
+#    @impls = grep { /\.c$/ } split( m/,/, $generated );
+#    for (@impls) {
+#        if ( -e $_ ) {
+#            local $/ = undef;
+#            print("\t$_\n") if $verbose;
+#            open my $IN_C, "<", "$_" or die "Can't open $_: $!";
+#            print {$PLATFORM_C} <<"END_HERE";
+#/*
+#** $_:
+#*/
+##line 1 "$_"
+#END_HERE
+#            print {$PLATFORM_C} <$IN_C>, "\n\n";
+#            close $IN_C;
+#        }
+#    }
+#
+#    # append the C code coda to the generated file
+#    print {$PLATFORM_C} <<"END_HERE";
+#
+#$coda
+#END_HERE
+#
+#    close $PLATFORM_C;
+    _set_implementations($conf, $verbose, $platform, $generated, $coda);
+
+    if ( $conf->data->get('platform_asm') ) {
+        my $asm_file = "config/gen/platform/$platform/asm.s";
+        if ( -e $asm_file ) {
+            copy_if_diff( $asm_file, "src/platform_asm.s" );
+        }
+    }
+
+    # interface is the same for all platforms
+    copy_if_diff( "config/gen/platform/platform_interface.h",
+        "include/parrot/platform_interface.h" );
+
+    return 1;
+}
+
+sub _get_platform {
+    my ($conf, $verbose) = @_;
     my $platform = lc ( $conf->data->get_p5('OSNAME') );
 
-    $platform = "ansi"  if defined( $conf->options->get('miniparrot') );
+    $platform = "ansi"  if $conf->options->get('miniparrot');
     $platform = "win32" if $platform =~ /^msys/;
     $platform = "win32" if $platform =~ /^mingw/;
     $platform =~ s/^ms//;
@@ -47,22 +292,19 @@
     $platform = 'generic' unless -d "config/gen/platform/$platform";
 
     print " platform='$platform' " if $verbose;
+    return $platform;
+}
 
+sub _get_generated {
+    my ($conf, $verbose) = @_;
     my $generated = $conf->data->get('TEMP_generated');
     $generated = '' unless defined $generated;
     print " ($generated) " if $verbose;
-    print("\n") if $verbose;
-
-    my $coda = <<'CODA';
-/*
- * Local variables:
- *   c-file-style: "parrot"
- * End:
- * vim: expandtab shiftwidth=4:
- */
-CODA
+    return $generated;
+}
 
-    # headers are merged into platform.h
+sub _set_headers {
+    my ($conf, $verbose, $platform, $generated, $coda) = @_;
     my @headers = qw/
         io.h
         math.h
@@ -162,8 +404,11 @@
 END_HERE
 
     close $PLATFORM_H;
+    return 1;
+}
 
-    # implementation files are merged into platform.c
+sub _set_implementations {
+    my ($conf, $verbose, $platform, $generated, $coda) = @_;
     my @impls = qw/
         time.c
         env.c
@@ -276,18 +521,6 @@
 END_HERE
 
     close $PLATFORM_C;
-
-    if ( $conf->data->get('platform_asm') ) {
-        my $asm_file = "config/gen/platform/$platform/asm.s";
-        if ( -e $asm_file ) {
-            copy_if_diff( $asm_file, "src/platform_asm.s" );
-        }
-    }
-
-    # interface is the same for all platforms
-    copy_if_diff( "config/gen/platform/platform_interface.h",
-        "include/parrot/platform_interface.h" );
-
     return 1;
 }
 

Modified: branches/parallel/t/steps/gen_platform-01.t
==============================================================================
--- branches/parallel/t/steps/gen_platform-01.t (original)
+++ branches/parallel/t/steps/gen_platform-01.t Sat Jul 26 09:54:20 2008
@@ -5,16 +5,101 @@
 
 use strict;
 use warnings;
-use Test::More tests =>  2;
+use Test::More qw(no_plan); # tests =>  2;
 use Carp;
 use lib qw( lib );
 use_ok('config::gen::platform');
+use Parrot::Configure;
+use Parrot::Configure::Options qw( process_options );
+use Parrot::Configure::Test qw(
+    test_step_thru_runstep
+    test_step_constructor_and_description
+);
+use IO::CaptureOutput qw( capture );
+
+my $args = process_options(
+    {
+        argv => [ ],
+        mode => q{configure},
+    }
+);
+
+my $conf = Parrot::Configure->new;
+my $pkg = q{gen::platform};
+$conf->add_steps($pkg);
+$conf->options->set( %{$args} );
+my $step = test_step_constructor_and_description($conf);
+
+my $platform_orig = $conf->data->get_p5('OSNAME');
+my $archname_orig = $conf->data->get_p5('archname');
+$conf->data->set_p5( archname => 'foo-bar' );
+my $verbose = 0;
+
+$conf->options->set( miniparrot => 1 );
+is( gen::platform::_get_platform( $conf, $verbose ), q{ansi},
+    "Got expected platform for miniparrot");
+$conf->options->set( miniparrot => undef );
+
+$conf->data->set_p5( OSNAME => 'msys' );
+is( gen::platform::_get_platform( $conf, $verbose ), q{win32},
+    "Got expected platform for msys");
+
+$conf->data->set_p5( OSNAME => 'mingw' );
+is( gen::platform::_get_platform( $conf, $verbose ), q{win32},
+    "Got expected platform for mingw");
+
+$conf->data->set_p5( OSNAME => 'MSWin32' );
+is( gen::platform::_get_platform( $conf, $verbose ), q{win32},
+    "Got expected platform for MSWin32");
+
+# re-set to original values
+$conf->data->set_p5( OSNAME => $platform_orig );
+$conf->data->set_p5( archname => $archname_orig );
+
+$conf->data->set_p5( archname => 'ia64-bar' );
+is( gen::platform::_get_platform( $conf, $verbose ), q{ia64},
+    "Got expected platform for ia64");
+
+$conf->data->set_p5( archname => 'foo-bar' );
+$conf->data->set_p5( OSNAME => 'foo' );
+{
+    $verbose = 1;
+    my ($stdout, $stderr, $rv);
+    my $expected = q{generic};
+    capture(
+        sub { $rv = gen::platform::_get_platform( $conf, $verbose ) },
+        \$stdout,
+        \$stderr,
+    );
+    is( $rv, $expected, "Got expected platform for foo");
+    like( $stdout, qr/platform='$expected'/, "Got expected verbose output");
+}
+
+# re-set to original values
+$conf->data->set_p5( archname => $archname_orig );
+$conf->data->set_p5( OSNAME => $platform_orig );
+
+my $TEMP_generated_orig = $conf->data->get('TEMP_generated');
+{
+    $verbose = 1;
+    my ($stdout, $stderr, $rv);
+    my $expected = q{foo};
+    $conf->data->set( TEMP_generated => $expected );
+    capture(
+        sub { $rv = gen::platform::_get_generated( $conf, $verbose ) },
+        \$stdout,
+        \$stderr,
+    );
+    is( $rv, $expected, "Got expected generated");
+    like( $stdout, qr/\($expected\)/, "Got expected verbose output");
+}
+$conf->data->set( TEMP_generated => undef );
+$verbose = 0;
+is( gen::platform::_get_generated( $conf, $verbose ), q{},
+    "Got expected generated");
 
-=for hints_for_testing By definition, test coverage of this package will
-appear low because you can only test the OS you are currently running
-on.  Use Test::More SKIP blocks as needed.
-
-=cut
+# re-set to original values
+$conf->data->set( TEMP_generated => $TEMP_generated_orig );
 
 pass("Completed all tests in $0");
 

Reply via email to