This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Implement 'skip' option for merging typemaps
[perl5.git] / dist / ExtUtils-ParseXS / t / 513-t-merge.t
CommitLineData
297f4492
S
1#!/usr/bin/perl
2use strict;
3use warnings;
4
8badd275 5use Test::More tests => 19;
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');
8badd275 25my $confl_skip_typemap_file = File::Spec->catfile($datadir, 'confl_skip.typemap');
297f4492 26
8226b442 27# test merging two typemaps
297f4492 28SCOPE: {
7320491e
S
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');
297f4492
S
33
34 $first->merge(typemap => $second);
35
36 is($first->as_string(), slurp($combined_typemap_file), "merging produces expected output");
37}
38
8226b442
S
39# test merging a typemap from file
40SCOPE: {
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
297f4492 51SCOPE: {
7320491e
S
52 my $first = ExtUtils::Typemaps->new(file => $first_typemap_file);
53 isa_ok($first, 'ExtUtils::Typemaps');
297f4492
S
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}
8226b442
S
60
61# test merging a conflicting typemap without "replace"
62SCOPE: {
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"
82SCOPE: {
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 },
8badd275 93 "Conflicting typemap merge with 'replace' doesn't croak"
8226b442
S
94 );
95
96 is($second->as_string(), slurp($confl_replace_typemap_file), "merging (string) produces expected output");
97}
98
8badd275
S
99# test merging a conflicting typemap file with "skip"
100SCOPE: {
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 );
8226b442 113
8badd275
S
114 is($second->as_string(), slurp($confl_skip_typemap_file), "merging (string) produces expected output");
115}
8226b442 116