Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 047_rebless_with_extra_params.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal;
6
7 use Class::MOP;
8
9 {
10     package Foo;
11     use metaclass;
12     Foo->meta->add_attribute('bar' => (reader => 'bar'));
13
14     sub new { (shift)->meta->new_object(@_) }
15
16     package Bar;
17     use metaclass;
18     use base 'Foo';
19     Bar->meta->add_attribute('baz' => (reader => 'baz', default => 'BAZ'));
20 }
21
22 # normal ...
23 {
24     my $foo = Foo->new(bar => 'BAR');
25     isa_ok($foo, 'Foo');
26
27     is($foo->bar, 'BAR', '... got the expect value');
28     ok(!$foo->can('baz'), '... no baz method though');
29
30     is( exception {
31         Bar->meta->rebless_instance($foo)
32     }, undef, '... this works' );
33
34     is($foo->bar, 'BAR', '... got the expect value');
35     ok($foo->can('baz'), '... we have baz method now');
36     is($foo->baz, 'BAZ', '... got the expect value');
37
38     is( exception {
39         Foo->meta->rebless_instance_back($foo)
40     }, undef, '... this works' );
41     is($foo->bar, 'BAR', '... got the expect value');
42     ok(!$foo->can('baz'), '... no baz method though');
43 }
44
45 # with extra params ...
46 {
47     my $foo = Foo->new(bar => 'BAR');
48     isa_ok($foo, 'Foo');
49
50     is($foo->bar, 'BAR', '... got the expect value');
51     ok(!$foo->can('baz'), '... no baz method though');
52
53     is( exception {
54         Bar->meta->rebless_instance($foo, (baz => 'FOO-BAZ'))
55     }, undef, '... this works' );
56
57     is($foo->bar, 'BAR', '... got the expect value');
58     ok($foo->can('baz'), '... we have baz method now');
59     is($foo->baz, 'FOO-BAZ', '... got the expect value');
60
61     is( exception {
62         Foo->meta->rebless_instance_back($foo)
63     }, undef, '... this works' );
64
65     is($foo->bar, 'BAR', '... got the expect value');
66     ok(!$foo->can('baz'), '... no baz method though');
67     ok(!exists($foo->{baz}), '... and the baz attribute was deinitialized');
68 }
69
70 # with extra params ...
71 {
72     my $foo = Foo->new(bar => 'BAR');
73     isa_ok($foo, 'Foo');
74
75     is($foo->bar, 'BAR', '... got the expect value');
76     ok(!$foo->can('baz'), '... no baz method though');
77
78     is( exception {
79         Bar->meta->rebless_instance($foo, (bar => 'FOO-BAR', baz => 'FOO-BAZ'))
80     }, undef, '... this works' );
81
82     is($foo->bar, 'FOO-BAR', '... got the expect value');
83     ok($foo->can('baz'), '... we have baz method now');
84     is($foo->baz, 'FOO-BAZ', '... got the expect value');
85
86     is( exception {
87         Foo->meta->rebless_instance_back($foo)
88     }, undef, '... this works' );
89
90     is($foo->bar, 'FOO-BAR', '... got the expect value');
91     ok(!$foo->can('baz'), '... no baz method though');
92     ok(!exists($foo->{baz}), '... and the baz attribute was deinitialized');
93 }
94
95 done_testing;