Affix

 view release on metacpan or  search on metacpan

lib/Test2/Tools/Affix.pm  view on Meta::CPAN

package    #
    Test2::Tools::Affix v0.12.0 {
    use v5.40;
    use blib;
    use Affix;
    use Affix::Build;
    use Test2::API qw[context run_subtest];
    use Test2::V0 -no_srand => 1, '!subtest';
    use Test2::Util::Importer 'Test2::Tools::Subtest' => ( subtest_streamed => { -as => 'subtest' } );
    use Test2::Plugin::UTF8;
    use Test2::IPC;
    use Path::Tiny qw[path tempfile];
    use Exporter 'import';
    use Capture::Tiny ':all';
    our @CARP_NOT;
    our %EXPORT_TAGS = (
        all => [
            our @EXPORT
                = qw[
                compile_ok affix_ok leaks
                plan todo skip skip_all done_testing diag note
                subtest ok isa_ok skip_all is isnt like
                pass fail
                lives dies try_ok warns warning
                U D T F DNE array string float number bool hash etc end
                refcount
                can_ok isa_ok
                capture imported_ok warns
                path tempfile tempdir]
        ]
    );
    #
    my $OS  = $^O;
    my $Inc = path($0)->absolute;
    $Inc = $Inc->parent while !$Inc->child('t')->is_dir;
    $Inc = $Inc->child( 't', 'src' );
    my @cleanup;

    END {
        for my $file ( grep {-f} @cleanup ) {
            unlink $file;
        }
        for my $dir ( grep {-d} @cleanup ) {
            $dir->remove_tree;
        }
    }
    #
    sub compile_ok( $name, $aggs //= {}, $keep //= 0 ) {
        my $c = context();

        #~ return $c->pass_and_release($name) if 1;
        #~ return $c->fail_and_release($name, @diag);
        my ($opt) = grep { -f $_ } "t/src/$name.cxx", "t/src/$name.c", "src/$name.cxx", "src/$name.c";
        if ($opt) {
            $opt = path($opt)->absolute;
        }
        else {
            $opt = tempfile(
                UNLINK => !$keep,
                SUFFIX => '_' . path( [ caller() ]->[1] )->basename . ( $name =~ m[^\s*//\s*ext:\s*\.c$]ms ? '.c' : '.cxx' )
            )->absolute;
            push @cleanup, $opt unless $keep;
            my ( $package, $filename, $line ) = caller;
            $filename = path($filename)->canonpath;
            $line++;
            $filename =~ s[\\][\\\\]g;    # Windows...
            $opt->spew_utf8(qq[#line $line "$filename"\r\n$name]);
        }
        if ( !$opt ) {
            $c->fail('Failed to locate test source');
            $c->release;
            return ();
        }
        $aggs->{cflags} .= ' -I' . $Inc;
        my $compiler = Affix::Build->new( debug => 0, name => 'testing', version => '1.0', flags => $aggs );
        $compiler->add( $opt->canonpath );
        $compiler->link;
        push @cleanup, $opt->canonpath, $compiler->link unless $keep;
        $c->ok( 1, 'build lib: ' . $compiler->link );
        $c->release;
        $compiler->link;
    }

    sub affix_ok ( $lib, $name, $args, $ret ) {
        my $c = context;
        my $sub;
        diag __PACKAGE__;
        diag join ', ', caller;
        my $okay = run_subtest(
            'affix ' . $name . '( ... )',
            sub {
                ok lives {
                    $sub = affix( $lib, $name, $args, $ret )
                }, 'affix ' . $name . ' ...';
                isa_ok $sub, ['Affix'], $name;



( run in 1.106 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )