File: plus.t

package info (click to toggle)
libtype-tiny-perl 2.004000-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,856 kB
  • sloc: perl: 14,858; makefile: 2; sh: 1
file content (87 lines) | stat: -rw-r--r-- 2,108 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
87
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Test::TypeTiny;

use Types::Standard qw( ArrayRef );
use Type::Tiny::Bitfield (
	Colour  => { RED => 0x01, BLUE => 0x02, GREEN => 0x04 },
	Style   => { DOTTED => 0x08, ZIGZAG => 0x10, BLINK => 0x20 },
);

my $Combined = Colour + Style;

ok( $Combined->isa('Type::Tiny::Bitfield'), "$Combined isa Type::Tiny::Bitfield" );
is( $Combined->display_name, 'Colour+Style', "$Combined display_name" );
ok( $Combined->is_anon, "$Combined is_anon" );

should_pass( $_, $Combined ) for 0 .. 0x3F;
should_fail( $_, $Combined ) for 0x40, 'BLEH', [], -1, undef, ArrayRef;

is( $Combined->coerce( 'RED|GREEN|ZIGZAG' ), 21, 'coerce' );

like(
	exception {
		my $x = Colour + ArrayRef;
	},
	qr/Bad overloaded operation/,
	'Exception when trying to add Bitfield type and non-Bitfield type',
);

like(
	exception {
		my $x = ArrayRef() + Colour;
	},
	qr/Bad overloaded operation/,
	'Exception when trying to add non-Bitfield type and Bitfield type',
);

like(
	exception {
		my $x = Colour + [];
	},
	qr/Bad overloaded operation/,
	'Exception when trying to add Bitfield type and non-type',
);

like(
	exception {
		my $x = [] + Colour;
	},
	qr/Bad overloaded operation/,
	'Exception when trying to add non-type and Bitfield type',
);

like(
	exception {
		my $x = Colour + Type::Tiny::Bitfield->new(
			name   => 'Shape',
			values => { CIRCLE => 0x40, BLUE => 0x80 },
		);
	},
	qr/Conflicting value: BLUE/,
	'Exception when trying to combine conflicting Bitfield types',
);

my $zzz = 0;
sub combine_types_with_coercions {
	my ( $x, $y ) = map {
		my $coercion = $_;
		++$zzz;
		Type::Tiny::Bitfield->new(
			values   => { "ZZZ$zzz" => 2 ** $zzz },
			coercion => $coercion,
		);
	} @_;
	return $x + $y;
}

subtest 'Combining Bitfield types with and without coercions works' => sub {
	ok( ! combine_types_with_coercions( undef, undef )->has_coercion );
	ok(   combine_types_with_coercions( undef, 1 )->has_coercion );
	ok(   combine_types_with_coercions( 1, undef )->has_coercion );
	ok(   combine_types_with_coercions( 1, 1 )->has_coercion );
};

done_testing;