File: types_custom.t

package info (click to toggle)
libfunction-parameters-perl 2.002005-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: perl: 3,945; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (3)
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
#!perl
use warnings FATAL => 'all';
use strict;

use Test::More tests => 8;
use Test::Fatal;

use Function::Parameters qw(:strict);
use Function::Parameters {
    def => { strict => 1 },
};

{
    package MyTC;

    method new(
        $class:
        $name,
        $check,
        $get_message = fun ($value) {
            "Validation failed for constraint '$name' with value '$value'"
        },
    ) {
        bless {
            name => $name,
            check => $check,
            get_message => $get_message,
        }, $class
    }

    method check($value) {
        $self->{check}($value)
    }

    method get_message($value) {
        $self->{get_message}($value)
    }
}

use constant {
    TEvenNum  => MyTC->new('even number'  => fun ($n) { $n =~ /^[0-9]+\z/ && $n % 2 == 0 }),
    TShortStr => MyTC->new('short string' => fun ($s) { length($s) < 10 }),
};

fun foo((TEvenNum) $x, (TShortStr) $y) {
    "$x/$y"
}

is foo(42, "hello"), "42/hello";
like exception { foo 41, "hello" },       qr{\bValidation failed for constraint 'even number' with value '41'};
like exception { foo 42, "1234567890~" }, qr{\bValidation failed for constraint 'short string' with value '1234567890~'};
like exception { foo 41, "1234567890~" }, qr{\bValidation failed for constraint 'even number' with value '41'};

def foo2((TEvenNum) $x, (TShortStr) $y) {
    "$x/$y"
}

is foo2(42, "hello"), "42/hello";
like exception { foo2 41, "hello" },       qr{\bValidation failed for constraint 'even number' with value '41'};
like exception { foo2 42, "1234567890~" }, qr{\bValidation failed for constraint 'short string' with value '1234567890~'};
like exception { foo2 41, "1234567890~" }, qr{\bValidation failed for constraint 'even number' with value '41'};