This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
call AV set magic in list assign
[perl5.git] / ext / XS-APItest / t / magic.t
CommitLineData
26ab20ee
FR
1use strict;
2use warnings;
3use Test::More;
4
5use XS::APItest;
6
7my $sv = bless {}, 'Moo';
8my $foo = 'affe';
9my $bar = 'tiger';
10
11ok !mg_find_foo($sv), 'no foo magic yet';
12ok !mg_find_bar($sv), 'no bar magic yet';
13
14sv_magic_foo($sv, $foo);
15is mg_find_foo($sv), $foo, 'foo magic attached';
16ok !mg_find_bar($sv), '... but still no bar magic';
17
18sv_magic_bar($sv, $bar);
19is mg_find_foo($sv), $foo, 'foo magic still attached';
20is mg_find_bar($sv), $bar, '... and bar magic is there too';
21
22sv_unmagic_foo($sv);
23ok !mg_find_foo($sv), 'foo magic removed';
24is mg_find_bar($sv), $bar, '... but bar magic is still there';
25
26sv_unmagic_bar($sv);
27ok !mg_find_foo($sv), 'foo magic still removed';
28ok !mg_find_bar($sv), '... and bar magic is removed too';
29
406700cc
NC
30is(test_get_vtbl(), 0, 'get_vtbl(-1) returns NULL');
31
1d5686ec
FC
32use Scalar::Util 'weaken';
33eval { sv_magic(\!0, $foo) };
34is $@, "", 'PERL_MAGIC_ext is permitted on read-only things';
35
80c1439f
DM
36# assigning to an array/hash with only set magic should call that magic
37
38{
39 my (@a, %h, $i);
40
41 sv_magic_myset(\@a, $i);
42 sv_magic_myset(\%h, $i);
43
44 $i = 0;
45 @a = (1,2);
46 is($i, 2, "array with set magic");
47
48 $i = 0;
49 @a = ();
50 is($i, 0, "array () with set magic");
51
52 {
53 local $TODO = "HVs don't call set magic - not sure if should";
54
55 $i = 0;
56 %h = qw(a 1 b 2);
57 is($i, 4, "hash with set magic");
58 }
59
60 $i = 0;
61 %h = qw();
62 is($i, 0, "hash () with set magic");
63}
64
26ab20ee 65done_testing;