4 # test method calls and autoloading.
10 set_up_inc( qw(. ../lib ../cpan/parent/lib) );
11 require './charset_tools.pl';
16 use open qw( :utf8 :std );
21 #Can't use bless yet, as it might not be clean
23 sub F::b { ::is shift, "F"; "UTF8 meth" }
24 sub F::b { ::is shift, "F"; "UTF8 Stash" }
25 sub F::b { ::is shift, "F"; "UTF8 Stash&meth" }
27 is(F->b, "UTF8 meth", "If the method is in UTF-8, lookup works through explicitly named methods");
28 is(F->${\"b"}, "UTF8 meth", '..as does for ->${\""}');
29 eval { F->${\"b\0nul"} };
30 ok $@, "If the method is in UTF-8, lookup is nul-clean";
32 is(F->b, "UTF8 Stash", "If the stash is in UTF-8, lookup works through explicitly named methods");
33 is(F->${\"b"}, "UTF8 Stash", '..as does for ->${\""}');
34 eval { F->${\"b\0nul"} };
35 ok $@, "If the stash is in UTF-8, lookup is nul-clean";
37 is(F->b, "UTF8 Stash&meth", "If both stash and method are in UTF-8, lookup works through explicitly named methods");
38 is(F->${\"b"}, "UTF8 Stash&meth", '..as does for ->${\""}');
39 eval { F->${\"b\0nul"} };
40 ok $@, "Even if both stash and method are in UTF-8, lookup is nul-clean";
42 eval { my $ref = \my $var; $ref->method };
43 like $@, qr/Can't call method "method" on unblessed reference /u;
47 use open qw( :utf8 :std );
51 eval '$e = bless {}, "E::A"; E::A->foo()';
52 like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/u);
53 eval '$e = bless {}, "E::B"; $e->foo()';
54 like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/u);
56 like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /u);
58 eval 'UNIVERSAL->E::D::foo()';
59 like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /u);
60 eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
61 like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /u);
63 $e = bless {}, "E::F"; # force package to exist
64 eval 'UNIVERSAL->E::F::foo()';
65 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/u);
66 eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
67 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/u);
70 is(do { use utf8; use open qw( :utf8 :std ); eval 'Foo->boogie()';
71 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps /u ? 1 : $@}, 1);
73 #This reimplements a bit of _fresh_perl() from test.pl, as we want to decode
74 #the output of that program before using it.
76 skip_if_miniperl('no dynamic loading on miniperl, no Encode');
78 my $prog = q!use utf8; use open qw( :utf8 :std ); sub T::DESTROY { $x = $_[0]; } bless [], "T";!;
81 my $tmpfile = tempfile();
82 my $runperl_args = {};
83 $runperl_args->{progfile} = $tmpfile;
84 $runperl_args->{stderr} = 1;
86 open TEST, '>', $tmpfile or die "Cannot open $tmpfile: $!";
89 close TEST or die "Cannot close $tmpfile: $!";
91 my $results = runperl(%$runperl_args);
94 $results = Encode::decode("UTF-8", $results);
97 qr/DESTROY created new reference to dead object 'T' during global destruction./u,
98 "DESTROY creating a new reference to the object generates a warning in UTF-8.");
102 sub new { bless {}, shift }
103 sub nèw { bless {}, shift }
106 like( Føø::Bær::new("Føø::Bær"), qr/Føø::Bær=HASH/u, 'Can access new directly through a UTF-8 package.' );
107 like( Føø::Bær->new, qr/Føø::Bær=HASH/u, 'Can access new as a method through a UTF-8 package.' );
108 like( Føø::Bær::nèw("Føø::Bær"), qr/Føø::Bær=HASH/u, 'Can access nèw directly through a UTF-8 package.' );
109 like( Føø::Bær->nèw, qr/Føø::Bær=HASH/u, 'Can access nèw as a method through a UTF-8 package.' );
111 is( ref Føø::Bær->new, 'Føø::Bær');
113 my $new_ascii = "new";
114 my $new_latin = "nèw";
115 my $e_with_grave = byte_utf8a_to_utf8n("\303\250");
116 my $new_utf8 = "n${e_with_grave}w";
117 my $newoct = "n${e_with_grave}w";
118 utf8::decode($new_utf8);
120 like( Føø::Bær->$new_ascii, qr/Føø::Bær=HASH/u, "Can access \$new_ascii, [$new_ascii], stored in a scalar, as a method, through a UTF-8 package." );
121 like( Føø::Bær->$new_latin, qr/Føø::Bær=HASH/u, "Can access \$new_latin, [$new_latin], stored in a scalar, as a method, through a UTF-8 package." );
122 like( Føø::Bær->$new_utf8, qr/Føø::Bær=HASH/u, "Can access \$new_utf8, [$new_utf8], stored in a scalar, as a method, through a UTF-8 package." );
125 eval { Føø::Bær->$newoct };
126 like($@, qr/Can't locate object method "n${e_with_grave}w" via package "Føø::Bær"/u, "Can't access [$newoct], stored in a scalar, as a method through a UTF-8 package." );
130 like( nèw Føø::Bær, qr/Føø::Bær=HASH/u, "Can access [nèw] as a method through a UTF-8 indirect object package.");
132 my $pkg_latin_1 = 'Føø::Bær';
134 like( $pkg_latin_1->new, qr/Føø::Bær=HASH/u, 'Can access new as a method when the UTF-8 package name is in a scalar.');
135 like( $pkg_latin_1->nèw, qr/Føø::Bær=HASH/u, 'Can access nèw as a method when the UTF-8 package name is in a scalar.');
137 like( $pkg_latin_1->$new_ascii, qr/Føø::Bær=HASH/u, "Can access \$new_ascii, [$new_ascii], stored in a scalar, as a method, when the UTF-8 package name is also in a scalar.");
138 like( $pkg_latin_1->$new_latin, qr/Føø::Bær=HASH/u, "Can access \$new_latin, [$new_latin], stored in a scalar, as a method, when the UTF-8 package name is also in a scalar.");
139 like( $pkg_latin_1->$new_utf8, qr/Føø::Bær=HASH/u, "Can access \$new_utf8, [$new_utf8], stored in a scalar, as a method, when the UTF-8 package name is also in a scalar." );
142 eval { $pkg_latin_1->$newoct };
143 like($@, qr/Can't locate object method "n${e_with_grave}w" via package "Føø::Bær"/u, "Can't access [$newoct], stored in a scalar, as a method, when the UTF-8 package name is also in a scalar.");
146 ok !!Føø::Bær->can($new_ascii), "->can works for [$new_ascii]";
147 ok !!Føø::Bær->can($new_latin), "->can works for [$new_latin]";
148 ok((not !!Føø::Bær->can($newoct)), "->can doesn't work for [$newoct]");
151 sub new { bless {}, shift }
152 sub ニュー { bless {}, shift }
155 like( クラス::new("クラス"), qr/クラス=HASH/u);
156 like( クラス->new, qr/クラス=HASH/u);
158 like( クラス::ニュー("クラス"), qr/クラス=HASH/u);
159 like( クラス->ニュー, qr/クラス=HASH/u);
161 like( ニュー クラス, qr/クラス=HASH/u, "Indirect object is UTF-8, as is the class.");
163 is( ref クラス->new, 'クラス');
164 is( ref クラス->ニュー, 'クラス');
167 our @ISA = qw( Føø::Bær );
171 use parent qw( -norequire Føø::Bær );
175 use parent qw( -norequire Føø::Bær クラス );
178 ok(Foo::Bar->new, 'Simple inheritance works by pushing into @ISA,');
179 ok(Foo::Bar->nèw, 'Even with UTF-8 methods');
181 ok(Foo::Bàz->new, 'Simple inheritance works with parent using -norequire,');
182 ok(Foo::Bàz->nèw, 'Even with UTF-8 methods');
184 ok(ฟọ::バッズ->new, 'parent using -norequire, in a UTF-8 package.');
185 ok(ฟọ::バッズ->nèw, 'Also works with UTF-8 methods');
186 ok(ฟọ::バッズ->ニュー, 'Even methods from an UTF-8 parent');
188 BEGIN {no strict 'refs';
189 ++${"\xff::foo"} if $::IS_ASCII;
190 ++${"\xdf::foo"} if $::IS_EBCDIC;
191 } # autovivify the package
192 package ÿ { # without UTF8
196 "\xff::\x{100}", '$AUTOLOAD made from Latin1 package + UTF8 sub';
200 "\xdf::\x{100}", '$AUTOLOAD made from Latin1 package + UTF8 sub';
206 #This test should go somewhere else.
207 #DATA was being generated in the wrong package.
211 ::ok( *{"ʑ::DATA"}{IO}, "DATA is generated in the right glob");
212 ::ok !defined(*{"main::DATA"}{IO});
213 ::is scalar <DATA>, "Some data\n";