This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
parts/inc/magic: Fix to run on early perls
[perl5.git] / dist / Devel-PPPort / t / magic.t
1 ################################################################################
2 #
3 #            !!!!!   Do NOT edit this file directly!   !!!!!
4 #
5 #            Edit mktests.PL and/or parts/inc/magic instead.
6 #
7 #  This file was automatically generated from the definition files in the
8 #  parts/inc/ subdirectory by mktests.PL. To learn more about how all this
9 #  works, please read the F<HACKERS> file that came with this distribution.
10 #
11 ################################################################################
12
13 BEGIN {
14   if ($ENV{'PERL_CORE'}) {
15     chdir 't' if -d 't';
16     @INC = ('../lib', '../ext/Devel-PPPort/t') if -d '../lib' && -d '../ext';
17     require Config; import Config;
18     use vars '%Config';
19     if (" $Config{'extensions'} " !~ m[ Devel/PPPort ]) {
20       print "1..0 # Skip -- Perl configured without Devel::PPPort module\n";
21       exit 0;
22     }
23   }
24   else {
25     unshift @INC, 't';
26   }
27
28   sub load {
29     eval "use Test";
30     require 'testutil.pl' if $@;
31   }
32
33   if (45) {
34     load();
35     plan(tests => 45);
36   }
37 }
38
39 use Devel::PPPort;
40 use strict;
41 BEGIN { $^W = 1; }
42
43 package Devel::PPPort;
44 use vars '@ISA';
45 require DynaLoader;
46 @ISA = qw(DynaLoader);
47 bootstrap Devel::PPPort;
48
49 package main;
50
51 # Find proper magic
52 ok(my $obj1 = Devel::PPPort->new_with_mg());
53 ok(Devel::PPPort::as_string($obj1), 'hello');
54
55 # Find with no magic
56 my $obj = bless {}, 'Fake::Class';
57 ok(Devel::PPPort::as_string($obj), "Sorry, your princess is in another castle.");
58
59 # Find with other magic (not the magic we are looking for)
60 ok($obj = Devel::PPPort->new_with_other_mg());
61 ok(Devel::PPPort::as_string($obj), "Sorry, your princess is in another castle.");
62
63 # Okay, attempt to remove magic that isn't there
64 Devel::PPPort::remove_other_magic($obj1);
65 ok(Devel::PPPort::as_string($obj1), 'hello');
66
67 # Remove magic that IS there
68 Devel::PPPort::remove_null_magic($obj1);
69 ok(Devel::PPPort::as_string($obj1), "Sorry, your princess is in another castle.");
70
71 # Removing when no magic present
72 Devel::PPPort::remove_null_magic($obj1);
73 ok(Devel::PPPort::as_string($obj1), "Sorry, your princess is in another castle.");
74
75 use Tie::Hash;
76 my %h;
77 tie %h, 'Tie::StdHash';
78 $h{foo} = 'foo';
79 $h{bar} = '';
80
81 &Devel::PPPort::sv_catpv_mg($h{foo}, 'bar');
82 ok($h{foo}, 'foobar');
83
84 &Devel::PPPort::sv_catpvn_mg($h{bar}, 'baz');
85 ok($h{bar}, 'baz');
86
87 &Devel::PPPort::sv_catsv_mg($h{foo}, '42');
88 ok($h{foo}, 'foobar42');
89
90 &Devel::PPPort::sv_setiv_mg($h{bar}, 42);
91 ok($h{bar}, 42);
92
93 &Devel::PPPort::sv_setnv_mg($h{PI}, 3.14159);
94 ok(abs($h{PI} - 3.14159) < 0.01);
95
96 &Devel::PPPort::sv_setpv_mg($h{mhx}, 'mhx');
97 ok($h{mhx}, 'mhx');
98
99 &Devel::PPPort::sv_setpvn_mg($h{mhx}, 'Marcus');
100 ok($h{mhx}, 'Marcus');
101
102 &Devel::PPPort::sv_setsv_mg($h{sv}, 'SV');
103 ok($h{sv}, 'SV');
104
105 &Devel::PPPort::sv_setuv_mg($h{sv}, 4711);
106 ok($h{sv}, 4711);
107
108 &Devel::PPPort::sv_usepvn_mg($h{sv}, 'Perl');
109 ok($h{sv}, 'Perl');
110
111 # v1 is treated as a bareword in older perls...
112 my $ver = do { local $SIG{'__WARN__'} = sub {}; eval qq[v1.2.0] };
113 ok("$]" < 5.009 || $@ eq '');
114 ok("$]" < 5.009 || Devel::PPPort::SvVSTRING_mg($ver));
115 ok(!Devel::PPPort::SvVSTRING_mg(4711));
116
117 my $foo = 'bar';
118 ok(Devel::PPPort::sv_magic_portable($foo));
119 ok($foo eq 'bar');
120
121 if ( "$]" < '5.007003' ) {
122     for (1..22) {
123         skip 'skip: no SV_NOSTEAL support', 0;
124     }
125 } else {
126     tie my $scalar, 'TieScalarCounter', 10;
127     my $fetch = $scalar;
128
129     ok tied($scalar)->{fetch}, 1;
130     ok tied($scalar)->{store}, 0;
131     ok Devel::PPPort::magic_SvIV_nomg($scalar), 10;
132     ok tied($scalar)->{fetch}, 1;
133     ok tied($scalar)->{store}, 0;
134     ok Devel::PPPort::magic_SvUV_nomg($scalar), 10;
135     ok tied($scalar)->{fetch}, 1;
136     ok tied($scalar)->{store}, 0;
137     ok Devel::PPPort::magic_SvNV_nomg($scalar), 10;
138     ok tied($scalar)->{fetch}, 1;
139     ok tied($scalar)->{store}, 0;
140     ok Devel::PPPort::magic_SvPV_nomg_nolen($scalar), 10;
141     ok tied($scalar)->{fetch}, 1;
142     ok tied($scalar)->{store}, 0;
143     ok Devel::PPPort::magic_SvTRUE_nomg($scalar);
144     ok tied($scalar)->{fetch}, 1;
145     ok tied($scalar)->{store}, 0;
146
147     my $object = OverloadedObject->new('string', 5.5, 0);
148
149     ok Devel::PPPort::magic_SvIV_nomg($object), 5;
150     ok Devel::PPPort::magic_SvUV_nomg($object), 5;
151     ok Devel::PPPort::magic_SvNV_nomg($object), 5.5;
152     ok Devel::PPPort::magic_SvPV_nomg_nolen($object), 'string';
153     ok !Devel::PPPort::magic_SvTRUE_nomg($object);
154 }
155
156 package TieScalarCounter;
157
158 sub TIESCALAR {
159     my ($class, $value) = @_;
160     return bless { fetch => 0, store => 0, value => $value }, $class;
161 }
162
163 sub FETCH {
164     my ($self) = @_;
165     $self->{fetch}++;
166     return $self->{value};
167 }
168
169 sub STORE {
170     my ($self, $value) = @_;
171     $self->{store}++;
172     $self->{value} = $value;
173 }
174
175 package OverloadedObject;
176
177 sub new {
178     my ($class, $str, $num, $bool) = @_;
179     return bless { str => $str, num => $num, bool => $bool }, $class;
180 }
181
182 use overload
183     '""' => sub { $_[0]->{str} },
184     '0+' => sub { $_[0]->{num} },
185     'bool' => sub { $_[0]->{bool} },
186     ;
187