This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ParseXS - better support for duplicate ALIASes
[perl5.git] / dist / ExtUtils-ParseXS / t / 512-t-file.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 6;
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 $cmp_typemap_file = File::Spec->catfile($datadir, 'simple.typemap');
21 my $cmp_typemap_str  = slurp($cmp_typemap_file);
22
23 my $map = ExtUtils::Typemaps->new();
24 $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
25 $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
26 $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
27 $map->add_typemap(ctype => 'int', xstype => 'T_IV');
28 $map->add_inputmap(xstype => 'T_IV', code => '$var = ($type)SvIV($arg);');
29 $map->add_outputmap(xstype => 'T_IV', code => 'sv_setiv($arg, (IV)$var);');
30
31 is($map->as_string(), $cmp_typemap_str, "Simple typemap matches reference file");
32
33 my $tmpdir = File::Temp::tempdir(CLEANUP => 1, TMPDIR => 1);
34 my $tmpfile = File::Spec->catfile($tmpdir, 'simple.typemap');
35
36 $map->write(file => $tmpfile);
37 is($map->as_string(), slurp($tmpfile), "Simple typemap write matches as_string");
38 is(ExtUtils::Typemaps->new(file => $cmp_typemap_file)->as_string(), $cmp_typemap_str, "Simple typemap roundtrips");
39 is(ExtUtils::Typemaps->new(file => $tmpfile)->as_string(), $cmp_typemap_str, "Simple typemap roundtrips (2)");
40
41 SCOPE: {
42   local $map->{file} = $cmp_typemap_file;
43   is_deeply(ExtUtils::Typemaps->new(file => $cmp_typemap_file), $map, "Simple typemap roundtrips (in memory)");
44 }
45
46 # test that we can also create them from a string
47 my $map_from_str = ExtUtils::Typemaps->new(string => $map->as_string());
48 is_deeply($map_from_str, $map);
49