actually use documented defaults for custom keywords
[p5sagit/Function-Parameters.git] / t / types_custom.t
CommitLineData
52c18b0f 1#!perl
2use warnings FATAL => 'all';
3use strict;
4
11305599 5use Test::More tests => 8;
52c18b0f 6use Test::Fatal;
7
8use Function::Parameters qw(:strict);
11305599 9use Function::Parameters {
10 def => { check_argument_count => 1 },
11};
52c18b0f 12
13{
14 package MyTC;
15
16 method new(
17 $class:
18 $name,
19 $check,
20 $get_message = fun ($value) {
21 "Validation failed for constraint '$name' with value '$value'"
22 },
23 ) {
24 bless {
25 name => $name,
26 check => $check,
27 get_message => $get_message,
28 }, $class
29 }
30
31 method check($value) {
32 $self->{check}($value)
33 }
34
35 method get_message($value) {
36 $self->{get_message}($value)
37 }
38}
39
40use constant {
41 TEvenNum => MyTC->new('even number' => fun ($n) { $n =~ /^[0-9]+\z/ && $n % 2 == 0 }),
42 TShortStr => MyTC->new('short string' => fun ($s) { length($s) < 10 }),
43};
44
45fun foo((TEvenNum) $x, (TShortStr) $y) {
46 "$x/$y"
47}
48
49is foo(42, "hello"), "42/hello";
50like exception { foo 41, "hello" }, qr{\bValidation failed for constraint 'even number' with value '41'};
51like exception { foo 42, "1234567890~" }, qr{\bValidation failed for constraint 'short string' with value '1234567890~'};
52like exception { foo 41, "1234567890~" }, qr{\bValidation failed for constraint 'even number' with value '41'};
11305599 53
54def foo2((TEvenNum) $x, (TShortStr) $y) {
55 "$x/$y"
56}
57
58is foo2(42, "hello"), "42/hello";
59like exception { foo2 41, "hello" }, qr{\bValidation failed for constraint 'even number' with value '41'};
60like exception { foo2 42, "1234567890~" }, qr{\bValidation failed for constraint 'short string' with value '1234567890~'};
61like exception { foo2 41, "1234567890~" }, qr{\bValidation failed for constraint 'even number' with value '41'};