This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Implement 'replace' option when merging typemaps
[perl5.git] / dist / ExtUtils-ParseXS / t / 513-t-merge.t
CommitLineData
297f4492
S
1#!/usr/bin/perl
2use strict;
3use warnings;
4
8226b442 5use Test::More tests => 15;
7320491e 6use ExtUtils::Typemaps;
297f4492
S
7use File::Spec;
8use File::Temp;
9
10my $datadir = -d 't' ? File::Spec->catdir(qw/t data/) : 'data';
11
12sub 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
8226b442
S
20my $first_typemap_file = File::Spec->catfile($datadir, 'simple.typemap');
21my $second_typemap_file = File::Spec->catfile($datadir, 'other.typemap');
22my $combined_typemap_file = File::Spec->catfile($datadir, 'combined.typemap');
23my $conflicting_typemap_file = File::Spec->catfile($datadir, 'conflicting.typemap');
24my $confl_replace_typemap_file = File::Spec->catfile($datadir, 'confl_repl.typemap');
297f4492 25
8226b442 26# test merging two typemaps
297f4492 27SCOPE: {
7320491e
S
28 my $first = ExtUtils::Typemaps->new(file => $first_typemap_file);
29 isa_ok($first, 'ExtUtils::Typemaps');
30 my $second = ExtUtils::Typemaps->new(file => $second_typemap_file);
31 isa_ok($second, 'ExtUtils::Typemaps');
297f4492
S
32
33 $first->merge(typemap => $second);
34
35 is($first->as_string(), slurp($combined_typemap_file), "merging produces expected output");
36}
37
8226b442
S
38# test merging a typemap from file
39SCOPE: {
40 my $first = ExtUtils::Typemaps->new(file => $first_typemap_file);
41 isa_ok($first, 'ExtUtils::Typemaps');
42
43 $first->merge(file => $second_typemap_file);
44
45 is($first->as_string(), slurp($combined_typemap_file), "merging produces expected output");
46}
47
48
49# test merging a typemap as string
297f4492 50SCOPE: {
7320491e
S
51 my $first = ExtUtils::Typemaps->new(file => $first_typemap_file);
52 isa_ok($first, 'ExtUtils::Typemaps');
297f4492
S
53 my $second_str = slurp($second_typemap_file);
54
55 $first->add_string(string => $second_str);
56
57 is($first->as_string(), slurp($combined_typemap_file), "merging (string) produces expected output");
58}
8226b442
S
59
60# test merging a conflicting typemap without "replace"
61SCOPE: {
62 my $second = ExtUtils::Typemaps->new(file => $second_typemap_file);
63 isa_ok($second, 'ExtUtils::Typemaps');
64 my $conflict = ExtUtils::Typemaps->new(file => $conflicting_typemap_file);
65 isa_ok($conflict, 'ExtUtils::Typemaps');
66
67 ok(
68 !eval {
69 $second->merge(typemap => $conflict);
70 1;
71 },
72 "Merging conflicting typemap croaks"
73 );
74 ok(
75 $@ =~ /Multiple definition/,
76 "Conflicting typemap error as expected"
77 );
78}
79
80# test merging a conflicting typemap with "replace"
81SCOPE: {
82 my $second = ExtUtils::Typemaps->new(file => $second_typemap_file);
83 isa_ok($second, 'ExtUtils::Typemaps');
84 my $conflict = ExtUtils::Typemaps->new(file => $conflicting_typemap_file);
85 isa_ok($conflict, 'ExtUtils::Typemaps');
86
87 ok(
88 eval {
89 $second->merge(typemap => $conflict, replace => 1);
90 1;
91 },
92 "Conflicting typemap merge with replace doesn't croak"
93 );
94
95 is($second->as_string(), slurp($confl_replace_typemap_file), "merging (string) produces expected output");
96}
97
98
99