15 $instance = bless {@_}, $class;
19 sub foo { shift->{foo} }
29 $class->meta->new_object(
30 __INSTANCE__ => $class->SUPER::new(@_),
35 __PACKAGE__->meta->add_attribute(
40 my ($value, $writer, $attr) = @_;
49 my $foo = Foo::Sub->new;
51 isa_ok($foo, 'Foo::Sub');
52 is($foo, $instance, "used the passed-in instance");
57 my $foo = Foo::Sub->new(foo => 'FOO');
59 isa_ok($foo, 'Foo::Sub');
60 is($foo, $instance, "used the passed-in instance");
61 is($foo->foo, 'FOO', "set non-CMOP constructor args");
66 my $foo = Foo::Sub->new(bar => 'bar');
68 isa_ok($foo, 'Foo::Sub');
69 is($foo, $instance, "used the passed-in instance");
70 is($foo->bar, 'BAR', "set CMOP attributes");
75 my $foo = Foo::Sub->new(foo => 'FOO', bar => 'bar');
77 isa_ok($foo, 'Foo::Sub');
78 is($foo, $instance, "used the passed-in instance");
79 is($foo->foo, 'FOO', "set non-CMOP constructor arg");
80 is($foo->bar, 'BAR', "set correct CMOP attribute");
88 $instance = bless {@_};
92 sub foo { shift->{foo} }
102 $class->meta->new_object(
103 __INSTANCE__ => $class->SUPER::new(@_),
108 __PACKAGE__->meta->add_attribute(
113 my ($value, $writer, $attr) = @_;
114 $writer->(uc $value);
120 like( exception { BadFoo::Sub->new }, qr/BadFoo=HASH.*is not a BadFoo::Sub/, "error with incorrect constructors" );
123 my $meta = Class::MOP::Class->create('Really::Bad::Foo');
125 $meta->new_object(__INSTANCE__ => (bless {}, 'Some::Other::Class'))
126 }, qr/Some::Other::Class=HASH.*is not a Really::Bad::Foo/, "error with completely invalid class" );
130 my $meta = Class::MOP::Class->create('Really::Bad::Foo::2');
131 for my $invalid ('foo', 1, 0, '') {
133 $meta->new_object(__INSTANCE__ => $invalid)
134 }, qr/The __INSTANCE__ parameter must be a blessed reference, not $invalid/, "error with unblessed thing" );