File: plus-constructors.t

package info (click to toggle)
libtype-tiny-perl 2.002001-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,948 kB
  • sloc: perl: 14,610; makefile: 2; sh: 1
file content (127 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
=pod

=encoding utf-8

=head1 PURPOSE

Checks the C<Type::Tiny::Class>'s C<plus_constructors> method.

=head1 DEPENDENCIES

Requires Moose 2.00; skipped otherwise.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017-2023 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 lib qw( . ./t ../inc ./inc );
use utf8;

use Test::More;
use Test::Requires { Moose => 2.00 };
use Test::TypeTiny;

my ($Address, $Person);

BEGIN {
	package Address;
	
	use Moose;
	use Types::Standard qw( Str );
	use Type::Utils;
	
	has [qw/ line1 line2 town county postcode country /] => (
		is       => "ro",
		isa      => Str,
	);
	
	sub _new_from_array
	{
		my $class = shift;
		my @addr  = ref($_[0]) ? @{$_[0]} : @_;
		$class->new(
			line1     => $addr[0],
			line2     => $addr[1],
			town      => $addr[2],
			county    => $addr[3],
			postcode  => $addr[4],
			country   => $addr[5],
		);
	}
	
	$Address = class_type { class => __PACKAGE__ };
};

BEGIN {
	package Person;
	
	use Moose;
	use Types::Standard qw( Str Join Tuple HashRef );
	use Type::Utils;
	
	has name => (
		required => 1,
		coerce   => 1,
		is       => "ro",
		isa      => Str->plus_coercions(Join[" "]),
	);
	
	has addr => (
		coerce   => 1,
		is       => "ro",
		isa      => $Address->plus_constructors(
			(Tuple[(Str) x 6]) => "_new_from_array",
			(HashRef)          => "new",
		),
	);
	
	sub _new_from_name
	{
		my $class = shift;
		my ($name) = @_;
		$class->new(name => $name);
	}
	
	$Person = class_type { class => __PACKAGE__ };
};

ok(
	"Person"->meta->get_attribute("addr")->type_constraint->is_a_type_of($Address),
	q["Person"->meta->get_attribute("addr")->type_constraint->is_a_type_of($Address)],
);

my $me = Person->new(
	name   => ["Toby", "Inkster"],
	addr   => ["Flat 2, 39 Hartington Road", "West Ealing", "LONDON", "", "W13 8QL", "United Kingdom"],
);

my $me2 = Person->new(
	name   => "Toby Inkster",
	addr   => Address->new(
		line1     => "Flat 2, 39 Hartington Road",
		line2     => "West Ealing",
		town      => "LONDON",
		county    => "",
		postcode  => "W13 8QL",
		country   => "United Kingdom",
	),
);

is_deeply($me, $me2, 'coercion worked');

my $you = $Person->plus_constructors->coerce({ name => "Livvy" });
my $you2 = Person->new(name => "Livvy");
is_deeply($you, $you2, 'coercion worked (plus_constructors with no parameters)');

done_testing;