5 use Test::More tests => 43;
6 use ExtUtils::Typemaps;
10 ok(ExtUtils::Typemaps->new()->is_empty(), "This is an empty typemap");
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");
19 is($map->as_string(), <<'HERE', "Simple typemap matches expectations");
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');
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");
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");
54 $var = ($type)SvUV($arg);
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');
63 my $in = $map->get_inputmap(xstype => 'T_UV');
64 isa_ok($in, 'ExtUtils::Typemaps::InputMap');
65 is($in->xstype, 'T_UV');
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");
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");
85 sv_setuv($arg, (UV)$var);
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');
94 my $in = $map->get_outputmap(xstype => 'T_UV');
95 isa_ok($in, 'ExtUtils::Typemaps::OutputMap');
96 is($in->xstype, 'T_UV');
99 # typemap & input & output
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");
112 $var = ($type)SvUV($arg);
116 sv_setuv($arg, (UV)$var);
120 # two typemaps & input & output
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);');
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");
137 $var = ($type)SvUV($arg);
139 $var = ($type)SvIV($arg);
143 sv_setuv($arg, (UV)$var);
145 sv_setiv($arg, (IV)$var);
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');
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');