Commits

Toby Inkster committed 03f7f24

fix MooX::Struct::Processor internals issue

Comments (0)

Files changed (2)

lib/MooX/Struct.pm

 			{
 				my $opts   = Data::OptList::mkopt($proto);
 				my $klass  = $self->create_class($opts);
+				my $seen_extends;
 				my @fields = _uniq map {
-					$self->process_argument($klass, @$_)
+					++$seen_extends if $_->[0] =~ /^-(?:extends|isa)$/;
+					$self->process_argument($klass, @$_);
 				} @$opts;
+				unshift @fields, $self->base->FIELDS
+					if !$seen_extends && $self->base->can('FIELDS');
 				$self->process_method($klass, FIELDS => sub { @fields });
 				$self->process_method($klass, TYPE   => sub { $subname }) if defined $subname;
 				$proto = $klass;
+=head1 PURPOSE
+
+Tests that a MooX::Struct::Processor, configured with a base class
+that has some attributes, will generate structs that are aware of
+those attributes (shows them in C<FIELDS>).
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=cut
+
+use strict;
+use warnings;
+use Test::More;
+
+use MooX::Struct Foo => ['$foo'];
+
+BEGIN {
+	"MooX::Struct::Processor"
+		-> new(
+			base  => Foo,
+			flags => { retain => 1 },
+		)
+		-> process(
+			main => (
+				Bar => ['$bar'],
+				Baz => ['$baz', -class => \'Baz'],
+			),
+		)
+	;
+};
+
+is_deeply(
+	[ Foo->FIELDS ],
+	[ qw( foo ) ],
+);
+
+isa_ok(Bar, Foo);
+
+is_deeply(
+	[ Bar->FIELDS ],
+	[ qw( foo bar ) ],
+);
+
+isa_ok(Baz, Foo);
+
+is_deeply(
+	[ Baz->FIELDS ],
+	[ qw( foo baz ) ],
+);
+
+my $bar = Bar[1, 2];
+
+is($bar->foo, 1);
+is($bar->bar, 2);
+
+done_testing;
+