]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/30-scalar.t
Fix the testsuite for 5.8.3 and lower
[perl/modules/Variable-Magic.git] / t / 30-scalar.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => (2 * 14 + 2) + 2 * (2 * 8 + 4) + 1;
7
8 use Variable::Magic qw/cast dispell/;
9
10 use lib 't/lib';
11 use Variable::Magic::TestWatcher;
12
13 my $wiz = init
14         [ qw/get set len clear free copy dup local fetch store exists delete/ ],
15         'scalar';
16
17 my $n = int rand 1000;
18 my $a = $n;
19
20 check { cast $a, $wiz } { }, 'cast';
21
22 my $b;
23 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
24 check { $b = $a } { get => 1 }, 'assign to';
25 is $b, $n, 'scalar: assign to correctly';
26
27 $b = check { "X${a}Y" } { get => 1 }, 'interpolate';
28 is $b, "X${n}Y", 'scalar: interpolate correctly';
29
30 $b = check { \$a } { }, 'reference';
31
32 check { $a = 123; () } { set => 1 }, 'assign to';
33
34 check { ++$a; () } { get => 1, set => 1 }, 'increment';
35
36 check { --$a; () } { get => 1, set => 1 }, 'decrement';
37
38 check { $a *= 1.5; () } { get => 1, set => 1 }, 'multiply in place';
39
40 check { $a /= 1.5; () } { get => 1, set => 1 }, 'divide in place';
41
42 check {
43  my $b = $n;
44  check { cast $b, $wiz } { }, 'cast 2';
45 } { free => 1 }, 'scope end';
46
47 check { undef $a } { set => 1 }, 'undef';
48
49 check { dispell $a, $wiz } { }, 'dispell';
50
51 # Array element
52
53 my @a = (7, 8, 9);
54
55 check { cast $a[1], $wiz } { }, 'array element: cast';
56
57 check { $a[1] = 6; () } { set => 1 }, 'array element: set';
58
59 $b = check { $a[1] } { get => 1 }, 'array element: get';
60 is $b, 6, 'scalar: array element: get correctly';
61
62 check { $a[0] = 5 } { }, 'array element: set other';
63
64 $b = check { $a[2] } { }, 'array element: get other';
65 is $b, 9, 'scalar: array element: get other correctly';
66
67 $b = check { exists $a[1] } { }, 'array element: exists';
68 is $b, 1, 'scalar: array element: exists correctly';
69
70 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
71 check { $b = delete $a[1] } { get => 1, free => ($] > 5.008005 ? 1 : 0) }, 'array element: delete';
72 is $b, 6, 'scalar: array element: delete correctly';
73
74 check { $a[1] = 4 } { }, 'array element: set after delete';
75
76 # Hash element
77
78 my %h = (a => 7, b => 8);
79
80 check { cast $h{b}, $wiz } { }, 'hash element: cast';
81
82 check { $h{b} = 6; () } { set => 1 }, 'hash element: set';
83
84 $b = check { $h{b} } { get => 1 }, 'hash element: get';
85 is $b, 6, 'scalar: hash element: get correctly';
86
87 check { $h{a} = 5 } { }, 'hash element: set other';
88
89 $b = check { $h{a} } { }, 'hash element: get other';
90 is $b, 5, 'scalar: hash element: get other correctly';
91
92 $b = check { exists $h{b} } { }, 'hash element: exists';
93 is $b, 1, 'scalar: hash element: exists correctly';
94
95 $b = check { delete $h{b} } { get => 1, free => 1 }, 'hash element: delete';
96 is $b, 6, 'scalar: hash element: delete correctly';
97
98 check { $h{b} = 4 } { }, 'hash element: set after delete';
99