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