Skip to content

Commit 0032151

Browse files
committed
boolean.pm and builtin::{true,false} tests
1 parent 92607fb commit 0032151

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+428
-3
lines changed

devel.maint-scripts/add-tests.pl

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
# bleh => 'regexp' => qr/./,
88
my $new = <<'NEW_LINES';
9-
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
10-
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
9+
fail => 'boolean::false' => boolean::false,
10+
fail => 'boolean::true' => boolean::true,
11+
fail => 'builtin::false' => do { builtin->can('false') ? builtin::false() : !!0 },
12+
fail => 'builtin::true' => do { builtin->can('true') ? builtin::true() : !!1 },
1113
NEW_LINES
1214

1315
my $dir = path('t/21-types/');

inc/boolean.pm

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use strict; use warnings;
2+
package boolean;
3+
our $VERSION = '0.46';
4+
5+
my ($true, $false);
6+
7+
use overload
8+
'""' => sub { ${$_[0]} },
9+
'!' => sub { ${$_[0]} ? $false : $true },
10+
fallback => 1;
11+
12+
use base 'Exporter';
13+
@boolean::EXPORT = qw(true false boolean);
14+
@boolean::EXPORT_OK = qw(isTrue isFalse isBoolean);
15+
%boolean::EXPORT_TAGS = (
16+
all => [@boolean::EXPORT, @boolean::EXPORT_OK],
17+
test => [qw(isTrue isFalse isBoolean)],
18+
);
19+
20+
sub import {
21+
my @options = grep $_ ne '-truth', @_;
22+
$_[0]->truth if @options != @_;
23+
@_ = @options;
24+
goto &Exporter::import;
25+
}
26+
27+
my ($true_val, $false_val, $bool_vals);
28+
29+
BEGIN {
30+
my $t = 1;
31+
my $f = 0;
32+
$true = do {bless \$t, 'boolean'};
33+
$false = do {bless \$f, 'boolean'};
34+
35+
$true_val = overload::StrVal($true);
36+
$false_val = overload::StrVal($false);
37+
$bool_vals = {$true_val => 1, $false_val => 1};
38+
}
39+
40+
# refaddrs change on thread spawn, so CLONE fixes them up
41+
sub CLONE {
42+
$true_val = overload::StrVal($true);
43+
$false_val = overload::StrVal($false);
44+
$bool_vals = {$true_val => 1, $false_val => 1};
45+
}
46+
47+
sub true() { $true }
48+
sub false() { $false }
49+
sub boolean($) {
50+
die "Not enough arguments for boolean::boolean" if scalar(@_) == 0;
51+
die "Too many arguments for boolean::boolean" if scalar(@_) > 1;
52+
return not(defined $_[0]) ? false :
53+
"$_[0]" ? $true : $false;
54+
}
55+
sub isTrue($) {
56+
not(defined $_[0]) ? false :
57+
(overload::StrVal($_[0]) eq $true_val) ? true : false;
58+
}
59+
sub isFalse($) {
60+
not(defined $_[0]) ? false :
61+
(overload::StrVal($_[0]) eq $false_val) ? true : false;
62+
}
63+
sub isBoolean($) {
64+
not(defined $_[0]) ? false :
65+
(exists $bool_vals->{overload::StrVal($_[0])}) ? true : false;
66+
}
67+
68+
sub truth {
69+
die "-truth not supported on Perl 5.22 or later" if $] >= 5.021005;
70+
# enable modifying true and false
71+
&Internals::SvREADONLY( \ !!0, 0);
72+
&Internals::SvREADONLY( \ !!1, 0);
73+
# turn perl internal booleans into blessed booleans:
74+
${ \ !!0 } = $false;
75+
${ \ !!1 } = $true;
76+
# make true and false read-only again
77+
&Internals::SvREADONLY( \ !!0, 1);
78+
&Internals::SvREADONLY( \ !!1, 1);
79+
}
80+
81+
sub TO_JSON { ${$_[0]} ? \1 : \0 }
82+
83+
1;

meta/rights.pret

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ f`t/mk-test-manifest.pl`
110110
dc:license <http://dev.perl.org/licenses/>;
111111
dc:rightsHolder cpan:TOBYINK.
112112

113+
f`inc/boolean.pm`
114+
dc:license <http://dev.perl.org/licenses/>;
115+
dc:rightsHolder cpan:INGY.
116+
113117
f`inc/Test/Fatal.pm`
114118
dc:license <http://dev.perl.org/licenses/>;
115119
dc:rightsHolder cpan:RJBS.

t/21-types/Any.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( Any );
2829

2930
isa_ok(Any, 'Type::Tiny', 'Any');
@@ -102,6 +103,10 @@ my @tests = (
102103
pass => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
103104
pass => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
104105
pass => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
106+
pass => 'boolean::false' => boolean::false,
107+
pass => 'boolean::true' => boolean::true,
108+
pass => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
109+
pass => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
105110
#TESTS
106111
);
107112

t/21-types/ArrayLike.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::TypeTiny qw( ArrayLike );
2829

2930
isa_ok(ArrayLike, 'Type::Tiny', 'ArrayLike');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/ArrayRef.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( ArrayRef );
2829

2930
isa_ok(ArrayRef, 'Type::Tiny', 'ArrayRef');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/Bool.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( Bool );
2829

2930
isa_ok(Bool, 'Type::Tiny', 'Bool');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
pass => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
pass => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/BoolLike.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::TypeTiny qw( BoolLike );
2829

2930
isa_ok(BoolLike, 'Type::Tiny', 'BoolLike');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
pass => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
pass => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
pass => 'boolean::false' => boolean::false,
106+
pass => 'boolean::true' => boolean::true,
107+
pass => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
pass => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/ClassName.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( ClassName );
2829

2930
isa_ok(ClassName, 'Type::Tiny', 'ClassName');
@@ -100,6 +101,10 @@ my @tests = (
100101
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
101102
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
102103
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
104+
fail => 'boolean::false' => boolean::false,
105+
fail => 'boolean::true' => boolean::true,
106+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
107+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
103108
#TESTS
104109
);
105110

t/21-types/CodeLike.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::TypeTiny qw( CodeLike );
2829

2930
isa_ok(CodeLike, 'Type::Tiny', 'CodeLike');
@@ -101,6 +102,10 @@ my @tests = (
101102
pass => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/CodeRef.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( CodeRef );
2829

2930
isa_ok(CodeRef, 'Type::Tiny', 'CodeRef');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/ConsumerOf.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( ConsumerOf );
2829

2930
isa_ok(ConsumerOf, 'Type::Tiny', 'ConsumerOf');
@@ -100,6 +101,10 @@ my @tests = (
100101
pass => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
101102
pass => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
102103
pass => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
104+
pass => 'boolean::false' => boolean::false,
105+
pass => 'boolean::true' => boolean::true,
106+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
107+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
103108
#TESTS
104109
);
105110

t/21-types/CycleTuple.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( CycleTuple );
2829

2930
isa_ok(CycleTuple, 'Type::Tiny', 'CycleTuple');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/Defined.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( Defined );
2829

2930
isa_ok(Defined, 'Type::Tiny', 'Defined');
@@ -100,6 +101,10 @@ my @tests = (
100101
pass => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
101102
pass => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
102103
pass => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
104+
pass => 'boolean::false' => boolean::false,
105+
pass => 'boolean::true' => boolean::true,
106+
pass => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
107+
pass => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
103108
#TESTS
104109
);
105110

t/21-types/DelimitedStr.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Common::String qw( DelimitedStr );
2829

2930
isa_ok(DelimitedStr, 'Type::Tiny', 'DelimitedStr');
@@ -100,6 +101,10 @@ my @tests = (
100101
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
101102
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
102103
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
104+
fail => 'boolean::false' => boolean::false,
105+
fail => 'boolean::true' => boolean::true,
106+
pass => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
107+
pass => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
103108
#TESTS
104109
);
105110

t/21-types/Dict.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( Dict );
2829

2930
isa_ok(Dict, 'Type::Tiny', 'Dict');
@@ -101,6 +102,10 @@ my @tests = (
101102
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
102103
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
103104
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
105+
fail => 'boolean::false' => boolean::false,
106+
fail => 'boolean::true' => boolean::true,
107+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
108+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
104109
#TESTS
105110
);
106111

t/21-types/Enum.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( Enum );
2829

2930
isa_ok(Enum, 'Type::Tiny', 'Enum');
@@ -100,6 +101,10 @@ my @tests = (
100101
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
101102
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
102103
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
104+
fail => 'boolean::false' => boolean::false,
105+
fail => 'boolean::true' => boolean::true,
106+
pass => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
107+
pass => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
103108
#TESTS
104109
);
105110

t/21-types/FileHandle.t

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use warnings;
2424
use Test::More;
2525
use Test::Fatal;
2626
use Test::TypeTiny;
27+
use Test::Requires qw(boolean);
2728
use Types::Standard qw( FileHandle );
2829

2930
isa_ok(FileHandle, 'Type::Tiny', 'FileHandle');
@@ -100,6 +101,10 @@ my @tests = (
100101
fail => 'object overloading coderef' => do { package Local::OL::Code; use overload q[&{}] => sub { $_[0][0] }; bless [sub { 1 }] },
101102
fail => 'object booling to false' => do { package Local::OL::BoolFalse; use overload q[bool] => sub { 0 }; bless [] },
102103
fail => 'object booling to true' => do { package Local::OL::BoolTrue; use overload q[bool] => sub { 1 }; bless [] },
104+
fail => 'boolean::false' => boolean::false,
105+
fail => 'boolean::true' => boolean::true,
106+
fail => 'builtin::false' => do { no warnings; builtin->can('false') ? builtin::false() : !!0 },
107+
fail => 'builtin::true' => do { no warnings; builtin->can('true') ? builtin::true() : !!1 },
103108
#TESTS
104109
);
105110

0 commit comments

Comments
 (0)