Reshuffle test loop (no func. changes). Examine under -w
[p5sagit/Class-Accessor-Grouped.git] / t / accessors.t
CommitLineData
6c6bc8c2 1use Test::More tests => 136;
e7d391a8 2use strict;
3use warnings;
4use lib 't/lib';
d1dc76a1 5use B qw/svref_2object/;
e7d391a8 6
8019c4d8 7# we test the pure-perl versions only, but allow overrides
8# from the accessor_xs test-umbrella
9# Also make sure a rogue envvar will not interfere with
10# things
28344104 11my $use_xs;
9540f4e4 12BEGIN {
ba8c183b 13 $Class::Accessor::Grouped::USE_XS = 0
14 unless defined $Class::Accessor::Grouped::USE_XS;
15 $ENV{CAG_USE_XS} = 1;
16 $use_xs = $Class::Accessor::Grouped::USE_XS;
8019c4d8 17};
18
5fc5d14f 19require AccessorGroupsSubclass;
1ee74bdd 20
28344104 21my $test_accessors = {
ba8c183b 22 singlefield => {
5fc5d14f 23 is_simple => 1,
ba8c183b 24 has_extra => 1,
25 },
26 runtime_around => {
5fc5d14f 27 # even though this accessor is declared as simple it will *not* be
28 # reinstalled due to the runtime 'around'
29 #is_simple => 1,
ba8c183b 30 has_extra => 1,
31 },
32 multiple1 => {
33 },
34 multiple2 => {
35 },
36 lr1name => {
37 custom_field => 'lr1;field',
38 },
39 lr2name => {
40 custom_field => "lr2'field",
41 },
4d70ba11 42 fieldname_torture => {
5fc5d14f 43 is_simple => 1,
4d70ba11 44 custom_field => join ('', map { chr($_) } (0..255) ),
4d70ba11 45 },
28344104 46};
47
5fc5d14f 48for my $obj (
49 AccessorGroupsSubclass->new,
50) {
51 for my $name (sort keys %$test_accessors) {
52 my $alias = "_${name}_accessor";
53 my $field = $test_accessors->{$name}{custom_field} || $name;
54 my $extra = $test_accessors->{$name}{has_extra};
ba8c183b 55
5fc5d14f 56 can_ok($obj, $name, $alias);
57 ok(!$obj->can($field))
58 if $field ne $name;
ba8c183b 59
5fc5d14f 60 # initial method name
61 for my $meth ($name, $alias) {
62 my $cv = svref_2object( $obj->can($meth) );
63 is($cv->GV->NAME, $meth, "initial $meth accessor is named");
64 is(
65 $cv->GV->STASH->NAME,
66 'AccessorGroups',
67 "initial $meth class correct",
68 );
69 }
ba8c183b 70
5fc5d14f 71 is($obj->$name, undef);
72 is($obj->$alias, undef);
ba8c183b 73
5fc5d14f 74 # get/set via name
75 is($obj->$name('a'), 'a');
76 is($obj->$name, 'a');
77 is($obj->{$field}, $extra ? 'a Extra tackled on' : 'a');
ba8c183b 78
5fc5d14f 79 # alias gets same as name
80 is($obj->$alias, 'a');
ba8c183b 81
5fc5d14f 82 # get/set via alias
83 is($obj->$alias('b'), 'b');
84 is($obj->$alias, 'b');
85 is($obj->{$field}, $extra ? 'b Extra tackled on' : 'b');
ba8c183b 86
5fc5d14f 87 # alias gets same as name
88 is($obj->$name, 'b');
ba8c183b 89
5fc5d14f 90 for my $meth ($name, $alias) {
91 my $cv = svref_2object( $obj->can($meth) );
92 is($cv->GV->NAME, $meth, "$meth accessor is named after operations");
93 is(
94 $cv->GV->STASH->NAME,
95 # XS deferred subs install into each caller, not into the original parent
96 ($use_xs and $test_accessors->{$name}{is_simple})
97 ? ref $obj
98 : 'AccessorGroups'
99 ,
100 "$meth class correct after operations",
101 );
102 }
ba8c183b 103 }
5fc5d14f 104}
e7d391a8 105
8019c4d8 106# important
9540f4e4 1071;