File: Accessor-Fast.t

package info (click to toggle)
libclass-accessor-perl 0.19-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 96 kB
  • ctags: 19
  • sloc: perl: 209; makefile: 44
file content (165 lines) | stat: -rw-r--r-- 4,289 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
use strict;

use vars qw($Total_tests);

my $loaded;
my $test_num;
BEGIN { $| = 1; $^W = 1; $test_num=1}
END {print "not ok $test_num\n" unless $loaded;}
print "1..$Total_tests\n";
use Class::Accessor::Fast;
$loaded = 1;
ok(1,                                                           'compile()' );
######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):
sub ok {
    my($test, $name) = @_;
    print "not " unless $test;
    print "ok $test_num";
    print " - $name" if defined $name;
    print "\n";
    $test_num++;
}

sub eqarray  {
    my($a1, $a2) = @_;
    return 0 unless @$a1 == @$a2;
    my $ok = 1;
    for (0..$#{$a1}) { 
        unless($a1->[$_] eq $a2->[$_]) {
        $ok = 0;
        last;
        }
    }
    return $ok;
}

# Change this to your # of ok() calls + 1
BEGIN { $Total_tests = 20 }


# Set up a testing package.
package Foo;

@Foo::ISA = qw(Class::Accessor::Fast);
Foo->mk_accessors(qw( foo bar yar car mar ));
Foo->mk_ro_accessors(qw(static unchanged));
Foo->mk_wo_accessors(qw(sekret double_sekret));


sub car {
    shift->_car_accessor(@_);
}

sub mar {
    return "Overloaded";
}

package main;

my Foo $test = Foo->new({ static       => "variable",
                          unchanged    => "dynamic",
                        });

# Test accessors.
$test->foo(42);
$test->bar('Meep');
ok( $test->foo   == 42 and
    $test->{foo} == 42,                                 'accessor get/set'  );

ok( $test->static eq 'variable',                        'accessor read-only' );
eval {
    $test->static('foo');
};
ok( scalar $@ =~ /^'main' cannot alter the value of 'static' on objects of class 'Foo'/, 'accessor read-only:  write protection' );

$test->double_sekret(1001001);
ok( $test->{double_sekret} == 1001001,                  'accessor write-only');
eval {
    () = $test->double_sekret;
};
ok( scalar $@ =~ /^'main' cannot access the value of 'double_sekret' on objects of class 'Foo'/, 'accessor write-only:  read protection' );


ok( $test->_foo_accessor == 42,                         'accessor alias'    );

$test->car("AMC Javalin");
ok( $test->car eq 'AMC Javalin' );

# Make sure we can "override" accessors.
ok( $test->mar eq 'Overloaded' );

# Make sure bogus accessors die.
eval { $test->gargle() };
ok( $@,                                                 'bad accessor()'    );



# Test that the accessor works properly in list context with a single arg.
my Foo $test2 = Foo->new;
my @args = ($test2->foo, $test2->bar);
ok( @args == 2,                         'accessor get in list context'      );



# Make sure a DESTROY field won't slip through.
package Arrgh;
@Arrgh::ISA = qw(Foo);

eval {
    local $SIG{__WARN__} = sub { die @_ };
    Arrgh->mk_accessor(qw(DESTROY));
};

::ok( $@ and $@ =~ /Having a data accessor named DESTROY in 'Arrgh'/i,
                                                        'No DESTROY field'  );

# Override &Arrgh::DESTROY to shut up the warning we intentionally created
#*Arrgh::DESTROY = sub {};
#() = *Arrgh::DESTROY;  # shut up typo warning.



package Altoids;

use base qw(Class::Accessor::Fast);
use fields qw(curiously strong mints);
Altoids->mk_accessors(keys %Altoids::FIELDS);

::ok(defined &Altoids::curiously);
::ok(defined &Altoids::strong);
::ok(defined &Altoids::mints);

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;
    return fields::new($class);
}

my Altoids $tin = Altoids->new;

$tin->curiously('Curiouser and curiouser');
::ok($tin->{curiously} eq 'Curiouser and curiouser');


# Subclassing works, too.
package Mint::Snuff;
use base qw(Altoids);

::ok(defined &Altoids::curiously);
::ok(defined &Altoids::strong);
::ok(defined &Altoids::mints);

my Mint::Snuff $pouch = Mint::Snuff->new;
$pouch->strong('Fuck you up strong!');
::ok($pouch->{strong} eq 'Fuck you up strong!');