File: 08alt-constructor.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 (72 lines) | stat: -rw-r--r-- 1,382 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
=head1 PURPOSE

Check square-bracket-style constructor.

Also checks constructor called with a hashref (works, but not officially
supported).

Tests that objects overloading both hash and array are considered to be
hashrefs by the constructor, not arrayrefs.

=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 Test::More;
use MooX::Struct
	Point   => [ qw( +x +y ) ],
	Point3D => [ -extends => [qw(Point)], qw( +z ) ],
;

my $point1 = Point[3, 4];
my $point2 = Point3D[3, 4, 5];
my $point3 = Point3D[3, 4];

is("$point1", "3 4");
is("$point2", "3 4 5");
is("$point3", "3 4 0");

is(
	Point3D->new([3, 4, 5])->TO_STRING,
	"3 4 5",
);

is(
	Point3D->new({ z=>1, y=>2, x=>3, y=>4, z=>5 })->TO_STRING,
	"3 4 5",
);

ok not eval {
	Point3D->new( \*STDERR )
};

ok not eval {
	Point3D[1, 2, 3, 4]
};

{
	package Local::WeirdHash;
	use overload '@{}' => 'TO_ARRAY';
	sub TO_ARRAY {
		my $self = shift;
		[ sort keys %$self ];
	}
}

my $weird = bless { z=>1, y=>2, x=>3, y=>4, z=>5 }, 'Local::WeirdHash';
is(
	Point3D->new($weird)->TO_STRING,
	"3 4 5",
	'if constructed with an object that "does" array and hash, hash is preferred',
);

done_testing();