1#!/usr/bin/perl
2use strict;
3use warnings;
4
5# tests for the quick-n-dirty interface for XS inclusion
6
7use Test::More tests => 6;
8use File::Spec;
9use ExtUtils::Typemaps::Cmd;
10
11my $datadir = -d 't' ? File::Spec->catdir(qw/t data/) : 'data';
12my $libdir = -d 't' ? File::Spec->catdir(qw/t lib/) : 'lib';
13
14unshift @INC, $libdir;
15
16sub slurp {
17  my $file = shift;
18  open my $fh, '<', $file
19    or die "Cannot open file '$file' for reading: $!";
20  local $/ = undef;
21  return <$fh>;
22}
23
24sub permute (&@) {
25  my $code = shift;
26  my @idx = 0..$#_;
27  while ( $code->(@_[@idx]) ) {
28    my $p = $#idx;
29    --$p while $idx[$p-1] > $idx[$p];
30    my $q = $p or return;
31    push @idx, reverse splice @idx, $p;
32    ++$q while $idx[$p-1] > $idx[$q];
33    @idx[$p-1,$q]=@idx[$q,$p-1];
34  }
35}
36
37
38SCOPE: {
39  no warnings 'once';
40  ok(defined(*embeddable_typemap{CODE}), "function exported");
41}
42
43my $start = "TYPEMAP: <<END_TYPEMAP;\n";
44my $end = "\nEND_TYPEMAP\n";
45is(
46  embeddable_typemap(),
47  "${start}TYPEMAP\n$end",
48  "empty call to embeddable_typemap"
49);
50
51my $typemap_file = File::Spec->catfile($datadir, "simple.typemap");
52is(
53  embeddable_typemap($typemap_file),
54  $start . slurp($typemap_file) . $end,
55  "embeddable typemap from file"
56);
57
58my $foo_content = <<HERE;
59TYPEMAP
60myfoo*	T_PV
61HERE
62is(
63  embeddable_typemap("TypemapTest::Foo"),
64  "$start$foo_content$end",
65  "embeddable typemap from full module name"
66);
67
68
69my $test_content = <<HERE;
70TYPEMAP
71mytype*	T_SV
72HERE
73is(
74  embeddable_typemap("Test"),
75  "$start$test_content$end",
76  "embeddable typemap from relative module name"
77);
78
79SCOPE: {
80  my $combined = embeddable_typemap("Test", "TypemapTest::Foo");
81  my @lines = (
82    'myfoo*	T_PV',
83    'mytype*	T_SV',
84  );
85  my @exp = map {"TYPEMAP\n" . join("\n", @$_) . "\n"}
86            (\@lines, [reverse @lines]);
87  ok(scalar(grep "$start$_$end" eq $combined, @exp), "combined both modules")
88    or note("Actual output: '$combined'");
89}
90
91# in theory, we should test
92# embeddable_typemap($typemap_file, "Test", "TypemapTest::Foo"),
93# but I can't be bothered.
94