This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Attempt band-aid fix for win32 build failure
[perl5.git] / dist / ExtUtils-ParseXS / t / 512-t-file.t
CommitLineData
297f4492
S
1#!/usr/bin/perl
2use strict;
3use warnings;
4
5use Test::More tests => 6;
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
20my $cmp_typemap_file = File::Spec->catfile($datadir, 'simple.typemap');
21my $cmp_typemap_str = slurp($cmp_typemap_file);
22
7320491e 23my $map = ExtUtils::Typemaps->new();
297f4492
S
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
31is($map->as_string(), $cmp_typemap_str, "Simple typemap matches reference file");
32
33my $tmpdir = File::Temp::tempdir(CLEANUP => 1, TMPDIR => 1);
34my $tmpfile = File::Spec->catdir($tmpdir, 'simple.typemap');
35
36$map->write(file => $tmpfile);
37is($map->as_string(), slurp($tmpfile), "Simple typemap write matches as_string");
7320491e
S
38is(ExtUtils::Typemaps->new(file => $cmp_typemap_file)->as_string(), $cmp_typemap_str, "Simple typemap roundtrips");
39is(ExtUtils::Typemaps->new(file => $tmpfile)->as_string(), $cmp_typemap_str, "Simple typemap roundtrips (2)");
297f4492
S
40
41SCOPE: {
42 local $map->{file} = $cmp_typemap_file;
7320491e 43 is_deeply(ExtUtils::Typemaps->new(file => $cmp_typemap_file), $map, "Simple typemap roundtrips (in memory)");
297f4492
S
44}
45
46# test that we can also create them from a string
7320491e 47my $map_from_str = ExtUtils::Typemaps->new(string => $map->as_string());
297f4492
S
48is_deeply($map_from_str, $map);
49