/
Test.pm
1164 lines (876 loc) · 35.7 KB
/
Test.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2004-2009, Parrot Foundation.
=head1 NAME
Parrot::Test - testing routines for Parrot and language implementations
=head1 SYNOPSIS
Set the number of tests to be run like this:
use Parrot::Test tests => 8;
Write individual tests like this:
pasm_output_is(<<'CODE', <<'OUTPUT', "description of test");
print "this is ok\n"
end
CODE
this is ok
OUTPUT
=head1 DESCRIPTION
This module provides various Parrot-specific test functions.
=head2 Functions
The parameter C<$language> is the language of the code.
The parameter C<$code> is the code that should be executed or transformed.
The parameter C<$expected> is the expected result.
The parameter C<$unexpected> is the unexpected result.
The parameter C<$description> should describe the test.
Any optional parameters can follow. For example, to mark a test as a TODO test
(where you know the implementation does not yet work), pass:
todo => 'reason to consider this TODO'
at the end of the argument list. Valid reasons include C<bug>,
C<unimplemented>, and so on.
B<Note:> you I<must> use a C<$description> with TODO tests.
=over 4
=item C<language_output_is( $language, $code, $expected, $description)>
=item C<language_error_output_is( $language, $code, $expected, $description)>
Runs a language test and passes the test if a string comparison
of the output with the expected result it true.
For C<language_error_output_is()> the exit code also has to be non-zero.
=item C<language_output_like( $language, $code, $expected, $description)>
=item C<language_error_output_like( $language, $code, $expected, $description)>
Runs a language test and passes the test if the output matches the expected
result.
For C<language_error_output_like()> the exit code also has to be non-zero.
=item C<language_output_isnt( $language, $code, $expected, $description)>
=item C<language_error_output_isnt( $language, $code, $expected, $description)>
Runs a language test and passes the test if a string comparison
if a string comparison of the output with the unexpected result is false.
For C<language_error_output_isnt()> the exit code also has to be non-zero.
=item C<pasm_output_is($code, $expected, $description)>
Runs the Parrot Assembler code and passes the test if a string comparison of
the output with the expected result it true.
=item C<pasm_error_output_is($code, $expected, $description)>
Runs the Parrot Assembler code and passes the test if a string comparison of
the output with the expected result it true I<and> if Parrot exits with a
non-zero exit code.
=item C<pasm_output_like($code, $expected, $description)>
Runs the Parrot Assembler code and passes the test if the output matches
C<$expected>.
=item C<pasm_error_output_like($code, $expected, $description)>
Runs the Parrot Assembler code and passes the test if the output matches
C<$expected> I<and> if Parrot exits with a non-zero exit code.
=item C<pasm_output_isnt($code, $unexpected, $description)>
Runs the Parrot Assembler code and passes the test if a string comparison of
the output with the unexpected result is false.
=item C<pasm_error_output_isnt($code, $unexpected, $description)>
Runs the Parrot Assembler code and passes the test if a string comparison of
the output with the unexpected result is false I<and> if Parrot exits with a
non-zero exit code.
=item C<pasm_exit_code_is($code, $exit_code, $description)>
Runs the PASM code and passes the test if the exit code equals $exit_code,
fails the test otherwise.
=item C<pir_output_is($code, $expected, $description)>
Runs the PIR code and passes the test if a string comparison of output with the
expected result is true.
=item C<pir_error_output_is($code, $expected, $description)>
Runs the PIR code and passes the test if a string comparison of output with the
expected result is true I<and> if Parrot exits with a non-zero exit code.
=item C<pir_output_like($code, $expected, $description)>
Runs the PIR code and passes the test if output matches the expected result.
=item C<pir_error_output_like($code, $expected, $description)>
Runs the PIR code and passes the test if output matches the expected result
I<and> if Parrot exits with a non-zero exit code.
=item C<pir_output_isnt($code, $unexpected, $description)>
Runs the PIR code and passes the test if a string comparison of the output with
the unexpected result is false.
=item C<pir_error_output_isnt($code, $unexpected, $description)>
Runs the PIR code and passes the test if a string comparison of the output with
the unexpected result is false I<and> if Parrot exits with a non-zero exit
code.
=item C<pir_exit_code_is($code, $exit_code, $description)>
Runs the PIR code and passes the test if the exit code equals $exit_code,
fails the test otherwise.
=item C<pbc_output_is($code, $expected, $description)>
Runs the Parrot Bytecode and passes the test if a string comparison of output
with the expected result is true.
=item C<pbc_error_output_is($code, $expected, $description)>
Runs the Parrot Bytecode and passes the test if a string comparison of the output
with the expected result is true I<and> if Parrot exits with a non-zero exit code.
=item C<pbc_output_like($code, $expected, $description)>
Runs the Parrot Bytecode and passes the test if output matches the expected
result.
=item C<pbc_error_output_like($code, $expected, $description)>
Runs the Parrot Bytecode and passes the test if output matches the expected
result I<and> if Parrot exits with a non-zero exit code.
=item C<pbc_output_isnt($code, $unexpected, $description)>
Runs the Parrot Bytecode and passes the test if a string comparison of output
with the unexpected result is false.
=item C<pbc_error_output_isnt($code, $unexpected, $description)>
Runs the Parrot Bytecode and passes the test if a string comparison of output
with the unexpected result is false I<and> if Parrot exits with a non-zero exit
code.
=item C<pbc_exit_code_is($code, $exit_code, $description)>
Runs the Parrot Bytecode and passes the test if the exit code equals $exit_code,
fails the test otherwise.
=item C<c_output_is($code, $expected, $description, %options)>
Compiles and runs the C code, passing the test if a string comparison of output
with the expected result it true. Valid options are 'todo' => 'reason' to mark
a TODO test.
=item C<c_output_like($code, $expected, $description, %options)>
Compiles and runs the C code, passing the test if output matches the expected
result. Valid options are 'todo' => 'reason' to mark a TODO test.
=item C<c_output_isnt($code, $unexpected, $description, %options)>
Compiles and runs the C code, passing the test if a string comparison of output
with the unexpected result is false. Valid options are 'todo' => 'reason' to
mark a TODO test.
=item C<example_output_is( $example_f, $expected, @todo )>
=item C<example_output_like( $example_f, $expected, @todo )>
=item C<example_output_isnt( $example_f, $expected, @todo )>
Determines the language, PIR or PASM, from the extension of C<$example_f> and runs
the appropriate C<^language_output_(is|like|isnt)> sub.
C<$example_f> is used as a description, so don't pass one.
=item C<skip($why, $how_many)>
Use within a C<SKIP: { ... }> block to indicate why and how many tests to skip,
just like in Test::More.
=item C<run_command($command, %options)>
Run the given $command in a cross-platform manner.
%options include...
STDOUT name of file to redirect STDOUT to
STDERR name of file to redirect STDERR to
CD directory to run the command in
For example:
# equivalent to "cd some_dir && make test"
run_command("make test", CD => "some_dir");
=item C<slurp_file($file_name)>
Read the whole file $file_name and return the content as a string. This is
just an alias for C<Parrot::BuildUtil::slurp_file>.
=item C<convert_line_endings($text)>
Convert Win32 style line endins with Unix style line endings.
=item C<path_to_parrot()>
Construct an absolute path to the parrot root dir.
=item C<per_test( $ext, $test_no )>
Construct a path for a temporary files.
Takes C<$0> into account.
=item C<write_code_to_file($code, $code_f)>
Writes C<$code> into the file C<$code_f>.
=item C<generate_languages_functions>
Generate functions that are only used by a couple of
Parrot::Test::<lang> modules.
This implementation is experimental and currently only works
for languages/pipp.
=back
=cut
package Parrot::Test;
use strict;
use warnings;
use Cwd;
use File::Spec;
use File::Basename;
use Memoize ();
use IO::File ();
use lib qw( lib );
use Parrot::BuildUtil ();
use Parrot::Config;
use Parrot::Test::Util 'create_tempfile';
require Exporter;
require Test::Builder;
require Test::More;
our @EXPORT = qw( plan run_command skip slurp_file pbc_postprocess_output_like
pir_stdin_output_is pir_stdin_output_like );
use base qw( Exporter );
# Memoize functions with a fixed output
Memoize::memoize('path_to_parrot');
# Tell parrot it's being tested--disables searching of installed libraries.
$ENV{PARROT_TEST} = 1 unless defined $ENV{PARROT_TEST};
my $builder = Test::Builder->new();
# Generate subs where the name serves as an
# extra parameter.
_generate_test_functions();
sub import {
my ( $class, $plan, @args ) = @_;
$builder->plan( $plan, @args );
__PACKAGE__->export_to_level( 2, __PACKAGE__ );
}
# this kludge is an hopefully portable way of having
# redirections ( tested on Linux and Win2k )
# An alternative is using Test::Output
sub run_command {
my ( $command, %options ) = @_;
my ( $out, $err, $chdir ) = _handle_test_options( \%options );
if ($PConfig{parrot_is_shared}) {
_handle_blib_path();
}
local *OLDOUT if $out; ## no critic Variables::ProhibitConditionalDeclarations
local *OLDERR if $err; ## no critic Variables::ProhibitConditionalDeclarations
# Save the old filehandles; we must not let them get closed.
open OLDOUT, '>&STDOUT' ## no critic InputOutput::ProhibitBarewordFileHandles
or die "Can't save stdout"
if $out;
open OLDERR, '>&STDERR' ## no critic InputOutput::ProhibitBarewordFileHandles
or die "Can't save stderr"
if $err;
open STDOUT, '>', $out or die "Can't redirect stdout to $out" if $out;
# See 'Obscure Open Tricks' in perlopentut
open STDERR, ">$err" ## no critic InputOutput::ProhibitTwoArgOpen
or die "Can't redirect stderr to $err"
if $err;
# If $command isn't already an arrayref (because of a multi-command
# test), make it so now so the code below can treat everybody the
# same.
$command = _handle_command( $command );
my $orig_dir;
if ($chdir) {
$orig_dir = cwd;
chdir $chdir;
}
# Execute all commands
# [#42161] [BUG] Parrot::Test throws "Can't spawn" warning on windows
# ...if a system call returns a negative value
# removed exec warnings to prevent this warning from messing up test results
{
no warnings 'exec';
system($_) for ( @{$command} );
}
if ($chdir) {
chdir $orig_dir;
}
my $exit_message = _prepare_exit_message();
close STDOUT or die "Can't close stdout" if $out;
close STDERR or die "Can't close stderr" if $err;
open STDOUT, ">&", \*OLDOUT or die "Can't restore stdout" if $out;
open STDERR, ">&", \*OLDERR or die "Can't restore stderr" if $err;
return $exit_message;
}
sub per_test {
my ( $ext, $test_no ) = @_;
return unless defined $ext and defined $test_no;
my $t = $0; # $0 is name of the test script
$t =~ s/\.t$/_$test_no$ext/;
return $t;
}
sub write_code_to_file {
my ( $code, $code_f ) = @_;
open my $CODE, '>', $code_f or die "Unable to open '$code_f'";
binmode $CODE;
print $CODE $code;
close $CODE;
return;
}
{
no warnings 'once';
# We can inherit from other modules, so we do so.
*plan = \&Test::More::plan;
*skip = \&Test::More::skip;
*slurp_file = \&Parrot::BuildUtil::slurp_file;
}
sub convert_line_endings {
my ($text) = @_;
$text =~ s/\cM\cJ/\n/g;
return;
}
sub path_to_parrot {
my $path = $INC{'Parrot/Config.pm'};
$path =~ s{ /lib/Parrot/Config.pm \z}{}xms;
return Cwd::realpath( $path );
}
sub generate_languages_functions {
my %test_map = (
output_is => 'is_eq',
error_output_is => 'is_eq',
output_like => 'like',
error_output_like => 'like',
output_isnt => 'isnt_eq',
error_output_isnt => 'isnt_eq',
);
foreach my $func ( keys %test_map ) {
my $test_sub = sub {
local *__ANON__ = $func;
my $self = shift;
my ( $code, $expected, $desc, %options ) = @_;
# set a todo-item for Test::Builder to find
my $call_pkg = $self->{builder}->exported_to() || '';
no strict 'refs'; ## no critic Variables::ProhibitConditionalDeclarations
local *{ $call_pkg . '::TODO' } = ## no critic Variables::ProhibitConditionalDeclarations
\$options{todo}
if defined $options{todo};
my $count = $self->{builder}->current_test() + 1;
# These are the thing that depend on the actual language implementation
my $out_f = $self->get_out_fn( $count, \%options );
my $lang_f = $self->get_lang_fn( $count, \%options );
my $cd = $self->get_cd( \%options );
my @test_prog = $self->get_test_prog( $count, \%options );
Parrot::Test::write_code_to_file( $code, $lang_f );
# set a todo-item for Test::Builder to find
my $skip_why = $self->skip_why( \%options );
if ($skip_why) {
$self->{builder}->skip($skip_why);
}
else {
# STDERR is written into same output file
my $exit_code = Parrot::Test::run_command(
\@test_prog,
CD => $cd,
STDOUT => $out_f,
STDERR => $out_f
);
my $real_output = slurp_file($out_f);
if ( $func =~ m/^ error_/xms ) {
return _handle_error_output( $self->{builder}, $real_output, $expected, $desc )
unless $exit_code;
}
elsif ($exit_code) {
$self->{builder}->ok( 0, $desc );
my $test_prog = join ' && ', @test_prog;
$self->{builder}->diag("'$test_prog' failed with exit code $exit_code.");
return 0;
}
my $meth = $test_map{$func};
$self->{builder}->$meth( $real_output, $expected, $desc );
}
# The generated files are left in the t/* directories.
# Let 'make clean' and '.gitignore' take care of them.
return;
};
my ($package) = caller();
create_sub($package, $func, $test_sub);
}
}
sub create_sub {
my $package = shift;
my $func = shift;
my $sub = shift;
no strict 'refs';
*{ $package . '::' . $func } = $sub;
return;
}
=over
=item "pbc_postprocess_output_like"
Takes a path to binary which will post process PBC, a file to run, the extension
of the file, one regex or an array reference of regexes, and an optional
diagnostic message. This function generates PBC for the input file, then post
processes this with the binary and captures the output. The output is then
verified to match the single or multiple regular expressions given.
my $postprocess = File::Spec->catfile( ".", "pbc_dump" );
my $file = 'foo.pir';
my $ext = 'pir';
my $check = [ qr/has a foo/, qr/and a bar/ ];
pbc_postprocess_output_like ( $postprocess,
$file, $ext, $check,
"checking pbc_dump"
);
=back
=cut
sub pbc_postprocess_output_like {
my ( $postprocess, $file, $ext, $check, $diag ) = @_;
my $testno = $builder->current_test() + 1;
my $codefn = "$0.$testno.$ext";
my $pbcfn = "$0.$testno.pbc";
my $stdoutfn = "$0.$testno.stdout";
my $f = IO::File->new(">$codefn");
my $parrot = File::Spec->catfile( ".", $PConfig{test_prog} );
$f->print($file);
$f->close();
system("$parrot -o $pbcfn $codefn 2>&1");
system("$postprocess $pbcfn >$stdoutfn 2>&1");
$f = IO::File->new($stdoutfn);
my $output = join( '', <$f> );
local $Test::Builder::Level = $Test::Builder::Level + 1;
unlink ($codefn, $pbcfn, $stdoutfn);
if (ref $check eq 'ARRAY') {
for my $regex (@$check) {
Test::More::like( $output, $regex, $diag );
$testno++;
}
}
else {
Test::More::like( $output, $check, $diag );
}
}
sub _pir_stdin_output_slurp {
my ($input_string, $code, $expected_ouptut) = @_;
my $stuff = sub {
# Put the string on a file.
my $string = shift;
my (undef, $file) = create_tempfile(UNLINK => 1);
open(my $out, '>', $file) or die "bug";
binmode $out;
print $out $string;
return $file;
};
# Write the input and code strings.
my $input_file = $stuff->($input_string);
my $code_file = $stuff->($code);
my $parrot = ".$PConfig{slash}parrot$PConfig{exe}";
# Slurp and compare the output.
my $result = do {
local $/;
open(my $in, '-|', "$parrot $code_file 2>&1 < $input_file")
or die "bug";
<$in>;
};
return $result;
}
=over
=item C<pir_stdin_output_is($input_string, $code, $expected, $description)>
Runs the PIR code while piping data into its standard input and passes the test
if a string comparison of output with the expected result is true.
=cut
sub pir_stdin_output_is {
my ($input_string, $code, $expected_output, $description) = @_;
my $result = _pir_stdin_output_slurp($input_string, $code, $expected_output);
Test::More::is($result, $expected_output, $description);
}
=item C<pir_stdin_output_like($input_string, $code, $expected, $description)>
Runs the PIR code while piping data into its standard input and passes the test
if the output matches the expected result.
=back
=cut
sub pir_stdin_output_like {
my ($input_string, $code, $expected_output, $description) = @_;
my $result = _pir_stdin_output_slurp($input_string, $code, $expected_output);
Test::More::like($result, $expected_output, $description);
}
# The following methods are private. They should not be used by modules
# inheriting from Parrot::Test.
sub _handle_error_output {
my ( $builder, $real_output, $expected, $desc ) = @_;
my $level = $builder->level();
$builder->level( $level + 1 );
$builder->ok( 0, $desc );
$builder->diag(
"Expected error but exited cleanly\n" . "Received:\n$real_output\nExpected:\n$expected\n" );
$builder->level($level);
return 0;
}
sub _run_test_file {
my ( $func, $code, $expected, $desc, %extra ) = @_;
my $path_to_parrot = path_to_parrot();
my $parrot = File::Spec->join( File::Spec->curdir(), 'parrot' . $PConfig{exe} );
# Strange Win line endings
convert_line_endings($expected);
# $test_no will be part of temporary file
my $test_no = $builder->current_test() + 1;
# Name of the file where output is written.
# Switch to a different extension when we are generating code.
my $out_f = per_test( '.out', $test_no );
# Name of the file with test code.
# This depends on which kind of code we are testing.
my $code_f;
if ( $func =~ m/^pir_(exit_code|.*?output)/ ) {
$code_f = per_test( '.pir', $test_no );
}
elsif ( $func =~ m/^pasm_(exit_code|.*?output_)/ ) {
$code_f = per_test( '.pasm', $test_no );
}
elsif ( $func =~ m/^pbc_(exit_code|.*?output_)/ ) {
$code_f = per_test( '.pbc', $test_no );
}
else {
die "Unknown test function: $func";
}
$code_f = File::Spec->rel2abs($code_f);
my $code_basef = basename($code_f);
# native tests are just run, others need to write code first
if ( $code_f !~ /\.pbc$/ ) {
write_code_to_file( $code, $code_f );
}
# honor opt* filename to actually run code with -Ox
my $args = $ENV{TEST_PROG_ARGS} || '';
my $opt = $code_basef =~ m!opt(.)! ? "-O$1" : "";
$args .= " $opt";
my $run_exec = 0;
if ( $args =~ s/--run-exec// ) {
$run_exec = 1;
my $pbc_f = per_test( '.pbc', $test_no );
my $o_f = per_test( '_pbcexe' . $PConfig{o}, $test_no );
# make cleanup and .gitignore more simple
my $exe_f = per_test( '_pbcexe' . $PConfig{exe}, $test_no );
my $exec_f = per_test( '_pbcexe', $test_no );
$exe_f =~ s@[\\/:]@$PConfig{slash}@g;
run_command(
qq{$parrot $args -o $pbc_f "$code_f"},
CD => $path_to_parrot,
STDOUT => $out_f,
STDERR => $out_f
);
if ( -e $pbc_f ) {
run_command(
qq{$parrot $args -o $o_f "$pbc_f"},
CD => $path_to_parrot,
STDOUT => $out_f,
STDERR => $out_f
);
if ( -e $o_f ) {
run_command(
qq{$PConfig{make} EXEC=$exec_f exec},
CD => $path_to_parrot,
STDOUT => $out_f,
STDERR => $out_f
);
if ( -e $exe_f ) {
run_command(
$exe_f,
CD => $path_to_parrot,
STDOUT => $out_f,
STDERR => $out_f
);
}
}
}
}
my ( $exit_code, $cmd );
unless ($run_exec) {
if ( $args =~ s/--run-pbc// || $args =~ s/-r // ) {
my $pbc_f = per_test( '.pbc', $test_no );
$args = qq{$args -o "$pbc_f"};
# In this case, we need to execute more than one command. Instead
# of a single scalar, build an array of commands.
$cmd = [ qq{$parrot $args "$code_f"}, qq{$parrot "$pbc_f"}, ];
}
else {
$cmd = qq{$parrot $args "$code_f"};
}
$exit_code = run_command(
$cmd,
CD => $path_to_parrot,
STDOUT => $out_f,
STDERR => $out_f
);
}
return ( $out_f, $cmd, $exit_code );
}
sub _generate_test_functions {
my $package = 'Parrot::Test';
my $path_to_parrot = path_to_parrot();
my $parrot = File::Spec->join( File::Spec->curdir(),
'parrot' . $PConfig{exe} );
##### 1: Parrot test map #####
my %parrot_test_map = map {
$_ . '_output_is' => 'is_eq',
$_ . '_error_output_is' => 'is_eq',
$_ . '_exit_code_is' => 'is_eq',
$_ . '_output_isnt' => 'isnt_eq',
$_ . '_error_output_isnt' => 'isnt_eq',
$_ . '_output_like' => 'like',
$_ . '_error_output_like' => 'like',
$_ . '_output_unlike' => 'unlike',
$_ . '_error_output_unlike' => 'unlike',
} qw( pasm pbc pir );
for my $func ( keys %parrot_test_map ) {
push @EXPORT, $func;
my $test_sub = sub {
local *__ANON__ = $func;
my ( $code, $expected, $desc, %extra ) = @_;
my $args = $ENV{TEST_PROG_ARGS} || '';
# Due to ongoing changes in PBC format, all tests in
# t/native_pbc/*.t are currently being SKIPped. This means we
# have no tests on which to model tests of the following block.
# Hence, test coverage will be lacking.
if ( $func =~ /^pbc_output_/ && $args =~ /-r / ) {
# native tests with --run-pbc don't make sense
return $builder->skip("no native tests with -r");
}
my ( $out_f, $cmd, $exit_code ) = _run_test_file( $func, @_ );
my $meth = $parrot_test_map{$func};
my $real_output = slurp_file($out_f);
_unlink_or_retain( $out_f );
# set a todo-item for Test::Builder to find
my $call_pkg = $builder->exported_to() || '';
no strict 'refs';
local *{ $call_pkg . '::TODO' } = ## no critic Variables::ProhibitConditionalDeclarations
\$extra{todo}
if defined $extra{todo};
if ( $func =~ /_exit_code_is$/ ) {
$expected = int($expected);
if ($exit_code == $expected) {
my $pass = $builder->$meth( $exit_code, $expected, $desc );
return $pass;
}
else {
$builder->ok(0);
return 0;
}
}
if ( $func =~ /_error_/ ) {
return _handle_error_output( $builder, $real_output, $expected, $desc )
unless $exit_code;
}
elsif ($exit_code) {
$builder->ok( 0, $desc );
$builder->diag( "Exited with error code: $exit_code\n"
. "Received:\n$real_output\nExpected:\n$expected\n" );
return 0;
}
my $pass = $builder->$meth( $real_output, $expected, $desc );
$builder->diag("'$cmd' failed with exit code $exit_code")
if not $pass and $exit_code;
return $pass;
};
create_sub($package, $func, $test_sub);
}
##### 2: Language test map #####
my %builtin_language_prefix = (
PIR_IMCC => 'pir',
PASM_IMCC => 'pasm',
);
my %language_test_map = (
language_output_is => 'output_is',
language_error_output_is => 'error_output_is',
language_output_like => 'output_like',
language_error_output_like => 'error_output_like',
language_output_isnt => 'output_isnt',
language_error_output_isnt => 'error_output_isnt',
);
foreach my $func ( keys %language_test_map ) {
push @EXPORT, $func;
my $test_sub = sub {
local *__ANON__ = $func;
my ( $language, @remaining ) = @_;
my $meth = $language_test_map{$func};
if ( my $prefix = $builtin_language_prefix{$language} ) {
# builtin languages are no tested with the example_output_xx() functions
my $level = $builder->level();
$builder->level( $level + 2 );
my $test_func = "${package}::${prefix}_${meth}";
no strict 'refs';
$test_func->(@remaining);
$builder->level($level);
}
else {
$language = ucfirst($language);
# make sure todo-items will work, by telling Test::Builder which
# package the .t file is in (one more than usual, due to the
# extra layer of package indirection
my $level = $builder->level();
$builder->level(2);
# Load module that knows how to test the language implementation
require "Parrot/Test/$language.pm";
my $class = "Parrot::Test::${language}";
# set the builder object, and parrot config.
my $obj = $class->new();
$obj->{builder} = $builder;
$obj->{relpath} = $path_to_parrot;
$obj->{parrot} = $parrot;
$obj->$meth(@remaining);
# restore prior level, just in case.
$builder->level($level);
}
};
create_sub($package, $func, $test_sub);
}
##### 3: Example test map #####
my %example_test_map = (
example_output_is => 'language_output_is',
example_output_like => 'language_output_like',
example_output_isnt => 'language_output_isnt',
example_error_output_is => 'language_error_output_is',
example_error_output_isnt => 'language_error_output_is',
example_error_output_like => 'language_error_output_like',
);
foreach my $func ( keys %example_test_map ) {
push @EXPORT, $func;
my $test_sub = sub {
local *__ANON__ = $func;
my ( $example_f, $expected, @options ) = @_;
my %lang_for_extension = (
pasm => 'PASM_IMCC',
pir => 'PIR_IMCC',
);
my ($extension) = $example_f =~ m{ [.] # introducing extension
( pasm | pir ) # match and capture the extension
\z # at end of string
}ixms;
if ( defined $extension ) {
my $code = slurp_file($example_f);
my $test_func = join( '::', $package, $example_test_map{$func} );
no strict 'refs';
$test_func->(
$lang_for_extension{$extension},
$code, $expected, $example_f, @options
);
}
else {
$builder->diag("no extension recognized for $example_f");
}
};
create_sub($package, $func, $test_sub);
}
##### 4: C test map #####
my %c_test_map = (
c_output_is => 'is_eq',
c_output_isnt => 'isnt_eq',
c_output_like => 'like',
c_output_unlike => 'unlike',
);
foreach my $func ( keys %c_test_map ) {
push @EXPORT, $func;
my $test_sub = sub {
local *__ANON__ = $func;
my ( $source, $expected, $desc, %options ) = @_;
# $test_no will be part of temporary files
my $test_no = $builder->current_test() + 1;
convert_line_endings($expected);
my $obj_f = per_test( $PConfig{o}, $test_no );
my $exe_f = per_test( $PConfig{exe}, $test_no );
$exe_f =~ s@[\\/:]@$PConfig{slash}@g;
my $out_f = per_test( '.out', $test_no );
my $build_f = per_test( '.build', $test_no );
# set todo-option before trying to compile or link
local *main::TODO;
*main::TODO = \$options{todo} if $options{todo};
# compile the source
{
my $source_f = per_test( '.c', $test_no );
write_code_to_file( $source, $source_f );
my $cmd =
"$PConfig{cc} $PConfig{ccflags} $PConfig{cc_debug} "
. ($^O =~ m/MSWin32/ and $PConfig{cc} eq 'cl' ? "-DPARROT_IN_EXTENSION" : "")
. " -I./include -c "
. "$PConfig{cc_o_out}$obj_f $source_f";
my $exit_code = run_command(
$cmd,
'STDOUT' => $build_f,
'STDERR' => $build_f
);
$builder->diag("'$cmd' failed with exit code $exit_code")
if $exit_code;
if ( !-e $obj_f ) {
$builder->diag( "Failed to build '$obj_f': " . slurp_file($build_f) );
unlink $build_f;
$builder->ok( 0, $desc );
return 0;
}
}
# link the compiled source, get an executable
{
my $cfg = File::Spec->join( 'src', "parrot_config$PConfig{o}" );