]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/25-copy.t
Convert t/25-copy.t to the new testing framework
[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;
35  check { $s = $a[3] } { copy => 1 }, 'tied array fetch';
36  is $s, 13, 'copy: tied array fetch correctly';
37
38  check { $s = exists $a[3] } { copy => 1 }, 'tied array exists';
39  ok $s, 'copy: tied array exists correctly';
40
41  check { undef @a } { }, 'tied array undef';
42 }
43
44 SKIP: {
45  eval "use Tie::Hash";
46  skip 'Tie::Hash required to test copy magic on hashes' => 2 * 9 + 6 if $@;
47  diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION;
48
49  tie my %h, 'Tie::StdHash';
50  %h = (a => 1, b => 2, c => 3);
51
52  my $res = check { cast %h, $wiz } { }, 'cast on tied hash';
53  ok $res, 'copy: cast on tied hash succeeded';
54
55  check { $h{b} = 7 } { copy => 1 }, 'tied hash store';
56
57  my $s;
58  check { $s = $h{c} } { copy => 1 }, 'tied hash fetch';
59  is $s, 3, 'copy: tied hash fetch correctly';
60
61  check { $s = exists $h{a} } { copy => 1 }, 'tied hash exists';
62  ok $s, 'copy: tied hash exists correctly';
63
64  check { $s = delete $h{b} } { copy => 1 }, 'tied hash delete';
65  is $s, 7, 'copy: tied hash delete correctly';
66
67  check { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
68
69  my @k;
70  check { @k = keys %h } { }, 'tied hash keys';
71  is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly';
72
73  my @v;
74  check { @v = values %h } { copy => 2 }, 'tied hash values';
75  is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
76
77  check { undef %h } { }, 'tied hash undef';
78 }