addin in the extra params for rebless_instance
[gitmo/Class-MOP.git] / t / 047_rebless_with_extra_params.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 22;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP');
11 }
12
13 {
14     package Foo;
15     use metaclass;
16     Foo->meta->add_attribute('bar' => (reader => 'bar'));
17     
18     sub new { (shift)->meta->new_object(@_) }
19     
20     package Bar;
21     use metaclass;
22     use base 'Foo';
23     Bar->meta->add_attribute('baz' => (reader => 'baz', default => 'BAZ'));    
24 }
25
26 # normal ...
27 {
28     my $foo = Foo->new(bar => 'BAR');
29     isa_ok($foo, 'Foo');
30
31     is($foo->bar, 'BAR', '... got the expect value');
32     ok(!$foo->can('baz'), '... no baz method though');
33
34     lives_ok {
35         Bar->meta->rebless_instance($foo)
36     } '... this works';
37
38     is($foo->bar, 'BAR', '... got the expect value');
39     ok($foo->can('baz'), '... we have baz method now');
40     is($foo->baz, 'BAZ', '... got the expect value');
41 }
42
43 # with extra params ...
44 {
45     my $foo = Foo->new(bar => 'BAR');
46     isa_ok($foo, 'Foo');
47
48     is($foo->bar, 'BAR', '... got the expect value');
49     ok(!$foo->can('baz'), '... no baz method though');
50
51     lives_ok {
52         Bar->meta->rebless_instance($foo, (baz => 'FOO-BAZ'))
53     } '... this works';
54
55     is($foo->bar, 'BAR', '... got the expect value');
56     ok($foo->can('baz'), '... we have baz method now');
57     is($foo->baz, 'FOO-BAZ', '... got the expect value');
58 }
59
60 # with extra params ...
61 {
62     my $foo = Foo->new(bar => 'BAR');
63     isa_ok($foo, 'Foo');
64
65     is($foo->bar, 'BAR', '... got the expect value');
66     ok(!$foo->can('baz'), '... no baz method though');
67
68     lives_ok {
69         Bar->meta->rebless_instance($foo, (bar => 'FOO-BAR', baz => 'FOO-BAZ'))
70     } '... this works';
71
72     is($foo->bar, 'FOO-BAR', '... got the expect value');
73     ok($foo->can('baz'), '... we have baz method now');
74     is($foo->baz, 'FOO-BAZ', '... got the expect value');
75 }
76
77