File Coverage

blib/lib/Test2/Tools/Type/Extras.pm
Criterion Covered Total %
statement 44 44 100.0
branch 11 16 68.7
condition n/a
subroutine 31 31 100.0
pod 0 13 0.0
total 86 104 82.6


line stmt bran cond sub pod time code
1             package Test2::Tools::Type::Extras;
2              
3 1     1   5 use strict;
  1         2  
  1         30  
4 1     1   3 use warnings;
  1         2  
  1         57  
5              
6 1     1   5 use base qw(Exporter);
  1         1  
  1         108  
7              
8 1     1   4 use Carp qw(croak);
  1         1  
  1         49  
9              
10 1     1   4 use Scalar::Util qw(blessed reftype);
  1         2  
  1         85  
11              
12             our $VERSION = '1.0.1';
13              
14             our @EXPORT = ('regex_supported');
15              
16             my @targets = map { $_.'::' } @_;
17              
18             {
19 1     1   9 no strict 'refs';
  1         1  
  1         684  
20             while(my($k, $v) = each(%{__PACKAGE__.'::'})) {
21             push @EXPORT, $k if(
22             $k =~ /^is_/ &&
23             ref($v) ne 'SCALAR' &&
24             defined(&{$v})
25             );
26             }
27             }
28              
29             *_checker = \&Test2::Tools::Type::_checker;
30              
31 11     11 0 43 sub is_positive { _checker(sub { $_[0] > 0 }, @_); }
  11     11   722  
32 9     9 0 62 sub is_negative { _checker(sub { $_[0] < 0 }, @_); }
  9     9   730  
33 3     3 0 10 sub is_zero { _checker(sub { $_[0] == 0; }, @_); }
  3     3   527  
34              
35             # There are tests that is_ref, is_object and is_hashref don't screw with
36             # the argument's type. That covers the implementations that use
37             # ref/blessed/reftype. If you add more checks with different innards,
38             # add tests for that as well as that they return what you expect.
39 5     5 0 9 sub is_ref { _checker(sub { ref($_[0]); }, @_); }
  5     5   483  
40 6     6 0 12 sub is_object { _checker(sub { blessed($_[0]); }, @_); }
  6     6   719  
41 7 100   7 0 49 sub is_hashref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'HASH' }, @_); }
  7     7   632  
42 2 50   2 0 8 sub is_arrayref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'ARRAY' }, @_); }
  2     2   187  
43 2 50   2 0 10 sub is_scalarref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'SCALAR' }, @_); }
  2     2   241  
44 2 50   2 0 8 sub is_coderef { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'CODE' }, @_); }
  2     2   198  
45 2 100   2 0 9 sub is_globref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'GLOB' }, @_); }
  2     2   188  
46 2 50   2 0 10 sub is_refref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'REF' }, @_); }
  2     2   219  
47              
48             sub is_regex {
49 3 50   3 0 126 croak("You need perl 5.12 or higher to use is_regex")
50             unless(regex_supported());
51 3 100   3   16 _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'REGEXP' }, @_);
  3         13  
52             }
53              
54 7     7 0 20274 sub regex_supported { $] >= 5.012 }
55              
56             1;
57              
58             =head1 NAME
59              
60             Test2::Tools::Type::Extras - Extra tools for checking data types
61              
62             =head1 SYNOPSIS
63              
64             use Test2::V0;
65             use Test2::Tools::Type qw(:extras);
66              
67             =head1 DESCRIPTION
68              
69             This provides extra testing functions for Test2::Tools::Type. They
70             can all be used stand-alone or with Test2::Tools::Type's C method.
71              
72             This module is not intended for you to use it directly.
73              
74             =head1 BUGS
75              
76             If you find any bugs please report them on Github, preferably with a test case.
77              
78             =head1 FEEDBACK
79              
80             I welcome feedback about my code, especially constructive criticism.
81              
82             =head1 AUTHOR, COPYRIGHT and LICENCE
83              
84             Copyright 2024 David Cantrell EFE
85              
86             This software is free-as-in-speech software, and may be used,
87             distributed, and modified under the terms of either the GNU
88             General Public Licence version 2 or the Artistic Licence. It's
89             up to you which one you use. The full text of the licences can
90             be found in the files GPL2.txt and ARTISTIC.txt, respectively.
91              
92             =head1 CONSPIRACY
93              
94             This module is also free-as-in-mason software.
95              
96             =cut