1#!perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 44;
7
8use_ok('XS::APItest');
9
10my $level = -1;
11my @types = map { 'gv_fetchmeth' . $_ } '', qw( _sv _pv _pvn );
12
13sub test { "Sanity check" }
14
15for my $type ( 0..3 ) {
16    is *{
17         XS::APItest::gv_fetchmeth_type(\%::, "test", $type, $level, 0)
18        }{CODE}->(), "Sanity check";
19}
20
21for my $type ( 0..3 ) {
22    my $meth = "gen$type";
23    ok !XS::APItest::gv_fetchmeth_type(\%::, $meth, $type, -1, 0), "With level = -1, $types[$type] returns false.";
24    ok !$::{$meth}, "...and doesn't vivify the glob.";
25
26    ok !XS::APItest::gv_fetchmeth_type(\%::, $meth, $type, 0, 0), "With level = 0, $types[$type] still returns false.";
27    ok $::{$meth}, "...but does vivify the glob.";
28}
29
30{
31    no warnings 'once';
32    *method = sub { 1 };
33}
34
35ok !XS::APItest::gv_fetchmeth_type(\%::, "method\0not quite!", 0, $level, 0), "gv_fetchmeth() is nul-clean";
36ok !XS::APItest::gv_fetchmeth_type(\%::, "method\0not quite!", 1, $level, 0), "gv_fetchmeth_sv() is nul-clean";
37is XS::APItest::gv_fetchmeth_type(\%::, "method\0not quite!", 2, $level, 0), "*main::method", "gv_fetchmeth_pv() is not nul-clean";
38ok !XS::APItest::gv_fetchmeth_type(\%::, "method\0not quite!", 3, $level, 0), "gv_fetchmeth_pvn() is nul-clean";
39
40{
41    use utf8;
42    use open qw( :utf8 :std );
43
44    package ������������;
45
46    sub ������������������ { 1 }
47
48    use constant { ������1 => 1,
49                   ������2 => 2,
50                   ������3 => 3, };
51
52    my $meth_as_octets =
53            "\357\275\215\357\275\205\357\275\224\357\275\210\357\275\217\357\275\204";
54
55    $level = 1;
56    for my $type ( 1..3 ) {
57        ::is XS::APItest::gv_fetchmeth_type(\%������������::, "������������������", $type, $level, 0), "*������������::������������������", "$types[$type] is UTF-8 clean";
58        ::ok !XS::APItest::gv_fetchmeth_type(\%������������::, $meth_as_octets, $type, $level, 0);
59        ::ok !XS::APItest::gv_fetchmeth_type(\%������������::, "method", $type, $level, 0);
60        ::is XS::APItest::gv_fetchmeth_type(\%������������::, "������$type", $type, $level, 0), "*������������::������$type", "$types[$type] can fetch UTF-8 constant";
61        
62        {
63            no strict 'refs';
64            ::ok !XS::APItest::gv_fetchmeth_type(
65                            \%{"\357\275\215\357\275\201\357\275\211\357\275\216::"},
66                            "������������������", $type, $level, 0);
67            ::ok !XS::APItest::gv_fetchmeth_type(
68                            \%{"\357\275\215\357\275\201\357\275\211\357\275\216::"},
69                            "method", $type, $level, 0);
70        }
71    }
72}
73
74{
75    @Foo::ISA = qw/Bar/;
76    @Bar::ISA = qw//;
77
78    is(XS::APItest::gv_fetchmeth_type(\%Foo::, "nomethod", 1, -1, 0), undef, 'gv_fetchmeth_sv survives @ISA traversal');
79}
80