Redo conversion to Test::Fatal
[gitmo/Class-MOP.git] / t / 047_rebless_with_extra_params.t
CommitLineData
214e4bd7 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
871e9eb5 5use Test::Fatal;
214e4bd7 6
efd3d14c 7use Class::MOP;
214e4bd7 8
9{
10 package Foo;
11 use metaclass;
12 Foo->meta->add_attribute('bar' => (reader => 'bar'));
86a4d873 13
214e4bd7 14 sub new { (shift)->meta->new_object(@_) }
86a4d873 15
214e4bd7 16 package Bar;
17 use metaclass;
18 use base 'Foo';
86a4d873 19 Bar->meta->add_attribute('baz' => (reader => 'baz', default => 'BAZ'));
214e4bd7 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
871e9eb5 30 is( exception {
214e4bd7 31 Bar->meta->rebless_instance($foo)
871e9eb5 32 }, undef, '... this works' );
214e4bd7 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');
0708313d 37
871e9eb5 38 is( exception {
0708313d 39 Foo->meta->rebless_instance_back($foo)
871e9eb5 40 }, undef, '... this works' );
0708313d 41 is($foo->bar, 'BAR', '... got the expect value');
42 ok(!$foo->can('baz'), '... no baz method though');
214e4bd7 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
871e9eb5 53 is( exception {
214e4bd7 54 Bar->meta->rebless_instance($foo, (baz => 'FOO-BAZ'))
871e9eb5 55 }, undef, '... this works' );
214e4bd7 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');
0708313d 60
871e9eb5 61 is( exception {
0708313d 62 Foo->meta->rebless_instance_back($foo)
871e9eb5 63 }, undef, '... this works' );
0708313d 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');
214e4bd7 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
871e9eb5 78 is( exception {
214e4bd7 79 Bar->meta->rebless_instance($foo, (bar => 'FOO-BAR', baz => 'FOO-BAZ'))
871e9eb5 80 }, undef, '... this works' );
214e4bd7 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');
0708313d 85
871e9eb5 86 is( exception {
0708313d 87 Foo->meta->rebless_instance_back($foo)
871e9eb5 88 }, undef, '... this works' );
0708313d 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');
214e4bd7 93}
94
86a4d873 95done_testing;