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 / 510-t-bare.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 43;
6 use ExtUtils::Typemaps;
7
8 # empty typemap
9 SCOPE: {
10   ok(ExtUtils::Typemaps->new()->is_empty(), "This is an empty typemap");
11 }
12
13 # typemap only
14 SCOPE: {
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");
20 TYPEMAP
21 unsigned int    T_IV
22 HERE
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
43 SCOPE: {
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");
49 TYPEMAP
50 unsigned int    T_UV
51
52 INPUT
53 T_UV
54         $var = ($type)SvUV($arg);
55 HERE
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
74 SCOPE: {
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");
80 TYPEMAP
81 unsigned int    T_UV
82
83 OUTPUT
84 T_UV
85         sv_setuv($arg, (UV)$var);
86 HERE
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
100 SCOPE: {
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");
107 TYPEMAP
108 unsigned int    T_UV
109
110 INPUT
111 T_UV
112         $var = ($type)SvUV($arg);
113
114 OUTPUT
115 T_UV
116         sv_setuv($arg, (UV)$var);
117 HERE
118 }
119
120 # two typemaps & input & output
121 SCOPE: {
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");
131 TYPEMAP
132 unsigned int    T_UV
133 int     T_IV
134
135 INPUT
136 T_UV
137         $var = ($type)SvUV($arg);
138 T_IV
139         $var = ($type)SvIV($arg);
140
141 OUTPUT
142 T_UV
143         sv_setuv($arg, (UV)$var);
144 T_IV
145         sv_setiv($arg, (IV)$var);
146 HERE
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