File: 98baseclass.t

package info (click to toggle)
libmoox-struct-perl 0.020-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 288 kB
  • sloc: perl: 430; sh: 6; makefile: 2
file content (115 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
=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);

# The "interesting" thing about this package is that it provides
# no FIELDS method.
BEGIN {
	package Quux;
	use Moo;
	has bumf => (is => 'rw');
};

BEGIN {
	"MooX::Struct::Processor"
		-> new(
			base  => 'Quux',
			flags => { retain => 1 },
		)
		-> process(
			main => (
				Quuux => ['$xyzzy'],
				Quuuux => (),
			),
		)
	;
};

my $quuux = Quuux->new(xyzzy => 4, bumf => 2);
is_deeply([$quuux->FIELDS], ['xyzzy']);
is($quuux->xyzzy, 4);
is($quuux->bumf, 2);
like(ref $quuux, qr{^Quux::__ANON__::});

my $quuuux = Quuuux->new(bumf => 2);
is_deeply([$quuuux->FIELDS], []);
is($quuuux->bumf, 2);
like(ref $quuuux, qr{^Quux::__ANON__::});


BEGIN {
	"MooX::Struct::Processor"
		-> new(
			flags => { retain => 1 },
		)
		-> process(
			main => (
				Quux2 => [ -extends=>['Quux'], '$xyzzy' ],
			),
		)
	;
};
is_deeply([Quux2->FIELDS], ['xyzzy']);

done_testing;