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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
use strict;
use warnings;
use FindBin qw( $Bin );
use lib "$Bin/lib";
use Test::Fatal;
use Test::More 0.96;
use Test::Specio qw( test_constraint :vars );
use Specio::Constraint::Union;
use Specio::Declare;
use Specio::DeclaredAt;
use Specio::Library::Builtins;
# The test output looks something like this:
#
# "Attempt to free unreferenced scalar: SV 0xf64bf0 at /home/autarch/perl5/perlbrew/perls/perl-5.12.5/lib/site_perl/5.12.5/Test/Builder.pm line 302."
#
# But the problem isn't in Test::Builder. It's something to do with
# overloading, because it happens when we try to test the non-inlined types
# with a NumOverload object.
plan skip_all =>
'This test triggers some odd overloading bug that causes a segfault on older perls'
if $] < 5.014;
# The glob vars only work when they're use in the same package as where
# they're declared. Globs are weird.
my $GLOB = do {
## no critic (TestingAndDebugging::ProhibitNoWarnings)
no warnings 'once';
*SOME_GLOB;
};
## no critic (Variables::RequireInitializationForLocalVars)
local *FOO;
my $GLOB_OVERLOAD = _T::GlobOverload->new( \*FOO );
local *BAR;
{
## no critic (InputOutput::ProhibitBarewordFileHandles, InputOutput::RequireBriefOpen)
open BAR, '<', $^X or die "Could not open $^X for the test";
}
my $GLOB_OVERLOAD_FH = _T::GlobOverload->new( \*BAR );
my %tests = (
accept => [
$ZERO,
$ONE,
$INT,
$NEG_INT,
$NUM_OVERLOAD_ZERO,
$NUM_OVERLOAD_ONE,
$NUM_OVERLOAD_NEG,
qw(
1e20
1e100
-1e10
-1e+10
1E20
),
$ARRAY_REF,
$ARRAY_OVERLOAD,
],
reject => [
$BOOL_OVERLOAD_TRUE,
$BOOL_OVERLOAD_FALSE,
$NUM,
$NEG_NUM,
$NUM_OVERLOAD_NEG_DECIMAL,
$NUM_OVERLOAD_DECIMAL,
$EMPTY_STRING,
$STRING,
$NUM_IN_STRING,
$STR_OVERLOAD_EMPTY,
$STR_OVERLOAD_FULL,
$INT_WITH_NL1,
$INT_WITH_NL2,
$SCALAR_REF,
$SCALAR_REF_REF,
$SCALAR_OVERLOAD,
$HASH_REF,
$HASH_OVERLOAD,
$CODE_REF,
$CODE_OVERLOAD,
$GLOB,
$GLOB_REF,
$GLOB_OVERLOAD,
$GLOB_OVERLOAD_FH,
$FH,
$FH_OBJECT,
$REGEX,
$REGEX_OBJ,
$REGEX_OVERLOAD,
$FAKE_REGEX,
$OBJECT,
$UNDEF,
qw(
1e-10
-1e-10
1.23456e10
1.23456e-10
-1.23456e10
-1.23456e-10
-1.23456e+10
),
],
);
subtest(
'unnamed union made of two builtins',
sub {
my $unnamed_union = Specio::Constraint::Union->new(
of => [ t('Int'), t('ArrayRef') ],
declared_at => Specio::DeclaredAt->new_from_caller(0),
);
ok(
$unnamed_union->_has_inline_generator,
'union of two types with inline generator has a generator'
);
is(
$unnamed_union->name,
'Int | ArrayRef',
'name is generated from constituent types'
);
ok(
!$unnamed_union->is_anon,
'unnamed union is not anonymous because name is generated'
);
is(
$unnamed_union->parent,
undef,
'parent method returns undef'
);
ok(
!$unnamed_union->_has_parent,
'union has no parent'
);
test_constraint( $unnamed_union, \%tests );
}
);
subtest(
'explicitly named union made of two builtins',
sub {
my $named_union = union(
'MyUnion',
of => [ t('Int'), t('ArrayRef') ],
);
is(
$named_union->name,
'MyUnion',
'name passed to union() is used'
);
test_constraint( $named_union, \%tests );
}
);
subtest(
'union made of two types without inline generators',
sub {
my $my_int = anon(
parent => t('Num'),
constraint => sub {
return (
(
defined( $_[0] )
&& !ref( $_[0] )
&& (
do {
## no critic (Variables::ProhibitUnusedVarsStricter)
( my $val1 = $_[0] )
=~ /\A-?[0-9]+(?:[Ee]\+?[0-9]+)?\z/;
}
)
)
|| (
Scalar::Util::blessed( $_[0] )
&& overload::Overloaded( $_[0] )
&& defined overload::Method( $_[0], '0+' )
&& do {
## no critic (Variables::ProhibitUnusedVarsStricter)
( my $val2 = $_[0] + 0 )
=~ /\A-?[0-9]+(?:[Ee]\+?[0-9]+)?\z/;
}
)
);
},
);
my $my_arrayref = anon(
parent => t('Ref'),
constraint => sub {
return (
ref( $_[0] ) eq 'ARRAY'
|| ( Scalar::Util::blessed( $_[0] )
&& overload::Overloaded( $_[0] )
&& defined overload::Method( $_[0], '@{}' ) )
);
},
);
my $no_inline_union = union(
of => [ $my_int, $my_arrayref ],
);
is(
$no_inline_union->name,
undef,
'no name if union includes anonymous types',
);
ok(
$no_inline_union->is_anon,
'union is anonymous if any of its constituents are anonymous'
);
test_constraint( $no_inline_union, \%tests );
}
);
subtest(
'union made of builtin and type without inline generator',
sub {
my $my_int = anon(
parent => t('Num'),
constraint => sub {
return (
(
defined( $_[0] )
&& !ref( $_[0] )
&& (
do {
## no critic (Variables::ProhibitUnusedVarsStricter)
( my $val1 = $_[0] )
=~ /\A-?[0-9]+(?:[Ee]\+?[0-9]+)?\z/;
}
)
)
|| (
Scalar::Util::blessed( $_[0] )
&& overload::Overloaded( $_[0] )
&& defined overload::Method( $_[0], '0+' )
&& do {
## no critic (Variables::ProhibitUnusedVarsStricter)
( my $val2 = $_[0] + 0 )
=~ /\A-?[0-9]+(?:[Ee]\+?[0-9]+)?\z/;
}
)
);
},
);
my $mixed_inline_union = union(
of => [ $my_int, t('ArrayRef') ],
);
is(
$mixed_inline_union->name,
undef,
'no name if union includes anonymous types',
);
ok(
$mixed_inline_union->is_anon,
'union is anonymous if any of its constituents are anonymous'
);
test_constraint( $mixed_inline_union, \%tests );
}
);
done_testing();
|