use Test::Specio qw( test_constraint :vars ); test_constraint( t('Foo'), { accept => [ 'foo', 'bar' ], reject => [ 42, {}, $EMPTY_STRING, $HASH_REF ], } );
The third argument is optional. This is a sub reference which will be called to generate a description of the value being tested. This defaults to calling this package's "describe" sub, but you can provide your own.
{ Bool => { accept => [ $ZERO, $ONE, $BOOL_OVERLOAD_TRUE, $BOOL_OVERLOAD_FALSE, ..., ], reject => [ $INT, $NEG_INT, $NUM, $NEG_NUM, ..., $OBJECT, ], }, Maybe => {...}, }
You need to pass in a glob, an object which overloads globification, and an object which overloads globification to return an open filehandle. See below for more details on how to create these things.
An arbitrary positive integer.
An arbitrary negative integer.
An arbitrary positive non-integer number.
An arbitrary negative non-integer number.
An arbitrary non-empty string.
An arbitrary string which contains a number.
An string containing an integer followed by a newline.
An string containing a newline followed by an integer.
A reference containing a reference to a scalar.
An opened filehandle.
An opened IO::File object.
A regex created with "qr//".
A regex created with "qr//" that was then blessed into class.
A non-regex blessed into the "Regexp" class which Perl uses internally for "qr//" objects.
An arbitrary object.
A string containing a loaded package name.
An object which overloads boolification to return true.
An object which overloads boolification to return false.
An object which overloads stringification to return an empty string.
An object which overloads stringification to return a non-empty string.
An object which overloads stringification to return a loaded package name.
An object which overloads scalar dereferencing to return a non-empty string.
An object which overloads array dereferencing to return a non-empty array.
An object which overloads hash dereferencing to return a non-empty hash.
my $GLOB = do { no warnings 'once'; *SOME_GLOB; };
The "_T::GlobOverload" package is defined when you load "Test::Specio" so you can create your own glob overloading objects. Such objects cannot be exported because the glob they return does not transfer across packages properly.
You can create such a variable like this:
local *FOO; my $GLOB_OVERLOAD = _T::GlobOverload->new( \*FOO );
If you want to create a glob overloading object that returns a filehandle, do this:
local *BAR; open BAR, '<', $0 or die "Could not open $0 for the test"; my $GLOB_OVERLOAD_FH = _T::GlobOverload->new( \*BAR );
I am also usually active on IRC as 'autarch' on "irc://irc.perl.org".
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license can be found in the LICENSE file included with this distribution.