File: 59valueconstraints.t

package info (click to toggle)
libkavorka-perl 0.039-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 696 kB
  • sloc: perl: 2,850; makefile: 6; sh: 6
file content (70 lines) | stat: -rw-r--r-- 1,461 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
=pod

=encoding utf-8

=head1 PURPOSE

Check that value constraints work.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017 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 Kavorka;
use match::simple qw(match);

fun foo (Int $x where [2,4,6,8] = 2, $y = 0) {
	return $x + $y;
}

fun bar (Int $x where { match $_, [2,4,6,8] } = 4, $y = 0) {
	return $x + $y;
}

fun baz ($x where { match $_, [2,4,6,8] } = 6, $y = 0) {
	return $x + $y;
}

subtest "smartmatch-style value constraint" => fun
{
	is(foo(), 2);
	is(foo(8, 1), 9);
	like(exception { foo(1.1) }, qr/^Value "?1\.1"? did not pass type constraint "?Int"?/);
	like(exception { foo(111) }, qr/^\$x failed value constraint/);
	done_testing;
};

subtest "block value constraint" => fun
{
	is(bar(), 4);
	is(bar(8, 1), 9);
	like(exception { bar(1.1) }, qr/^Value "?1\.1"? did not pass type constraint "?Int"?/);
	like(exception { bar(111) }, qr/^\$x failed value constraint/);
	done_testing;
};

subtest "value constraint with no type constraint" => fun
{
	is(baz(), 6);
	is(baz(8), 8);
	like(exception { baz(1.1) }, qr/^\$x failed value constraint/);
	like(exception { baz(111) }, qr/^\$x failed value constraint/);
	done_testing;
};

done_testing;