| 1 | package Test::Proto; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use Test::Proto::Base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use Test::Proto::ArrayRef; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 4 | ▶ | use Test::Proto::HashRef; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 4 | ▶ | use Test::Proto::CodeRef; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 4 | ▶ | use Test::Proto::Object; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 4 | ▶ | use Test::Proto::Series; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 4 | ▶ | use Test::Proto::Repeatable; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 4 | ▶ | use Test::Proto::Alternation; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 4 | ▶ | use Test::Proto::Compare; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 4 | ▶ | use Test::Proto::Compare::Numeric; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 4 | ▶ | use Test::Proto::Common (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 4 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 4 | ▶ | use base "Exporter"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 4 | our @EXPORT_OK = qw(&p &pArray &pHash &pCode &pObject &pSeries &pRepeatable &pAlternation &c &cNumeric); # symbols to export on request | |||||||||||||||||||
| 20 | 4 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | Test::Proto - OO test script golf sugar | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 VERSION | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | Version 0.027 | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 4 | our $VERSION = '0.027'; #~ must keep in sync with $Test::Proto::Base::VERSION; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | This module provides an expressive interface for validating deep structures and objects. | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | use Test::Proto ':all'; | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | pArray ->contains_only(pSeries('', pHash), | ||||||||||||||||||||
| 41 | "ArrayRef must contain only an empty string followed by a hashref") | ||||||||||||||||||||
| 42 | ->ok(["", {a=>'b'}]); | ||||||||||||||||||||
| 43 | # provides diagnostics, including subtests as TAP, using Test::Builder | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | p ->like(qr/^\d+$/, 'looks like a positive integer') | ||||||||||||||||||||
| 46 | ->unlike(qr/^0\d+$/, 'no leading zeros') | ||||||||||||||||||||
| 47 | ->validate('123'); | ||||||||||||||||||||
| 48 | # returns an object with a true value | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | pObject ->is_a('XML::LibXML::Node', 'must inherit from XML::LibXML::Node') | ||||||||||||||||||||
| 51 | ->is_a('XML::LibXML::Element', 'what it really is') | ||||||||||||||||||||
| 52 | ->method_exists('findnodes', 'must have the findnodes method') | ||||||||||||||||||||
| 53 | ->method_scalar_context('localName', [], | ||||||||||||||||||||
| 54 | p->like(qr/blockquote|li|p/, 'We can add normal text here') | ||||||||||||||||||||
| 55 | ) | ||||||||||||||||||||
| 56 | ->ok(XML::LibXML::Element->new('li')); | ||||||||||||||||||||
| 57 | # have a look at the nested prototype in try_can | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | The idea behind Test::Proto is that test scripts for code written on modern, OO principles should themselves resemble the target code rather than sequential code. | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Tests for deep structures and objects tend should not be repetitive and should be flexible. Test::Proto allows you to create objects "protoypes" intended to test structures which conform to a known type. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | As in the example above, the way it works is: you create a prototype object, add test cases to the prototype using method calls, and then validate your string/arryref/object/etc. against the prototype using the validate or ok method. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | NB: The meaning of "prototype" used here is not related to subroutine prototypes (declaring the arguments expected by a given function or method). | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head1 FUNCTIONS | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 p | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | p | ||||||||||||||||||||
| 72 | p('foo') | ||||||||||||||||||||
| 73 | p(['bar']) | ||||||||||||||||||||
| 74 | p({foo=>'bar'}) | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | Returns a basic prototype. See L<Test::Proto::Base>. If an argument is passed, upgrades the argument and uses the resulting prototype. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 6 | ▶ | sub p { | ||||||||||||||||||
|
Subroutine
p: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 81 | 6 | ▶ | return Test::Proto::Common::upgrade( $_[0] ) if 1 == scalar @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 1 | return Test::Proto::Base->new(@_); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head2 pArray | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Returns a prototype for an array/ArrayRef. See L<Test::Proto::ArrayRef>. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | ▶ | sub pArray { | ||||||||||||||||||
|
Subroutine
pArray: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 92 | 2 | ▶ | return Test::Proto::Common::upgrade( $_[0] ) if 1 == scalar @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 1 | return Test::Proto::ArrayRef->new(@_)->array; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head2 pHash | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Returns a prototype for a hash/HashRef. See L<Test::Proto::HashRef>. | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | ▶ | sub pHash { | ||||||||||||||||||
|
Subroutine
pHash: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 103 | 2 | ▶ | return Test::Proto::Common::upgrade( $_[0] ) if 1 == scalar @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 1 | return Test::Proto::HashRef->new(@_)->hash; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head2 pCode | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Returns a prototype for a CodeRef. See L<Test::Proto::CodeRef>. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 1 | ▶ | sub pCode { | ||||||||||||||||||
|
Subroutine
pCode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 114 | 1 | return Test::Proto::CodeRef->new(@_); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head2 pObject | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | pObject | ||||||||||||||||||||
| 120 | pObject('IO::Handle') # tests with is_a | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Returns a prototype for an object. See L<Test::Proto::Object>. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 10 | ▶ | sub pObject { | ||||||||||||||||||
|
Subroutine
pObject: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||
| 127 | 10 | ▶ | if ( 1 == scalar @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 9 | my $p = Test::Proto::Object->new()->blessed; | |||||||||||||||||||
| 129 | 9 | ▶ | if ( !ref $_[0] ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 130 | 2 | $p->is_a( $_[0] ); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | ▶ | elsif ( ( blessed $_[0] ) and $_[0]->isa('Test::Proto::Base') ) { | |||||||||||||||||||
|
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("Test::Proto::Base")
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("Test::Proto::Base")
builtin::blessed($_[0])
$_[0]->isa("Test::Proto::Base")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Test::Proto::Base")
| |||||||||||||||||||||
| 133 | 0 | $p->is_also( $_[0] ); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | elsif ( ref $_[0] =~ /^(?:HASH|ARRAY)$/ ) { | ||||||||||||||||||||
| 136 | 0 | $p->is_also( Test::Proto::Common::upgrade( $_[0] ) ); | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||
| 139 | 7 | ▶ | $p->refaddr( refaddr $_[0] ) if blessed $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 9 | return $p; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||
| 144 | 1 | return Test::Proto::Object->new(@_)->blessed; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head2 pSeries | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Returns a series object for use in validating lists. See L<Test::Proto::Series>. | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 1 | ▶ | sub pSeries { | ||||||||||||||||||
|
Subroutine
pSeries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 155 | 1 | return Test::Proto::Series->new(@_); | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 pRepeatable | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Returns a repeatable series object for use in validating lists. See L<Test::Proto::Repeatable>. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 1 | ▶ | sub pRepeatable { | ||||||||||||||||||
|
Subroutine
pRepeatable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 165 | 1 | return Test::Proto::Repeatable->new(@_); | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head2 pAlternation | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Returns an alternation for use in validating lists. See L<Test::Proto::Alternation>. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 1 | ▶ | sub pAlternation { | ||||||||||||||||||
|
Subroutine
pAlternation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 175 | 1 | return Test::Proto::Alternation->new(@_); | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 c | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Returns a comparison object (string, by default). See L<Test::Proto::Compare>. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 1 | ▶ | sub c { | ||||||||||||||||||
|
Subroutine
c: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 185 | 1 | return Test::Proto::Compare->new(@_); | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 cNumeric | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Returns a numeric comparison object. See L<Test::Proto::Compare::Numeric>. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 1 | ▶ | sub cNumeric { | ||||||||||||||||||
|
Subroutine
cNumeric: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 195 | 1 | return Test::Proto::Compare::Numeric->new(@_); | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | Begun by Daniel Perrett, C<< <perrettdl at googlemail.com> >> | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =head1 CONTRIBUTORS | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | Michael Schwern | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =head1 SEE ALSO | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | L<Data::DPath> | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | L<Data::Sah> | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | L<Data::Verifier> | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | L<Test::Deep> | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | L<Validation::Class> | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =head1 BUGS | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | Please report any bugs or feature requests to the github issues tracker at L<https://github.com/pdl/Test-Proto/issues>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =head1 SUPPORT | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | perldoc Test::Proto | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | You can also look for information at: | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =over 4 | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =item * Github (please report bugs here) | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | L<https://github.com/pdl/Test-Proto> | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =item * MetaCPAN | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | L<https://metacpan.org/module/Test::Proto> | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | L<http://annocpan.org/dist/Test-Proto> | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =item * Search CPAN | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | L<http://search.cpan.org/dist/Test-Proto/> | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =back | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | Copyright 2012-2013 Daniel Perrett. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 257 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 258 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 4 | return 1; # module loaded ok | |||||||||||||||||||