instance-refactored
[gitmo/Class-MOP.git] / t / 060_instance.t
CommitLineData
2bab2be6 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
49c93440 6use Test::More tests => 25;
2bab2be6 7use Test::Exception;
8
2bab2be6 9BEGIN {
10 use_ok('Class::MOP::Instance');
11}
12
de943e6a 13can_ok( "Class::MOP::Instance", $_ ) for qw/
49c93440 14 new
15
de943e6a 16 create_instance
17 bless_instance_structure
18
49c93440 19 get_all_slots
de943e6a 20
21 get_slot_value
22 set_slot_value
de943e6a 23
24 inline_get_slot_value
25 inline_set_slot_value
de943e6a 26/;
27
28{
29 package Foo;
30 use metaclass;
49c93440 31
32 Foo->meta->add_attribute('moosen');
de943e6a 33
34 package Bar;
35 use metaclass;
36 use base qw/Foo/;
de943e6a 37
49c93440 38 Bar->meta->add_attribute('elken');
39}
de943e6a 40
49c93440 41my $mi_foo = Foo->meta->get_meta_instance;
42isa_ok($mi_foo, "Class::MOP::Instance");
de943e6a 43
49c93440 44is_deeply(
45 [ $mi_foo->get_all_slots ],
46 [ "moosen" ],
47 '... get all slots for Foo');
de943e6a 48
49my $mi_bar = Bar->meta->get_meta_instance;
49c93440 50isa_ok($mi_bar, "Class::MOP::Instance");
de943e6a 51
49c93440 52isnt($mi_foo, $mi_bar, '... they are not the same instance');
de943e6a 53
49c93440 54is_deeply(
55 [ sort $mi_bar->get_all_slots ],
56 [ "elken", "moosen" ],
57 '... get all slots for Bar');
de943e6a 58
49c93440 59my $i_foo = $mi_foo->create_instance;
60isa_ok($i_foo, "Foo");
de943e6a 61
49c93440 62{
63 my $i_foo_2 = $mi_foo->create_instance;
64 isa_ok($i_foo_2, "Foo");
65 isnt($i_foo_2, $i_foo, '... not the same instance');
66 is_deeply($i_foo, $i_foo_2, '... but the same structure');
67}
de943e6a 68
49c93440 69ok(!defined($mi_foo->get_slot_value( $i_foo, "moosen" )), "... no value for slot");
de943e6a 70
de943e6a 71$mi_foo->set_slot_value( $i_foo, "moosen", "the value" );
72
49c93440 73is($mi_foo->get_slot_value( $i_foo, "moosen" ), "the value", "... get slot value");
74
75ok(!$i_foo->can('moosen'), '... Foo cant moosen');
76
77eval 'sub Foo::moosen { ' . $mi_foo->inline_get_slot_value( '$_[0]', 'moosen' ) . ' }';
78ok(!$@, "compilation of inline get value had no error");
de943e6a 79
49c93440 80can_ok($i_foo, 'moosen');
de943e6a 81
49c93440 82is($i_foo->moosen, "the value", "... inline get value worked");
de943e6a 83
84$mi_foo->set_slot_value( $i_foo, "moosen", "the other value" );
85
49c93440 86is($i_foo->moosen, "the other value", "... inline get value worked (even after value is changed)");