File: compile-named-oo.t

package info (click to toggle)
libtype-tiny-perl 1.004004-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,128 kB
  • sloc: perl: 8,761; makefile: 7
file content (86 lines) | stat: -rw-r--r-- 2,532 bytes parent folder | download
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
=pod

=encoding utf-8

=head1 PURPOSE

Test L<Type::Params> C<compile_named_oo> function.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2018-2019 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 Test::Fatal;

use Type::Params qw( compile_named_oo );
use Types::Standard qw( -types );


my $coderef = compile_named_oo(
	foo    => Int,
	bar    => Optional[Int],
	baz    => Optional[HashRef], { getter => 'bazz', predicate => 'haz' },
);

ok(CodeRef->check($coderef), 'compile_named_oo returns a coderef');

my @object;
$object[0] = $coderef->(  foo => 42, bar => 69, baz => { quux => 666 }  );
$object[1] = $coderef->({ foo => 42, bar => 69, baz => { quux => 666 } });
$object[2] = $coderef->(  foo => 42  );
$object[3] = $coderef->({ foo => 42 });

for my $i (0 .. 1) {
	ok(Object->check($object[$i]), "\$object[$i] is an object");
	can_ok($object[$i], qw( foo bar has_bar bazz haz ));
	is($object[$i]->foo, 42, "\$object[$i]->foo == 42");
	is($object[$i]->bar, 69, "\$object[$i]->bar == 69");
	is($object[$i]->bazz->{quux}, 666, "\$object[$i]->bazz->{quux} == 666");
	ok($object[$i]->has_bar, "\$object[$i]->has_bar");
	ok($object[$i]->haz, "\$object[$i]->haz");
	ok(! $object[$i]->can("has_foo"), 'no has_foo method');
	ok(! $object[$i]->can("has_baz"), 'no has_baz method');
}

for my $i (2 .. 3) {
	ok(Object->check($object[$i]), "\$object[$i] is an object");
	can_ok($object[$i], qw( foo bar has_bar bazz haz ));
	is($object[$i]->foo, 42, "\$object[$i]->foo == 42");
	is($object[$i]->bar, undef, "not defined \$object[$i]->bar");
	is($object[$i]->bazz, undef, "not defined \$object[$i]->bazz");
	ok(! $object[$i]->has_bar, "!\$object[$i]->has_bar");
	ok(! $object[$i]->haz, "!\$object[$i]->haz");
	ok(! $object[$i]->can("has_foo"), 'no has_foo method');
	ok(! $object[$i]->can("has_baz"), 'no has_baz method');
}


my $e = exception {
	compile_named_oo( 999 => Int );
};
ok(Object->check($e), 'exception thrown for bad accessor name');
like($e->message, qr/bad accessor name/i, 'correct message');


my $coderef2 = compile_named_oo(
	bar    => Optional[ArrayRef],
	baz    => Optional[CodeRef], { getter => 'bazz', predicate => 'haz' },
	foo    => Num,
);
my $coderef2obj = $coderef2->(foo => 1.1, bar => []);
is(ref($object[0]), ref($coderef2obj), 'packages reused when possible');


done_testing;