use style; in MOP.xs.
[gitmo/Class-MOP.git] / t / 106_LazyClass_test.t
CommitLineData
4300f56a 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
efd3d14c 6use Test::More tests => 25;
4300f56a 7use File::Spec;
8
efd3d14c 9BEGIN {use Class::MOP;
10dd437b 10 require_ok(File::Spec->catfile('examples', 'LazyClass.pod'));
4300f56a 11}
12
13{
14 package BinaryTree;
15
1becdfcc 16 use metaclass (
c23184fc 17 'attribute_metaclass' => 'LazyClass::Attribute',
18 'instance_metaclass' => 'LazyClass::Instance',
677eb158 19 );
4300f56a 20
1aeb4c53 21 BinaryTree->meta->add_attribute('node' => (
4300f56a 22 accessor => 'node',
c23184fc 23 init_arg => 'node'
4300f56a 24 ));
25
1aeb4c53 26 BinaryTree->meta->add_attribute('left' => (
4300f56a 27 reader => 'left',
28 default => sub { BinaryTree->new() }
29 ));
30
1aeb4c53 31 BinaryTree->meta->add_attribute('right' => (
4300f56a 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
c23184fc 42my $root = BinaryTree->new('node' => 0);
4300f56a 43isa_ok($root, 'BinaryTree');
44
1aeb4c53 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');
4300f56a 48
49isa_ok($root->left, 'BinaryTree');
50isa_ok($root->right, 'BinaryTree');
51
1aeb4c53 52ok(exists($root->{'left'}), '... left attribute has now been initialized');
53ok(exists($root->{'right'}), '... right attribute has now been initialized');
4300f56a 54
1aeb4c53 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');
4300f56a 58
1aeb4c53 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');
4300f56a 62
63is($root->left->node(), undef, '... the left node is uninitialized');
64
1aeb4c53 65ok(exists($root->left->{'node'}), '... node attribute has now been initialized');
4300f56a 66
67$root->left->node(1);
68is($root->left->node(), 1, '... the left node == 1');
69
1aeb4c53 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');
4300f56a 72
73is($root->right->node(), undef, '... the right node is uninitialized');
74
1aeb4c53 75ok(exists($root->right->{'node'}), '... node attribute has now been initialized');
4300f56a 76
77$root->right->node(2);
78is($root->right->node(), 2, '... the right node == 1');
79
1aeb4c53 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');
4300f56a 82