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