1#!/usr/bin/perl
2use strict;
3use warnings;
4
5use Test::More tests => 43;
6use ExtUtils::Typemaps;
7
8# empty typemap
9SCOPE: {
10  ok(ExtUtils::Typemaps->new()->is_empty(), "This is an empty typemap");
11}
12
13# typemap only
14SCOPE: {
15  my $map = ExtUtils::Typemaps->new();
16  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_IV');
17  ok(!$map->is_empty(), "This is not an empty typemap");
18
19  is($map->as_string(), <<'HERE', "Simple typemap matches expectations");
20TYPEMAP
21unsigned int	T_IV
22HERE
23
24  my $type = $map->get_typemap(ctype => 'unsigned int');
25  isa_ok($type, 'ExtUtils::Typemaps::Type');
26  is($type->ctype, 'unsigned int');
27  is($type->xstype, 'T_IV');
28  is($type->tidy_ctype, 'unsigned int');
29
30
31  # test failure
32  ok(!$map->get_typemap(ctype => 'foo'), "Access to nonexistent typemap doesn't die");
33  ok(!$map->get_inputmap(ctype => 'foo'), "Access to nonexistent inputmap via ctype doesn't die");
34  ok(!$map->get_outputmap(ctype => 'foo'), "Access to nonexistent outputmap via ctype doesn't die");
35  ok(!$map->get_inputmap(xstype => 'foo'), "Access to nonexistent inputmap via xstype doesn't die");
36  ok(!$map->get_outputmap(xstype => 'foo'), "Access to nonexistent outputmap via xstype doesn't die");
37  ok(!eval{$map->get_typemap('foo')} && $@, "Access to typemap with positional params dies");
38  ok(!eval{$map->get_inputmap('foo')} && $@, "Access to inputmap with positional params dies");
39  ok(!eval{$map->get_outputmap('foo')} && $@, "Access to outputmap with positional params dies");
40}
41
42# typemap & input
43SCOPE: {
44  my $map = ExtUtils::Typemaps->new();
45  $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
46  ok(!$map->is_empty(), "This is not an empty typemap");
47  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
48  is($map->as_string(), <<'HERE', "Simple typemap (with input) matches expectations");
49TYPEMAP
50unsigned int	T_UV
51
52INPUT
53T_UV
54	$var = ($type)SvUV($arg);
55HERE
56
57  my $type = $map->get_typemap(ctype => 'unsigned int');
58  isa_ok($type, 'ExtUtils::Typemaps::Type');
59  is($type->ctype, 'unsigned int');
60  is($type->xstype, 'T_UV');
61  is($type->tidy_ctype, 'unsigned int');
62
63  my $in = $map->get_inputmap(xstype => 'T_UV');
64  isa_ok($in, 'ExtUtils::Typemaps::InputMap');
65  is($in->xstype, 'T_UV');
66
67  # test fetching inputmap by ctype
68  my $in2 = $map->get_inputmap(ctype => 'unsigned int');
69  is_deeply($in2, $in, "get_inputmap returns the same typemap for ctype and xstype");
70}
71
72
73# typemap & output
74SCOPE: {
75  my $map = ExtUtils::Typemaps->new();
76  $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
77  ok(!$map->is_empty(), "This is not an empty typemap");
78  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
79  is($map->as_string(), <<'HERE', "Simple typemap (with output) matches expectations");
80TYPEMAP
81unsigned int	T_UV
82
83OUTPUT
84T_UV
85	sv_setuv($arg, (UV)$var);
86HERE
87
88  my $type = $map->get_typemap(ctype => 'unsigned int');
89  isa_ok($type, 'ExtUtils::Typemaps::Type');
90  is($type->ctype, 'unsigned int');
91  is($type->xstype, 'T_UV');
92  is($type->tidy_ctype, 'unsigned int');
93
94  my $in = $map->get_outputmap(xstype => 'T_UV');
95  isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
96  is($in->xstype, 'T_UV');
97}
98
99# typemap & input & output
100SCOPE: {
101  my $map = ExtUtils::Typemaps->new();
102  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
103  $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
104  $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
105  ok(!$map->is_empty(), "This is not an empty typemap");
106  is($map->as_string(), <<'HERE', "Simple typemap (with in- & output) matches expectations");
107TYPEMAP
108unsigned int	T_UV
109
110INPUT
111T_UV
112	$var = ($type)SvUV($arg);
113
114OUTPUT
115T_UV
116	sv_setuv($arg, (UV)$var);
117HERE
118}
119
120# two typemaps & input & output
121SCOPE: {
122  my $map = ExtUtils::Typemaps->new();
123  $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
124  $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
125  $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
126
127  $map->add_typemap(ctype => 'int', xstype => 'T_IV');
128  $map->add_inputmap(xstype => 'T_IV', code => '$var = ($type)SvIV($arg);');
129  $map->add_outputmap(xstype => 'T_IV', code => 'sv_setiv($arg, (IV)$var);');
130  is($map->as_string(), <<'HERE', "Simple typemap (with in- & output) matches expectations");
131TYPEMAP
132unsigned int	T_UV
133int	T_IV
134
135INPUT
136T_UV
137	$var = ($type)SvUV($arg);
138T_IV
139	$var = ($type)SvIV($arg);
140
141OUTPUT
142T_UV
143	sv_setuv($arg, (UV)$var);
144T_IV
145	sv_setiv($arg, (IV)$var);
146HERE
147  my $type = $map->get_typemap(ctype => 'unsigned int');
148  isa_ok($type, 'ExtUtils::Typemaps::Type');
149  is($type->ctype, 'unsigned int');
150  is($type->xstype, 'T_UV');
151  is($type->tidy_ctype, 'unsigned int');
152
153  my $in = $map->get_outputmap(xstype => 'T_UV');
154  isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
155  is($in->xstype, 'T_UV');
156  $in = $map->get_outputmap(xstype => 'T_IV');
157  isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
158  is($in->xstype, 'T_IV');
159}
160
161