[t][TT #1122] Convert t/op/numbert.t to PIR, mgrimes++
[parrot.git] / t / tools / ops2pm / 10-print_module.t
blobbf610a0c9eb693cb9b8f87da9497b60cd56c7387
1 #! perl
2 # Copyright (C) 2007-2008, Parrot Foundation.
3 # $Id$
4 # 10-print_module.t
6 use strict;
7 use warnings;
9 BEGIN {
10     use FindBin qw($Bin);
11     use Cwd qw(cwd realpath);
12     realpath($Bin) =~ m{^(.*\/parrot)\/[^/]*\/[^/]*\/[^/]*$};
13     our $topdir = $1;
14     if ( defined $topdir ) {
15         print "\nOK:  Parrot top directory located\n";
16     }
17     else {
18         $topdir = realpath($Bin) . "/../../..";
19     }
20     unshift @INC, qq{$topdir/lib};
22 use Test::More tests => 42;
23 use Cwd;
24 use File::Copy;
25 use File::Temp (qw| tempdir |);
26 use IO::File;
28 use_ok('Parrot::Ops2pm');
30 use constant NUM_FILE  => "src/ops/ops.num";
31 use constant SKIP_FILE => "src/ops/ops.skip";
33 ok( chdir $main::topdir, "Positioned at top-level Parrot directory" );
35     local @ARGV = qw(
36         src/ops/core.ops
37         src/ops/bit.ops
38     );
39     my $cwd = cwd();
40     {
41         my $tdir = tempdir( CLEANUP => 1 );
42         ok( chdir $tdir, 'changed to temp directory for testing' );
43         ok( ( mkdir qq{$tdir/src} ),     "able to make tempdir/src" );
44         ok( ( mkdir qq{$tdir/src/ops} ), "able to make tempdir/src" );
45         foreach my $f (@ARGV) {
46             ok( copy( qq{$cwd/$f}, qq{$tdir/$f} ), "copied .ops file" );
47         }
48         my $num  = NUM_FILE;
49         my $skip = SKIP_FILE;
50         ok( copy( qq{$cwd/$num},  qq{$tdir/$num} ),  "copied ops.num file" );
51         ok( copy( qq{$cwd/$skip}, qq{$tdir/$skip} ), "copied ops.skip file" );
52         my @opsfiles = glob("./src/ops/*.ops");
54         my $self = Parrot::Ops2pm->new(
55             {
56                 argv    => [@opsfiles],
57                 script  => "tools/build/ops2pm.pl",
58                 nolines => undef,
59                 renum   => undef,
60                 moddir  => "lib/Parrot/OpLib",
61                 module  => "core.pm",
63                 #            inc_dir         => "include/parrot/oplib",
64                 #            inc_f           => "ops.h",
65             }
66         );
67         isa_ok( $self, q{Parrot::Ops2pm} );
69         ok( $self->prepare_ops, "prepare_ops() returned successfully" );
70         ok( defined( $self->{ops} ), "'ops' key has been defined" );
72         ok( $self->load_op_map_files(), "load_op_map_files() completed successfully" );
73         ok( -f $num,                    "ops.num located after renumbering" );
74         ok( -f $skip,                   "ops.skip located after renumbering" );
76         ok( $self->sort_ops(), "sort_ops returned successfully" );
78         ok( $self->prepare_real_ops(), "prepare_real_ops() returned successfully" );
80         ok( $self->print_module(), "print_module() returned true" );
81         ok( -f qq{$tdir/$self->{moddir}/$self->{module}}, "core.pm file written" );
83         # Todo:  test characteristics of .pm file written
85         ok( chdir $cwd, 'changed back to starting directory after testing' );
86     }
89 # --no-lines command-line option
91     local @ARGV = qw(
92         src/ops/core.ops
93         src/ops/bit.ops
94     );
95     my $cwd = cwd();
96     {
97         my $tdir = tempdir( CLEANUP => 1 );
98         ok( chdir $tdir, 'changed to temp directory for testing' );
99         ok( ( mkdir qq{$tdir/src} ),     "able to make tempdir/src" );
100         ok( ( mkdir qq{$tdir/src/ops} ), "able to make tempdir/src" );
101         foreach my $f (@ARGV) {
102             ok( copy( qq{$cwd/$f}, qq{$tdir/$f} ), "copied .ops file" );
103         }
104         my $num  = NUM_FILE;
105         my $skip = SKIP_FILE;
106         ok( copy( qq{$cwd/$num},  qq{$tdir/$num} ),  "copied ops.num file" );
107         ok( copy( qq{$cwd/$skip}, qq{$tdir/$skip} ), "copied ops.skip file" );
108         my @opsfiles = glob("./src/ops/*.ops");
110         my $self = Parrot::Ops2pm->new(
111             {
112                 argv    => [@opsfiles],
113                 script  => "tools/build/ops2pm.pl",
114                 nolines => 1,
115                 renum   => undef,
116                 moddir  => "lib/Parrot/OpLib",
117                 module  => "core.pm",
119                 #            inc_dir         => "include/parrot/oplib",
120                 #            inc_f           => "ops.h",
121             }
122         );
123         isa_ok( $self, q{Parrot::Ops2pm} );
125         ok( $self->prepare_ops, "prepare_ops() returned successfully" );
126         ok( defined( $self->{ops} ), "'ops' key has been defined" );
128         ok( $self->load_op_map_files(), "load_op_map_files() completed successfully" );
129         ok( -f $num,                    "ops.num located after renumbering" );
130         ok( -f $skip,                   "ops.skip located after renumbering" );
132         ok( $self->sort_ops(), "sort_ops returned successfully" );
134         ok( $self->prepare_real_ops(), "prepare_real_ops() returned successfully" );
136         ok( $self->print_module(), "print_module() returned true" );
137         ok( -f qq{$tdir/$self->{moddir}/$self->{module}}, "core.pm file written" );
139         my $fhin = IO::File->new();
140         ok( ( $fhin->open("<$tdir/$self->{moddir}/$self->{module}") ),
141             "Able to open file for reading" );
142         my $corepm;
143         {
144             local $/;
145             $corepm = <$fhin>;
146         }
147         ok( $fhin->close(), "Able to close file after reading" );
148         unlike( $corepm, qr/#line/, "No '#line' directives found in generated C code" );
150         # Todo:  more tests of characteristics of .pm file written
152         ok( chdir $cwd, 'changed back to starting directory after testing' );
153     }
156 pass("Completed all tests in $0");
158 ################### DOCUMENTATION ###################
160 =head1 NAME
162 10-print_module.t - test C<Parrot::Ops2pm::print_module()>
164 =head1 SYNOPSIS
166     % prove t/tools/ops2pm/10-print_module.t
168 =head1 DESCRIPTION
170 The files in this directory test the publicly callable methods of
171 F<lib/Parrot/Ops2pm.pm> and F<lib/Parrot/Ops2pm/Auxiliary.pm>.
172 By doing so, they test the functionality of the F<ops2pm.pl> utility.
173 That functionality has largely been extracted
174 into the methods of F<Utils.pm>.
176 F<10-print_module.t> tests whether
177 C<Parrot::Ops2pm::print_module()> works properly.
179 =head1 TODO
181 The following statements, branches and conditions in C<print_module()>
182 are as yet uncovered:
184 =over 4
186 =item *
188 Directory failure:  can it be provoked?
190   if ( !-d $fulldir ) {
191     File::Path::mkpath( [ $fulldir ], 0, 0755 )
192       or die "$self->{script}: Could not mkdir $fulldir: $!!\n";
193   }
195 =back
197 =head1 AUTHOR
199 James E Keenan
201 =head1 SEE ALSO
203 Parrot::Ops2pm, F<ops2pm.pl>.
205 =cut
207 # Local Variables:
208 #   mode: cperl
209 #   cperl-indent-level: 4
210 #   fill-column: 100
211 # End:
212 # vim: expandtab shiftwidth=4: