2 # Copyright (C) 2007, Parrot Foundation.
11 use Cwd qw(cwd realpath);
12 our $topdir = realpath($Bin) . "/../..";
13 unshift @INC, qq{$topdir/lib};
15 use Test::More tests => 51;
17 use Parrot::Configure::Options qw| process_options |;
18 use Parrot::Configure::Options::Conf::CLI ();
19 use Parrot::Configure::Options::Conf::File ();
20 use Parrot::Configure::Options::Reconf ();
21 use IO::CaptureOutput qw| capture |;
24 my $badoption = q{samsonanddelilah};
27 %valid = map { $_, 1 } @Parrot::Configure::Options::Conf::CLI::valid_options;
29 ok( scalar keys %valid, "non-zero quantity of valid options found" );
30 ok( defined $valid{debugging}, "debugging option found" );
31 ok( defined $valid{maintainer}, "maintainer option found" );
32 ok( defined $valid{help}, "help option found" );
33 ok( defined $valid{version}, "version option found" );
34 ok( defined $valid{verbose}, "verbose option found" );
35 ok( !defined $valid{$badoption}, "invalid option not found" );
36 ok( !defined $valid{step}, "invalid 'step' option not found" );
37 ok( !defined $valid{target}, "invalid 'target' option not found" );
39 open my $FH, '<', "$main::topdir/Configure.pl"
40 or croak "Unable to open handle to $main::topdir/Configure.pl: $!";
46 close $FH or croak "Unable to close handle to Configure.pl: $!";
48 # Ignore any POD I have moved to an __END__ block.
49 $bigstr =~ s/__END__.*//s;
50 my ( @lines, @possible_methods );
51 @lines = grep { /^=item/ } ( split /\n/, $bigstr );
52 foreach my $l (@lines) {
54 if ( $l =~ /^=item C<--([-_\w]+)(?:[=>])/ ) {
56 push @possible_methods, $method;
60 foreach my $m (@possible_methods) {
61 unless ( defined $valid{$m} ) {
62 carp "Possibly invalid method: $m";
66 ok( !$invalid, "No invalid methods described in POD" );
68 my ($args, $step_list_ref);
69 ($args, $step_list_ref) = process_options(
75 ok( defined $args, "process_options() returned successfully" );
76 ok( $args->{debugging}, "debugging turned on by default" );
78 eval { ($args, $step_list_ref) = process_options( { argv => [] } ); };
81 qr/'mode' argument not provided to process_options\(\)/,
82 "process_options() failed due to lack of argument 'mode'"
85 eval { ($args, $step_list_ref) = process_options( { argv => [], mode => 'foobar' } ); };
88 qr/Invalid value for 'mode' argument to process_options\(\)/,
89 "process_options() failed due to invalid 'mode' argument"
92 ($args, $step_list_ref) = process_options(
99 "process_options() returned successfully even though no explicit 'argv' key was provided" );
101 my $CC = "/usr/bin/gcc-3.3";
102 my $CX = "/usr/bin/g++-3.3";
103 ($args, $step_list_ref) = process_options(
106 q{--cc=$CC}, q{--cxx=$CX}, q{--link=$CX}, q{--ld=$CX},
107 q{--without-icu}, q{--without-gmp},
109 mode => q{configure},
113 "process_options() returned successfully when options were specified" );
115 eval { ($args, $step_list_ref) = process_options( { argv => [qq<--${badoption}=72>], mode => q{configure}, } ); };
118 qr/^Invalid option.*$badoption/,
119 "process_options() failed due to bad option '$badoption'"
122 $badoption = q{step};
123 eval { ($args, $step_list_ref) = process_options( { argv => [qq<--${badoption}>], mode => q{configure}, } ); };
126 qr/^Invalid option.*$badoption/,
127 "process_options() failed due to bad option '$badoption'"
130 $badoption = q{target};
131 eval { ($args, $step_list_ref) = process_options( { argv => [qq<--${badoption}>], mode => q{configure}, } ); };
134 qr/^Invalid option.*$badoption/,
135 "process_options() failed due to bad option '$badoption'"
140 $args = capture( sub { process_options(
143 mode => q{configure},
147 "process_options() returned undef after 'help' option" );
148 like( $stdout, qr/--help/i, "got correct message after 'help' option" );
153 $args = capture( sub { process_options(
156 mode => q{configure},
160 "process_options() returned undef after '--' option triggered help message" );
161 like( $stdout, qr/--help/i, "got help message as expected" );
166 $args = capture( sub { process_options(
168 argv => [q{--version}],
169 mode => q{configure},
173 "process_options() returned undef after 'version' option" );
174 like( $stdout, qr/Parrot Version/i,
175 "got correct message after 'version' option" );
178 ($args, $step_list_ref) = process_options(
180 argv => [ q{--lex}, ],
181 mode => q{configure},
185 "process_options() returned successfully after 'lex' option" );
186 ok( $args->{maintainer}, "'maintainer' attribute is true after 'lex' option" );
188 ($args, $step_list_ref) = process_options(
190 argv => [ q{--yacc}, ],
191 mode => q{configure},
195 "process_options() returned successfully after 'yacc' option" );
196 ok( $args->{maintainer}, "'maintainer' attribute is true after 'yacc' option" );
198 ($args, $step_list_ref) = process_options(
200 argv => [q{--debugging=1}],
201 mode => q{configure},
204 ok( defined $args, "process_options() returned successfully" );
205 ok( $args->{debugging}, "debugging turned on explicitly" );
207 ($args, $step_list_ref) = process_options(
209 argv => [q{--debugging=0}],
210 mode => q{configure},
213 ok( defined $args, "process_options() returned successfully" );
214 ok( !$args->{debugging}, "debugging explicitly turned off" );
216 ######### Parrot::Configure::Options internal subroutines #########
218 my ($options_components, $script);
220 $args = { argv => [], mode => 'configure' };
221 ($args, $options_components, $script) =
222 Parrot::Configure::Options::_process_options_components($args);
223 is_deeply($args->{argv}, [], "Got expected value for 'argv' element");
224 is_deeply($options_components,
225 { %Parrot::Configure::Options::Conf::CLI::options_components },
226 "Got expected value for options components");
227 is($script, q{Configure.pl}, "Got expected value for script");
229 $args = { argv => [], mode => 'reconfigure' };
230 ($args, $options_components, $script) =
231 Parrot::Configure::Options::_process_options_components($args);
232 is_deeply($args->{argv}, [], "Got expected value for 'argv' element");
233 is_deeply($options_components,
234 { %Parrot::Configure::Options::Reconf::options_components },
235 "Got expected value for options components");
236 is($script, q{tools/dev/reconfigure.pl}, "Got expected value for script");
238 $args = { argv => [], mode => 'file' };
239 ($args, $options_components, $script) =
240 Parrot::Configure::Options::_process_options_components($args);
241 is_deeply($args->{argv}, [], "Got expected value for 'argv' element");
242 is_deeply($options_components,
243 { %Parrot::Configure::Options::Conf::File::options_components },
244 "Got expected value for options components");
245 is($script, q{Configure.pl}, "Got expected value for script");
247 my $cc = q{/usr/bin/gcc};
249 argv => [ q{--verbose}, q{--help}, qq{--cc=$cc} ],
252 ($args, $options_components, $script) =
253 Parrot::Configure::Options::_process_options_components($args);
254 my ($data, $short_circuits_ref) =
255 Parrot::Configure::Options::_initial_pass(
256 $args, $options_components, $script);
257 is($data->{verbose}, 1, "Got expected value for verbose");
258 is($data->{help}, 1, "Got expected value for help");
259 is($data->{cc}, $cc, "Got expected value for cc");
260 is_deeply($short_circuits_ref, [ q{help} ],
261 "Got expected short circuits");
264 argv => [ q{--verbose}, qq{--cc=$cc} ],
267 ($args, $options_components, $script) =
268 Parrot::Configure::Options::_process_options_components($args);
269 ($data, $short_circuits_ref) =
270 Parrot::Configure::Options::_initial_pass(
271 $args, $options_components, $script);
272 is($data->{verbose}, 1, "Got expected value for verbose");
273 ok(! defined $data->{help}, "Got expected value for help");
274 is($data->{cc}, $cc, "Got expected value for cc");
275 is_deeply($short_circuits_ref, [ ],
276 "Got expected short circuits");
278 pass("Completed all tests in $0");
280 ################### DOCUMENTATION ###################
284 001-options.t - test Parrot::Configure::Options as used in Configure.pl
288 % prove t/configure/001-options.t
292 The files in this directory test functionality used by F<Configure.pl>.
294 The tests in this file test subroutines exported by
295 Parrot::Configure::Options as it is used in F<Configure.pl>, I<i.e.>, with
296 C<mode =E<gt> q{configure}>.
304 Parrot::Configure::Options, Parrot::Configure::Options::Conf,
305 Parrot::Configure::Options::Conf::CLI, F<Configure.pl>.
311 # cperl-indent-level: 4
314 # vim: expandtab shiftwidth=4: