| 1 | #!/usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use Test::More tests => 15; |
| 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 | |
| 26 | # test merging two typemaps |
| 27 | SCOPE: { |
| 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'); |
| 32 | |
| 33 | $first->merge(typemap => $second); |
| 34 | |
| 35 | is($first->as_string(), slurp($combined_typemap_file), "merging produces expected output"); |
| 36 | } |
| 37 | |
| 38 | # test merging a typemap from file |
| 39 | SCOPE: { |
| 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 |
| 50 | SCOPE: { |
| 51 | my $first = ExtUtils::Typemaps->new(file => $first_typemap_file); |
| 52 | isa_ok($first, 'ExtUtils::Typemaps'); |
| 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 | } |
| 59 | |
| 60 | # test merging a conflicting typemap without "replace" |
| 61 | SCOPE: { |
| 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" |
| 81 | SCOPE: { |
| 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 | |