File: 06hash_constructor.t

package info (click to toggle)
libclass-xsaccessor-perl 1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 520 kB
  • sloc: perl: 841; ansic: 359; makefile: 6
file content (59 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;

package Class::XSAccessor::Test;

use Class::XSAccessor
  constructor => 'new',
  accessors => { bar => 'bar', blubber => 'blubber' },
  getters   => { get_foo => 'foo' },
  setters   => { set_foo => 'foo' };

our $DESTROYED = 0;

sub DESTROY { $DESTROYED = 1 }

package main;

use Test::More tests => 23;

ok (Class::XSAccessor::Test->can('new'));

my $obj = Class::XSAccessor::Test->new(bar => 'baz', 'blubber' => 'blabber');

ok ($obj->can('bar'));
is ($obj->set_foo('bar'), 'bar');
is ($obj->get_foo(), 'bar');
is ($obj->bar(), 'baz');
is ($obj->bar('quux'), 'quux');
is ($obj->bar(), 'quux');
is ($obj->blubber(), 'blabber');

my $obj2 = $obj->new(bar => 'baz', 'blubber' => 'blabber');
ok ($obj2->can('bar'));
is ($obj2->set_foo('bar'), 'bar');
is ($obj2->get_foo(), 'bar');
is ($obj2->bar(), 'baz');
is ($obj2->bar('quux'), 'quux');
is ($obj2->bar(), 'quux');
is ($obj2->blubber(), 'blabber');

eval 'Class::XSAccessor::Test->new("uneven_no_of_args")';
ok ($@);

# make sure the object refcount is valid (i.e. it's not reaped at the end of an inner scope if it's
# referenced in an outer scope)
{
    my $obj3;
    {
        is($Class::XSAccessor::Test::DESTROYED, 0);
        $obj3 = do { Class::XSAccessor::Test->new(bar => 'baz', 'blubber' => 'blabber') };
        is($Class::XSAccessor::Test::DESTROYED, 0);
    }
    is($Class::XSAccessor::Test::DESTROYED, 0);
    ok($obj3, 'object not reaped in outer scope');
    isa_ok($obj3, 'Class::XSAccessor::Test');
    can_ok($obj3, qw(bar blubber get_foo set_foo));
}

is($Class::XSAccessor::Test::DESTROYED, 1);