]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/25-copy.t
Don't assign results in check blocks
[perl/modules/Variable-Magic.git] / t / 25-copy.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Variable::Magic qw/cast MGf_COPY/;
9
10 if (MGf_COPY) {
11  plan tests => 2 + (2 * 5 + 3) + (2 * 9 + 6) + 1;
12 } else {
13  plan skip_all => 'No copy magic for this perl';
14 }
15
16 use lib 't/lib';
17 use Variable::Magic::TestWatcher;
18
19 my $wiz = init 'copy', 'copy';
20
21 SKIP: {
22  eval "use Tie::Array";
23  skip 'Tie::Array required to test copy magic on arrays', 2 * 5 + 3 if $@;
24  diag "Using Tie::Array $Tie::Array::VERSION" if defined $Tie::Array::VERSION;
25
26  tie my @a, 'Tie::StdArray';
27  @a = (1 .. 10);
28
29  my $res = check { cast @a, $wiz } { }, 'cast on tied array';
30  ok $res, 'copy: cast on tied array succeeded';
31
32  check { $a[3] = 13 } { copy => 1 }, 'tied array store';
33
34  my $s = check { $a[3] } { copy => 1 }, 'tied array fetch';
35  is $s, 13, 'copy: tied array fetch correctly';
36
37  $s = check { exists $a[3] } { copy => 1 }, 'tied array exists';
38  ok $s, 'copy: tied array exists correctly';
39
40  check { undef @a } { }, 'tied array undef';
41 }
42
43 SKIP: {
44  eval "use Tie::Hash";
45  skip 'Tie::Hash required to test copy magic on hashes' => 2 * 9 + 6 if $@;
46  diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION;
47
48  tie my %h, 'Tie::StdHash';
49  %h = (a => 1, b => 2, c => 3);
50
51  my $res = check { cast %h, $wiz } { }, 'cast on tied hash';
52  ok $res, 'copy: cast on tied hash succeeded';
53
54  check { $h{b} = 7 } { copy => 1 }, 'tied hash store';
55
56  my $s = check { $h{c} } { copy => 1 }, 'tied hash fetch';
57  is $s, 3, 'copy: tied hash fetch correctly';
58
59  $s = check { exists $h{a} } { copy => 1 }, 'tied hash exists';
60  ok $s, 'copy: tied hash exists correctly';
61
62  $s = check { delete $h{b} } { copy => 1 }, 'tied hash delete';
63  is $s, 7, 'copy: tied hash delete correctly';
64
65  check { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
66
67  my @k = check { keys %h } { }, 'tied hash keys';
68  is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly';
69
70  my @v = check { values %h } { copy => 2 }, 'tied hash values';
71  is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
72
73  check { undef %h } { }, 'tied hash undef';
74 }