]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/25-copy.t
Update VPIT::TestHelpers to e8344578
[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 lib 't/lib';
9 use VPIT::TestHelpers;
10
11 use Variable::Magic qw<cast dispell>;
12
13 plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 1;
14
15 use lib 't/lib';
16 use Variable::Magic::TestWatcher;
17 use Variable::Magic::TestValue;
18
19 my $wiz = init_watcher 'copy', 'copy';
20
21 SKIP: {
22  load_or_skip('Tie::Array', undef, undef, (2 * 5 + 3) + (2 * 2 + 1));
23
24  tie my @a, 'Tie::StdArray';
25  @a = (1 .. 10);
26
27  my $res = watch { cast @a, $wiz } { }, 'cast on tied array';
28  ok $res, 'copy: cast on tied array succeeded';
29
30  watch { $a[3] = 13 } { copy => 1 }, 'tied array store';
31
32  my $s = watch { $a[3] } { copy => 1 }, 'tied array fetch';
33  is $s, 13, 'copy: tied array fetch correctly';
34
35  $s = watch { exists $a[3] } { copy => 1 }, 'tied array exists';
36  ok $s, 'copy: tied array exists correctly';
37
38  watch { undef @a } { }, 'tied array undef';
39
40  {
41   tie my @val, 'Tie::StdArray';
42   @val = (4 .. 6);
43
44   my $wv = init_value @val, 'copy', 'copy';
45
46   value { $val[3] = 8 } [ 4 .. 6 ];
47
48   dispell @val, $wv;
49   is_deeply \@val, [ 4 .. 6, 8 ], 'copy: value after';
50  }
51 }
52
53 SKIP: {
54  load_or_skip('Tie::Hash', undef, undef, 2 * 9 + 6);
55
56  tie my %h, 'Tie::StdHash';
57  %h = (a => 1, b => 2, c => 3);
58
59  my $res = watch { cast %h, $wiz } { }, 'cast on tied hash';
60  ok $res, 'copy: cast on tied hash succeeded';
61
62  watch { $h{b} = 7 } { copy => 1 }, 'tied hash store';
63
64  my $s = watch { $h{c} } { copy => 1 }, 'tied hash fetch';
65  is $s, 3, 'copy: tied hash fetch correctly';
66
67  $s = watch { exists $h{a} } { copy => 1 }, 'tied hash exists';
68  ok $s, 'copy: tied hash exists correctly';
69
70  $s = watch { delete $h{b} } { copy => 1 }, 'tied hash delete';
71  is $s, 7, 'copy: tied hash delete correctly';
72
73  watch { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
74
75  my @k = watch { keys %h } { }, 'tied hash keys';
76  is_deeply [ sort @k ], [ qw<a c> ], 'copy: tied hash keys correctly';
77
78  my @v = watch { values %h } { copy => 2 }, 'tied hash values';
79  is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
80
81  watch { undef %h } { }, 'tied hash undef';
82 }