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
CommitLineData
297f4492
S
1#!/usr/bin/perl
2use strict;
3use warnings;
4
48150f65 5use Test::More tests => 43;
7320491e 6use ExtUtils::Typemaps;
297f4492 7
48150f65
S
8# empty typemap
9SCOPE: {
10 ok(ExtUtils::Typemaps->new()->is_empty(), "This is an empty typemap");
11}
12
297f4492
S
13# typemap only
14SCOPE: {
7320491e 15 my $map = ExtUtils::Typemaps->new();
297f4492 16 $map->add_typemap(ctype => 'unsigned int', xstype => 'T_IV');
48150f65
S
17 ok(!$map->is_empty(), "This is not an empty typemap");
18
297f4492
S
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');
7320491e 25 isa_ok($type, 'ExtUtils::Typemaps::Type');
297f4492
S
26 is($type->ctype, 'unsigned int');
27 is($type->xstype, 'T_IV');
28 is($type->tidy_ctype, 'unsigned int');
bb2320f0 29
48150f65 30
bb2320f0
S
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");
297f4492
S
40}
41
42# typemap & input
43SCOPE: {
7320491e 44 my $map = ExtUtils::Typemaps->new();
297f4492 45 $map->add_inputmap(xstype => 'T_UV', code => '$var = ($type)SvUV($arg);');
48150f65
S
46 ok(!$map->is_empty(), "This is not an empty typemap");
47 $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
297f4492
S
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');
7320491e 58 isa_ok($type, 'ExtUtils::Typemaps::Type');
297f4492
S
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');
7320491e 64 isa_ok($in, 'ExtUtils::Typemaps::InputMap');
297f4492 65 is($in->xstype, 'T_UV');
1c4122e7
S
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");
297f4492
S
70}
71
72
73# typemap & output
74SCOPE: {
7320491e 75 my $map = ExtUtils::Typemaps->new();
297f4492 76 $map->add_outputmap(xstype => 'T_UV', code => 'sv_setuv($arg, (UV)$var);');
48150f65
S
77 ok(!$map->is_empty(), "This is not an empty typemap");
78 $map->add_typemap(ctype => 'unsigned int', xstype => 'T_UV');
297f4492
S
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');
7320491e 89 isa_ok($type, 'ExtUtils::Typemaps::Type');
297f4492
S
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');
7320491e 95 isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
297f4492
S
96 is($in->xstype, 'T_UV');
97}
98
99# typemap & input & output
100SCOPE: {
7320491e 101 my $map = ExtUtils::Typemaps->new();
297f4492
S
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);');
48150f65 105 ok(!$map->is_empty(), "This is not an empty typemap");
297f4492
S
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: {
7320491e 122 my $map = ExtUtils::Typemaps->new();
297f4492
S
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');
7320491e 148 isa_ok($type, 'ExtUtils::Typemaps::Type');
297f4492
S
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');
7320491e 154 isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
297f4492
S
155 is($in->xstype, 'T_UV');
156 $in = $map->get_outputmap(xstype => 'T_IV');
7320491e 157 isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
297f4492
S
158 is($in->xstype, 'T_IV');
159}
160