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
|
use 5.008004;
use lib 'corpus/lib';
use lib 't/lib';
use Test2::V0 -no_srand => 1;
use Test::Alien;
use Alien::Foo;
use Alien::perlhelp;
use Alien::libfoo1;
use Env qw( @PATH );
use ExtUtils::CBuilder;
use Alien::Build::Util qw( _dump );
use List::Util 1.33 qw( any );
use Config;
use Test2::API 1.302096 ();
use MyTest::HaveCompiler qw( require_compiler );
$ENV{TEST_ALIEN_ALIENS_MISSING} = 0;
sub _reset
{
@Test::Alien::aliens = ();
}
subtest 'alien_ok' => sub {
_reset();
local $ENV{PATH} = $ENV{PATH};
subtest 'as class' => sub {
local $ENV{PATH} = $ENV{PATH};
is(
intercept { alien_ok 'Alien::Foo' },
array {
event Ok => sub {
call pass => T();
call name => 'Alien::Foo responds to: cflags libs dynamic_libs bin_dir';
};
end;
},
"alien_ok with class"
);
is $PATH[0], '/foo/bar/baz', 'bin_dir added to path';
};
subtest 'as object' => sub {
local $ENV{PATH} = $ENV{PATH};
my $alien = Alien::Foo->new;
is(
intercept { alien_ok $alien },
array {
event Ok => sub {
call pass => T();
call name => 'Alien::Foo[instance] responds to: cflags libs dynamic_libs bin_dir';
};
end;
},
"alien_ok with class"
);
is $PATH[0], '/foo/bar/baz', 'bin_dir added to path';
};
is(
intercept { alien_ok(Alien::Foo->new) },
array {
event Ok => sub {
call pass => T();
call name => 'Alien::Foo[instance] responds to: cflags libs dynamic_libs bin_dir';
};
end;
},
"alien_ok with instance"
);
is(
intercept { alien_ok 'Alien::Bogus' },
array {
event Ok => sub {
call pass => F();
call name => 'Alien::Bogus responds to: cflags libs dynamic_libs bin_dir';
};
event Diag => sub {};
event Diag => sub {
call message => " missing method $_";
} for qw( cflags libs dynamic_libs bin_dir );
end;
},
"alien_ok with bad class",
);
is(
intercept { alien_ok undef },
array {
event Ok => sub {
call pass => F();
call name => 'undef responds to: cflags libs dynamic_libs bin_dir';
};
event Diag => sub {};
event Diag => sub {
call message => ' undefined alien';
};
end;
},
'alien_ok with undef',
);
};
subtest 'helper_ok' => sub {
_reset();
alien_ok 'Alien::libfoo1';
helper_ok 'foo1';
helper_ok 'foo2', 'something else';
is(
intercept { helper_ok 'foo1' },
array {
event Ok => sub {
call pass => T();
call name => 'helper foo1 exists';
};
end;
},
'default test name',
);
is(
intercept { helper_ok 'foo2', 'something else' },
array {
event Ok => sub {
call pass => T();
call name => 'something else';
};
end;
},
'custom name',
);
is(
intercept { helper_ok 'foo3' },
array {
event Ok => sub {
call pass => F();
call name => 'helper foo3 exists';
};
event Diag => sub {};
end;
},
'failed test',
);
};
subtest 'plugin_ok' => sub {
_reset();
local $ENV{TEST_ALIEN_ALIENS_MISSING} = 1;
plugin_ok 'NesAdvantage::HelperTest';
helper_ok 'nes';
interpolate_template_is '%{nes}', 'advantage';
};
subtest 'interpolate_template_is' => sub {
_reset();
alien_ok 'Alien::libfoo1';
interpolate_template_is '%{foo1}', 'bar3';
interpolate_template_is '%{foo2}', qr{az7};
is(
intercept { interpolate_template_is '%{foo1}', 'bar3' },
array {
event Ok => sub {
call pass => T();
call name => 'template matches';
};
end;
},
'pass with default name',
);
is(
intercept { interpolate_template_is '%{foo1}', 'bar3', 'something else' },
array {
event Ok => sub {
call pass => T();
call name => 'something else';
};
end;
},
'pass with custom name',
);
is(
intercept { interpolate_template_is '%{foo1}', 'bar4' },
array {
event Ok => sub {
call pass => F();
call name => 'template matches';
};
event Diag => sub {};
event Diag => sub {
call message => 'value \'bar3\' does not equal \'bar4\'';
};
end;
},
'fail with string match',
);
is(
intercept { interpolate_template_is '%{foo1}', qr{xx} },
array {
event Ok => sub {
call pass => F();
call name => 'template matches';
};
event Diag => sub {};
event Diag => sub {
call message => match(qr{^value 'bar3' does not match });
};
end;
},
'fail with string match',
);
is(
intercept { interpolate_template_is '%{bogus}', 'bar4' },
array {
event Ok => sub {
call pass => F();
call name => 'template matches';
};
event Diag => sub {};
event Diag => sub {
call message => 'error in evaluation:';
};
event Diag => sub {
call message => match(qr/^ /);
};
end;
},
'bogus helper',
);
};
subtest 'ffi_ok' => sub {
skip_all 'Test requires FFI::Platypus'
unless eval { require FFI::Platypus; 1 };
_reset();
alien_ok synthetic {};
is(
intercept { ffi_ok; },
array {
event Ok => sub {
call pass => T();
call name => 'ffi';
};
end;
},
'empty ffi'
);
ffi_ok {}, 'min version test', with_subtest {
my($ffi) = @_;
my $version = $ffi->VERSION;
$version =~ s/_.*$//;
cmp_ok $version, '>=', 0.12;
};
ffi_ok { ignore_not_found => 1 }, 'ignore not found', with_subtest {
my($ffi) = @_;
my $version = $ffi->VERSION;
$version =~ s/_.*$//;
cmp_ok $version, '>=', 0.15;
eval { $ffi->attach( foo => [] => 'void') };
is $@, '';
is __PACKAGE__->can('foo'), F();
};
ffi_ok { lang => 'Fortran' }, 'lang', with_subtest {
my($ffi) = @_;
my $version = $ffi->VERSION;
$version =~ s/_.*$//;
cmp_ok $version, '>=', 0.18;
is $ffi->lang, 'Fortran';
};
subtest 'find symbols' => sub {
subtest 'good symbols' => sub {
my @symbols;
my $mock = mock 'FFI::Platypus' => (
override => [
find_symbol => sub {
my(undef, $symbol) = @_;
push @symbols, $symbol;
1;
},
],
);
is(
intercept { ffi_ok { symbols => [qw( foo bar baz )] } },
array {
event Ok => sub {
call pass => T();
call name => 'ffi';
};
},
'test passes',
);
is(
\@symbols,
[qw( foo bar baz )],
'tested symbols',
);
};
subtest 'bad symbols' => sub {
is(
intercept { ffi_ok { symbols => [qw( bogus1 bogus2 )] } },
array {
event Ok => sub {
call pass => F();
call name => 'ffi';
};
event Diag => sub {};
event Diag => sub {
call message => ' bogus1 not found';
};
event Diag => sub {
call message => ' bogus2 not found';
};
end;
},
'not found error'
);
};
};
subtest 'acme' => sub {
skip_all 'Test requires Acme::Alien::DontPanic 0.026'
unless eval {
require Acme::Alien::DontPanic;
Acme::Alien::DontPanic->VERSION('0.026');
};
alien_ok 'Acme::Alien::DontPanic';
ffi_ok { symbols => ['answer'] } , with_subtest {
my($ffi) = @_;
is $ffi->function('answer' => [] => 'int')->call(), 42, 'answer is 42';
};
};
};
subtest 'xs_ok' => sub {
require_compiler();
_reset();
alien_ok synthetic {};
subtest 'bad' => sub {
local $ENV{TEST_ALIEN_ALWAYS_KEEP} = 0;
is(
intercept { xs_ok '' },
array {
event Ok => sub {
call pass => F();
call name => 'xs';
};
event Diag => sub {};
event Diag => sub {
call message => ' XS does not have a module decleration that we could find';
};
end;
},
'xs with no module'
);
is(
intercept { xs_ok '', sub { } },
array {
event Ok => sub {
call pass => F();
call name => 'xs';
};
event Diag => sub {};
event Diag => sub {
call message => ' XS does not have a module decleration that we could find';
};
event Subtest => sub {
call buffered => T();
call subevents => array {
event Plan => sub {
call max => 0;
call directive => 'SKIP';
call reason => 'subtest requires xs success';
};
end;
};
};
end;
},
'xs fail with subtest'
);
# TODO: test that parsexs error should fail
is(
intercept { xs_ok q{
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#error "this should cause a compile error"
MODULE = Foo::Bar PACKAGE = Foo::Bar
} },
array {
event Ok => sub {
call pass => F();
call name => 'xs';
};
event Diag => sub {};
event Diag => sub {
call message => ' ExtUtils::CBuilder->compile failed';
};
etc;
},
'xs with C compile error'
);
# TODO: test that link error should fail
};
subtest 'good' => sub {
my $xs = <<'EOF';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
int baz(const char *class)
{
return 42;
}
MODULE = TA_MODULE PACKAGE = TA_MODULE
int baz(class);
const char *class;
EOF
xs_ok { xs => $xs, verbose => 1 }, with_subtest {
my($module) = @_;
is $module->baz(), 42, "call $module->baz()";
};
$xs =~ s{\bTA_MODULE\b}{Foo::Bar}g;
xs_ok $xs, 'xs without parameterized name', with_subtest {
my($module) = @_;
is $module, 'Foo::Bar';
is $module->baz(), 42, "call $module->baz()";
};
};
subtest 'with xs_load' => sub {
_reset();
my $xs = <<'EOF';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = TA_MODULE PACKAGE = TA_MODULE
int
get_foo_one(klass)
const char *klass
CODE:
RETVAL = FOO_ONE;
OUTPUT:
RETVAL
int
get_foo_two(klass)
const char *klass
CODE:
RETVAL = FOO_TWO;
OUTPUT:
RETVAL
EOF
my @aliens = (
synthetic({ cflags => '-DFOO_ONE=42' }),
synthetic({ cflags => '-DFOO_TWO=47' }),
);
alien_ok $aliens[0];
alien_ok $aliens[1];
my @xs_load_args;
my $mock = mock 'Test::Alien::Synthetic' => (
add => [
xs_load => sub {
my($alien, $module, $version, @rest) = @_;
@xs_load_args = @_;
require XSLoader;
XSLoader::load($module, $version);
},
],
);
xs_ok { xs => $xs, verbose => 1 }, with_subtest {
my($mod) = @_;
is($mod->get_foo_one, 42, 'get_foo_one');
is($mod->get_foo_two, 47, 'get_foo_two');
};
is(
\@xs_load_args,
array {
item object {
call 'cflags' => '-DFOO_ONE=42';
};
item match(qr{^Test::Alien::XS::Mod});
item '0.01';
item object {
call 'cflags' => '-DFOO_TWO=47';
};
end;
},
'called xs_load with correct args',
);
};
subtest 'acme' => sub {
skip_all 'Test requires Acme::Alien::DontPanic 0.026'
unless eval {
require Acme::Alien::DontPanic;
Acme::Alien::DontPanic->VERSION('0.026');
};
reset();
my $xs = <<'EOF';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <libdontpanic.h>
MODULE = Acme PACKAGE = Acme
int answer();
EOF
alien_ok 'Acme::Alien::DontPanic';
xs_ok $xs, with_subtest {
is Acme::answer(), 42, 'answer is 42';
};
};
};
subtest 'xs_ok without no compiler' => sub {
my $mock = mock 'ExtUtils::CBuilder' => (
'class' => 'ExtUtils::CBuilder',
override => [
have_compiler => sub {
0;
},
],
);
xs_ok '';
xs_ok '', sub {};
is(
intercept { xs_ok '' },
array {
event Skip => sub {
# doesn't seem to be a way of testing
# if an event was skipped
call pass => T();
call name => 'xs';
call effective_pass => T();
};
end;
},
'skip works'
);
is(
intercept { xs_ok '', sub {} },
array {
event Skip => sub {
# doesn't seem to be a way of testing
# if an event was skipped
call pass => T();
call name => 'xs';
call effective_pass => T();
};
event Skip => sub {
# doesn't seem to be a way of testing
# if an event was skipped
call pass => T();
call name => 'xs subtest';
call effective_pass => T();
};
end;
},
'skip works with cb'
);
};
subtest 'overrides no overrides' => sub {
_reset();
alien_ok synthetic { cflags => '-DD1=22' };
my $xs = <<'EOF';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
int
answer(const char *klass)
{
#ifdef D1
#ifdef D2
return D1+D2;
#else
return D1;
#endif
#else
#ifdef D2
return D2;
#else
return 0;
#endif
#endif
}
MODULE = TA_MODULE PACKAGE = TA_MODULE
int answer(klass);
const char *klass;
EOF
xs_ok { xs => $xs, cbuilder_compile => { extra_compiler_flags => '-DD2=20' }, verbose => 1 }, 'extra compiler flags as string', with_subtest {
my($mod) = @_;
is($mod->answer, 42);
};
xs_ok { xs => $xs, cbuilder_compile => { extra_compiler_flags => ['-DD2=20'] }, verbose => 1 }, 'extra compiler flags as array ref', with_subtest {
my($mod) = @_;
is($mod->answer, 42);
};
my $cbuilder_config;
no warnings 'once';
local *ExtUtils::CBuilder::new = do {
my $orig = ExtUtils::CBuilder->can('new');
sub {
my $class = shift;
my %args = @_;
$cbuilder_config = $args{config};
$class->$orig(@_);
};
};
xs_ok { xs => $xs, cbuilder_config => { foo => 'bar' } };
is
$cbuilder_config,
hash {
field 'foo' => 'bar';
etc;
}
;
};
subtest 'with_subtest SEGV' => sub {
# it may be possible to catch a segmentation fault,
# but not with signal handlers apparently. See:
# https://feepingcreature.github.io/handling.html
skip_all 'Catching SEGV not currently supported on Windows' if $^O eq 'MSWin32';
skip_all 'Test requires platforms with SEGV signal' if ! any { $_ eq 'SEGV' } split /\s+/, $Config{sig_name};
our $kill_line;
my $st = with_subtest {
note 'one';
kill 'SEGV', $$; BEGIN { $kill_line = __LINE__ };
note 'two';
};
my $e;
is(
$e = intercept {
$st->();
},
array {
event Note => sub {
call message => 'one';
};
event Bail => sub {
call reason => 'Segmentation fault';
call facet_data => hash {
field trace => hash {
field frame => array {
item 'main';
item __FILE__;
item $kill_line;
etc;
};
etc;
};
etc;
};
etc;
};
end;
},
) or diag _dump($e);
};
subtest 'diagnostic when calling tools without alien_ok' => sub {
_reset();
require_compiler;
my $xs = <<'EOF';
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = TA_MODULE PACKAGE = TA_MODULE
int answer();
CODE:
RETVAL = 1;
EOF
is
intercept { xs_ok { xs => $xs } },
array {
event Note => {} if $ENV{TEST_ALIEN_ALWAYS_KEEP};
event Ok => sub {
call pass => T();
call name => 'xs';
};
event Diag => sub {
call message => 'xs_ok called without any aliens, you may want to call alien_ok';
};
end;
},
'xs_ok displays dignostic sans alien_ok',
;
is
intercept { run_ok [$^X, -e => '1'] },
array {
event Ok => sub {
call pass => T();
call name => match qr/^run/;
};
event Note => sub {};
event Diag => sub {
call message => 'run_ok called without any aliens, you may want to call alien_ok';
};
end;
},
'run_ok displays diagnostic sans alien_ok',
;
is
intercept { run_ok qq{"$^X" -e 1} },
array {
event Ok => sub {
call pass => T();
call name => match qr/^run/;
};
event Note => sub {};
event Diag => sub {
call message => 'run_ok called without any aliens, you may want to call alien_ok';
};
end;
},
'run_ok displays diagnostic sans alien_ok',
;
if(eval { require FFI::Platypus; 1 })
{
is
intercept { ffi_ok; },
array {
event Ok => sub {
call pass => T();
call name => 'ffi';
};
event Diag => sub {
call message => 'ffi_ok called without any aliens, you may want to call alien_ok';
};
end;
},
'ffi_ok displays diagnostic sans alien_ok',
;
}
is
intercept { helper_ok 'foo' },
array {
event Ok => sub {
call pass => F();
call name => 'helper foo exists';
};
event Diag => sub {};
event Diag => sub {
call message => 'helper_ok called without any aliens, you may want to call alien_ok';
};
end;
},
'helper_ok displays diagnostic sans alien_ok',
;
is
intercept { interpolate_template_is "foo", "foo" },
array {
event Ok => sub {
call pass => T();
call name => 'template matches';
};
event Diag => sub {
call message => 'interpolate_template_is called without any aliens, you may want to call alien_ok';
};
end;
},
'interpolate_template_is called without any aliens, you may want to call alien_ok',
;
};
subtest 'interpolate_run_ok' => sub {
_reset();
alien_ok 'Alien::perlhelp';
is
intercept { interpolate_run_ok '%{bogus}' },
array {
event Ok => sub {
call pass => F();
call name => 'run %{bogus}';
};
event Diag => sub {};
event Diag => sub {
call message => 'error in evaluation:';
};
event Diag => sub {
call message => match qr/^ no helper defined for bogus at/;
};
end;
},
'run with bogus macro should fail',
;
interpolate_run_ok ['%{perlhelp}', -e => '1'];
interpolate_run_ok '"%{perlhelp}" -e 1';
};
done_testing;
|