1#!/usr/bin/perl 2use strict; 3use warnings; 4 5use Test::More tests => 6; 6use ExtUtils::Typemaps; 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 23my $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 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->catfile($tmpdir, 'simple.typemap'); 35 36$map->write(file => $tmpfile); 37is($map->as_string(), slurp($tmpfile), "Simple typemap write matches as_string"); 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)"); 40 41SCOPE: { 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 47my $map_from_str = ExtUtils::Typemaps->new(string => $map->as_string()); 48is_deeply($map_from_str, $map); 49 50