summaryrefslogtreecommitdiff
path: root/t/lib/MakeMaker/Test/Setup/XS.pm
blob: 195fd56feb04f828460b3f2740d7b8968441c064 (plain)
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
package MakeMaker::Test::Setup::XS;

@ISA = qw(Exporter);
require Exporter;
@EXPORT = qw(setup_xs teardown_xs);

use strict;
use File::Path;
use File::Basename;
use MakeMaker::Test::Utils;

my $Is_VMS = $^O eq 'VMS';

my %Files = (
             'XS-Test/lib/XS/Test.pm'     => <<'END',
package XS::Test;

require Exporter;
require DynaLoader;

$VERSION = 1.01;
@ISA    = qw(Exporter DynaLoader);
@EXPORT = qw(is_even);

bootstrap XS::Test $VERSION;

1;
END

             'XS-Test/Makefile.PL'          => <<'END',
use ExtUtils::MakeMaker;

WriteMakefile(
    NAME          => 'XS::Test',
    VERSION_FROM  => 'lib/XS/Test.pm',
);
END

             'XS-Test/Test.xs'              => <<'END',
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

MODULE = XS::Test       PACKAGE = XS::Test

PROTOTYPES: DISABLE

int
is_even(input)
       int     input
   CODE:
       RETVAL = (input % 2 == 0);
   OUTPUT:
       RETVAL        
END

             'XS-Test/t/is_even.t'          => <<'END',
#!/usr/bin/perl -w

use Test::More tests => 3;

use_ok "XS::Test";
ok !is_even(1);
ok is_even(2);
END
            );


sub setup_xs {
    setup_mm_test_root();
    chdir 'MM_TEST_ROOT:[t]' if $Is_VMS;

    while(my($file, $text) = each %Files) {
        # Convert to a relative, native file path.
        $file = File::Spec->catfile(File::Spec->curdir, split m{\/}, $file);

        my $dir = dirname($file);
        mkpath $dir;
        open(FILE, ">$file") || die "Can't create $file: $!";
        print FILE $text;
        close FILE;
    }

    return 1;
}

sub teardown_xs { 
    foreach my $file (keys %Files) {
        my $dir = dirname($file);
        if( -e $dir ) {
            rmtree($dir) || return;
        }
    }
    return 1;
}

1;