| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use Test::More tests => 19; |
| 6 |
use ExtUtils::Typemaps; |
| 7 |
use File::Spec; |
| 8 |
use File::Temp; |
| 9 |
|
| 10 |
my $datadir = -d 't' ? File::Spec->catdir(qw/t data/) : 'data'; |
| 11 |
|
| 12 |
sub slurp { |
| 13 |
my $file = shift; |
| 14 |
open my $fh, '<', $file |
| 15 |
or die "Cannot open file '$file' for reading: $!"; |
| 16 |
local $/ = undef; |
| 17 |
return <$fh>; |
| 18 |
} |
| 19 |
|
| 20 |
my $first_typemap_file = File::Spec->catfile($datadir, 'simple.typemap'); |
| 21 |
my $second_typemap_file = File::Spec->catfile($datadir, 'other.typemap'); |
| 22 |
my $combined_typemap_file = File::Spec->catfile($datadir, 'combined.typemap'); |
| 23 |
my $conflicting_typemap_file = File::Spec->catfile($datadir, 'conflicting.typemap'); |
| 24 |
my $confl_replace_typemap_file = File::Spec->catfile($datadir, 'confl_repl.typemap'); |
| 25 |
my $confl_skip_typemap_file = File::Spec->catfile($datadir, 'confl_skip.typemap'); |
| 26 |
|
| 27 |
# test merging two typemaps |
| 28 |
SCOPE: { |
| 29 |
my $first = ExtUtils::Typemaps->new(file => $first_typemap_file); |
| 30 |
isa_ok($first, 'ExtUtils::Typemaps'); |
| 31 |
my $second = ExtUtils::Typemaps->new(file => $second_typemap_file); |
| 32 |
isa_ok($second, 'ExtUtils::Typemaps'); |
| 33 |
|
| 34 |
$first->merge(typemap => $second); |
| 35 |
|
| 36 |
is($first->as_string(), slurp($combined_typemap_file), "merging produces expected output"); |
| 37 |
} |
| 38 |
|
| 39 |
# test merging a typemap from file |
| 40 |
SCOPE: { |
| 41 |
my $first = ExtUtils::Typemaps->new(file => $first_typemap_file); |
| 42 |
isa_ok($first, 'ExtUtils::Typemaps'); |
| 43 |
|
| 44 |
$first->merge(file => $second_typemap_file); |
| 45 |
|
| 46 |
is($first->as_string(), slurp($combined_typemap_file), "merging produces expected output"); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
# test merging a typemap as string |
| 51 |
SCOPE: { |
| 52 |
my $first = ExtUtils::Typemaps->new(file => $first_typemap_file); |
| 53 |
isa_ok($first, 'ExtUtils::Typemaps'); |
| 54 |
my $second_str = slurp($second_typemap_file); |
| 55 |
|
| 56 |
$first->add_string(string => $second_str); |
| 57 |
|
| 58 |
is($first->as_string(), slurp($combined_typemap_file), "merging (string) produces expected output"); |
| 59 |
} |
| 60 |
|
| 61 |
# test merging a conflicting typemap without "replace" |
| 62 |
SCOPE: { |
| 63 |
my $second = ExtUtils::Typemaps->new(file => $second_typemap_file); |
| 64 |
isa_ok($second, 'ExtUtils::Typemaps'); |
| 65 |
my $conflict = ExtUtils::Typemaps->new(file => $conflicting_typemap_file); |
| 66 |
isa_ok($conflict, 'ExtUtils::Typemaps'); |
| 67 |
|
| 68 |
ok( |
| 69 |
!eval { |
| 70 |
$second->merge(typemap => $conflict); |
| 71 |
1; |
| 72 |
}, |
| 73 |
"Merging conflicting typemap croaks" |
| 74 |
); |
| 75 |
ok( |
| 76 |
$@ =~ /Multiple definition/, |
| 77 |
"Conflicting typemap error as expected" |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
# test merging a conflicting typemap with "replace" |
| 82 |
SCOPE: { |
| 83 |
my $second = ExtUtils::Typemaps->new(file => $second_typemap_file); |
| 84 |
isa_ok($second, 'ExtUtils::Typemaps'); |
| 85 |
my $conflict = ExtUtils::Typemaps->new(file => $conflicting_typemap_file); |
| 86 |
isa_ok($conflict, 'ExtUtils::Typemaps'); |
| 87 |
|
| 88 |
ok( |
| 89 |
eval { |
| 90 |
$second->merge(typemap => $conflict, replace => 1); |
| 91 |
1; |
| 92 |
}, |
| 93 |
"Conflicting typemap merge with 'replace' doesn't croak" |
| 94 |
); |
| 95 |
|
| 96 |
is($second->as_string(), slurp($confl_replace_typemap_file), "merging (string) produces expected output"); |
| 97 |
} |
| 98 |
|
| 99 |
# test merging a conflicting typemap file with "skip" |
| 100 |
SCOPE: { |
| 101 |
my $second = ExtUtils::Typemaps->new(file => $second_typemap_file); |
| 102 |
isa_ok($second, 'ExtUtils::Typemaps'); |
| 103 |
my $conflict = ExtUtils::Typemaps->new(file => $conflicting_typemap_file); |
| 104 |
isa_ok($conflict, 'ExtUtils::Typemaps'); |
| 105 |
|
| 106 |
ok( |
| 107 |
eval { |
| 108 |
$second->merge(typemap => $conflict, skip => 1); |
| 109 |
1; |
| 110 |
}, |
| 111 |
"Conflicting typemap merge with 'skip' doesn't croak" |
| 112 |
); |
| 113 |
|
| 114 |
is($second->as_string(), slurp($confl_skip_typemap_file), "merging (string) produces expected output"); |
| 115 |
} |
| 116 |
|