Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / cmop / LazyClass_test.t
CommitLineData
38bf2a25 1use strict;
2use warnings;
3
4use Test::More;
5use File::Spec;
6
7use Class::MOP;
8
9BEGIN {
10 require_ok(File::Spec->catfile('examples', 'LazyClass.pod'));
11}
12
13{
14 package BinaryTree;
15
16 use metaclass (
17 'attribute_metaclass' => 'LazyClass::Attribute',
18 'instance_metaclass' => 'LazyClass::Instance',
19 );
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->new_object(@_) => $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
83done_testing;