]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/30-scalar.t
Fix tests for perl 5.13
[perl/modules/Variable-Magic.git] / t / 30-scalar.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Config qw/%Config/;
7
8 use Test::More tests => (2 * 14 + 2) + 2 * (2 * 8 + 4) + 3 + 1;
9
10 use Variable::Magic qw/wizard cast dispell MGf_COPY/;
11
12 use lib 't/lib';
13 use Variable::Magic::TestWatcher;
14
15 my $is_5130_release = ($] == 5.013 && !$Config{git_describe}) ? 1 : 0;
16
17 my $wiz = init_watcher
18         [ qw/get set len clear free copy dup local fetch store exists delete/ ],
19         'scalar';
20
21 my $n = int rand 1000;
22 my $a = $n;
23
24 watch { cast $a, $wiz } { }, 'cast';
25
26 my $b;
27 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
28 watch { $b = $a } { get => 1 }, 'assign to';
29 is $b, $n, 'scalar: assign to correctly';
30
31 $b = watch { "X${a}Y" } { get => 1 }, 'interpolate';
32 is $b, "X${n}Y", 'scalar: interpolate correctly';
33
34 $b = watch { \$a } { }, 'reference';
35
36 watch { $a = 123; () } { set => 1 }, 'assign to';
37
38 watch { ++$a; () } { get => 1, set => 1 }, 'increment';
39
40 watch { --$a; () } { get => 1, set => 1 }, 'decrement';
41
42 watch { $a *= 1.5; () } { get => 1, set => 1 }, 'multiply in place';
43
44 watch { $a /= 1.5; () } { get => 1, set => 1 }, 'divide in place';
45
46 watch {
47  my $b = $n;
48  watch { cast $b, $wiz } { }, 'cast 2';
49 } { free => 1 }, 'scope end';
50
51 watch { undef $a } { set => 1 }, 'undef';
52
53 watch { dispell $a, $wiz } { }, 'dispell';
54
55 # Array element
56
57 my @a = (7, 8, 9);
58
59 watch { cast $a[1], $wiz } { }, 'array element: cast';
60
61 watch { $a[1] = 6; () } { set => 1 }, 'array element: set';
62
63 $b = watch { $a[1] } { get => ($is_5130_release ? 2 : 1) },'array element: get';
64 is $b, 6, 'scalar: array element: get correctly';
65
66 watch { $a[0] = 5 } { }, 'array element: set other';
67
68 $b = watch { $a[2] } { }, 'array element: get other';
69 is $b, 9, 'scalar: array element: get other correctly';
70
71 $b = watch { exists $a[1] } { }, 'array element: exists';
72 is $b, 1, 'scalar: array element: exists correctly';
73
74 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
75 watch { $b = delete $a[1] } { get => 1, free => ($] > 5.008005 ? 1 : 0) }, 'array element: delete';
76 is $b, 6, 'scalar: array element: delete correctly';
77
78 watch { $a[1] = 4 } { }, 'array element: set after delete';
79
80 # Hash element
81
82 my %h = (a => 7, b => 8);
83
84 watch { cast $h{b}, $wiz } { }, 'hash element: cast';
85
86 watch { $h{b} = 6; () } { set => 1 }, 'hash element: set';
87
88 $b = watch { $h{b} } { get => ($is_5130_release ? 2 : 1) }, 'hash element: get';
89 is $b, 6, 'scalar: hash element: get correctly';
90
91 watch { $h{a} = 5 } { }, 'hash element: set other';
92
93 $b = watch { $h{a} } { }, 'hash element: get other';
94 is $b, 5, 'scalar: hash element: get other correctly';
95
96 $b = watch { exists $h{b} } { }, 'hash element: exists';
97 is $b, 1, 'scalar: hash element: exists correctly';
98
99 $b = watch { delete $h{b} } { get => 1, free => 1 }, 'hash element: delete';
100 is $b, 6, 'scalar: hash element: delete correctly';
101
102 watch { $h{b} = 4 } { }, 'hash element: set after delete';
103
104 SKIP: {
105  my $SKIP;
106
107  unless (MGf_COPY) {
108   $SKIP = 'No copy magic for this perl';
109  } elsif ($Config{useithreads} and $] le 5.008003) {
110   $SKIP = 'Causes havoc during global destruction for old threaded perls';
111  } else {
112   eval "use Tie::Array";
113   $SKIP = 'Tie::Array required to test clear magic on tied array values' if $@;
114  }
115
116  skip $SKIP => 3 if $SKIP;
117  diag "Using Tie::Array $Tie::Array::VERSION" if defined $Tie::Array::VERSION;
118
119  tie my @a, 'Tie::StdArray';
120  $a[0] = $$;
121
122  eval {
123   cast @a, wizard copy => sub { cast $_[3], $wiz; () };
124  };
125  is $@, '', 'cast copy magic on tied array';
126
127  watch { delete $a[0] } [ qw/get clear free/ ], 'delete from tied array';
128 }