File: 106_LazyClass_test.t

package info (click to toggle)
libclass-mop-perl 1.04-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,244 kB
  • ctags: 1,272
  • sloc: perl: 5,192; ansic: 241; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,730 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;

use Test::More;
use File::Spec;

use Class::MOP;

BEGIN {
    require_ok(File::Spec->catfile('examples', 'LazyClass.pod'));
}

{
    package BinaryTree;

    use metaclass (
        'attribute_metaclass' => 'LazyClass::Attribute',
        'instance_metaclass'  => 'LazyClass::Instance',
    );

    BinaryTree->meta->add_attribute('node' => (
        accessor => 'node',
        init_arg => 'node'
    ));

    BinaryTree->meta->add_attribute('left' => (
        reader  => 'left',
        default => sub { BinaryTree->new() }
    ));

    BinaryTree->meta->add_attribute('right' => (
        reader  => 'right',
        default => sub { BinaryTree->new() }
    ));

    sub new {
        my $class = shift;
        bless $class->meta->new_object(@_) => $class;
    }
}

my $root = BinaryTree->new('node' => 0);
isa_ok($root, 'BinaryTree');

ok(exists($root->{'node'}), '... node attribute has been initialized yet');
ok(!exists($root->{'left'}), '... left attribute has not been initialized yet');
ok(!exists($root->{'right'}), '... right attribute has not been initialized yet');

isa_ok($root->left, 'BinaryTree');
isa_ok($root->right, 'BinaryTree');

ok(exists($root->{'left'}), '... left attribute has now been initialized');
ok(exists($root->{'right'}), '... right attribute has now been initialized');

ok(!exists($root->left->{'node'}), '... node attribute has not been initialized yet');
ok(!exists($root->left->{'left'}), '... left attribute has not been initialized yet');
ok(!exists($root->left->{'right'}), '... right attribute has not been initialized yet');

ok(!exists($root->right->{'node'}), '... node attribute has not been initialized yet');
ok(!exists($root->right->{'left'}), '... left attribute has not been initialized yet');
ok(!exists($root->right->{'right'}), '... right attribute has not been initialized yet');

is($root->left->node(), undef, '... the left node is uninitialized');

ok(exists($root->left->{'node'}), '... node attribute has now been initialized');

$root->left->node(1);
is($root->left->node(), 1, '... the left node == 1');

ok(!exists($root->left->{'left'}), '... left attribute still has not been initialized yet');
ok(!exists($root->left->{'right'}), '... right attribute still has not been initialized yet');

is($root->right->node(), undef, '... the right node is uninitialized');

ok(exists($root->right->{'node'}), '... node attribute has now been initialized');

$root->right->node(2);
is($root->right->node(), 2, '... the right node == 1');

ok(!exists($root->right->{'left'}), '... left attribute still has not been initialized yet');
ok(!exists($root->right->{'right'}), '... right attribute still has not been initialized yet');

done_testing;