instance-refactored
[gitmo/Class-MOP.git] / t / 106_LazyClass_test.t
CommitLineData
4300f56a 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
046688ed 6use Test::More tests => 26;
4300f56a 7use File::Spec;
8
9BEGIN {
10 use_ok('Class::MOP');
11 require_ok(File::Spec->catdir('examples', 'LazyClass.pod'));
12}
13
14{
15 package BinaryTree;
16
fed4cee7 17 use metaclass 'Class::MOP::Class' => (
49c93440 18 ':attribute_metaclass' => 'LazyClass::Attribute',
677eb158 19 );
4300f56a 20
21 BinaryTree->meta->add_attribute('$:node' => (
22 accessor => 'node',
23 init_arg => ':node'
24 ));
25
26 BinaryTree->meta->add_attribute('$:left' => (
27 reader => 'left',
28 default => sub { BinaryTree->new() }
29 ));
30
31 BinaryTree->meta->add_attribute('$:right' => (
32 reader => 'right',
33 default => sub { BinaryTree->new() }
34 ));
35
36 sub new {
37 my $class = shift;
38 bless $class->meta->construct_instance(@_) => $class;
39 }
40}
41
42my $root = BinaryTree->new(':node' => 0);
43isa_ok($root, 'BinaryTree');
44
45ok(exists($root->{'$:node'}), '... node attribute has been initialized yet');
46ok(!exists($root->{'$:left'}), '... left attribute has not been initialized yet');
47ok(!exists($root->{'$:right'}), '... right attribute has not been initialized yet');
48
49isa_ok($root->left, 'BinaryTree');
50isa_ok($root->right, 'BinaryTree');
51
52ok(exists($root->{'$:left'}), '... left attribute has now been initialized');
53ok(exists($root->{'$:right'}), '... right attribute has now been initialized');
54
55ok(!exists($root->left->{'$:node'}), '... node attribute has not been initialized yet');
56ok(!exists($root->left->{'$:left'}), '... left attribute has not been initialized yet');
57ok(!exists($root->left->{'$:right'}), '... right attribute has not been initialized yet');
58
59ok(!exists($root->right->{'$:node'}), '... node attribute has not been initialized yet');
60ok(!exists($root->right->{'$:left'}), '... left attribute has not been initialized yet');
61ok(!exists($root->right->{'$:right'}), '... right attribute has not been initialized yet');
62
63is($root->left->node(), undef, '... the left node is uninitialized');
64
65ok(exists($root->left->{'$:node'}), '... node attribute has now been initialized');
66
67$root->left->node(1);
68is($root->left->node(), 1, '... the left node == 1');
69
70ok(!exists($root->left->{'$:left'}), '... left attribute still has not been initialized yet');
71ok(!exists($root->left->{'$:right'}), '... right attribute still has not been initialized yet');
72
73is($root->right->node(), undef, '... the right node is uninitialized');
74
75ok(exists($root->right->{'$:node'}), '... node attribute has now been initialized');
76
77$root->right->node(2);
78is($root->right->node(), 2, '... the right node == 1');
79
80ok(!exists($root->right->{'$:left'}), '... left attribute still has not been initialized yet');
81ok(!exists($root->right->{'$:right'}), '... right attribute still has not been initialized yet');
82