The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Proto - OO test script golf sugar

VERSION

Version 0.027

SYNOPSIS

This module provides an expressive interface for validating deep structures and objects.

use Test::Proto ':all';

pArray	->contains_only(pSeries('', pHash), 
		"ArrayRef must contain only an empty string followed by a hashref")
	->ok(["", {a=>'b'}]);
	# provides diagnostics, including subtests as TAP, using Test::Builder

p	->like(qr/^\d+$/, 'looks like a positive integer')
	->unlike(qr/^0\d+$/, 'no leading zeros')
	->validate('123');
	# returns an object with a true value

pObject	->is_a('XML::LibXML::Node', 'must inherit from XML::LibXML::Node')
	->is_a('XML::LibXML::Element', 'what it really is')
	->method_exists('findnodes', 'must have the findnodes method')
	->method_scalar_context('localName', [], 
		p->like(qr/blockquote|li|p/, 'We can add normal text here')
	)
	->ok(XML::LibXML::Element->new('li'));
	# have a look at the nested prototype in try_can

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.

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.

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.

NB: The meaning of "prototype" used here is not related to subroutine prototypes (declaring the arguments expected by a given function or method).

FUNCTIONS

p

p
p('foo')
p(['bar'])
p({foo=>'bar'})

Returns a basic prototype. See Test::Proto::Base. If an argument is passed, upgrades the argument and uses the resulting prototype.

pArray

Returns a prototype for an array/ArrayRef. See Test::Proto::ArrayRef.

pHash

Returns a prototype for a hash/HashRef. See Test::Proto::HashRef.

pCode

Returns a prototype for a CodeRef. See Test::Proto::CodeRef.

pObject

pObject
pObject('IO::Handle') # tests with is_a

Returns a prototype for an object. See Test::Proto::Object.

pSeries

Returns a series object for use in validating lists. See Test::Proto::Series.

pRepeatable

Returns a repeatable series object for use in validating lists. See Test::Proto::Repeatable.

pAlternation

Returns an alternation for use in validating lists. See Test::Proto::Alternation.

c

Returns a comparison object (string, by default). See Test::Proto::Compare.

cNumeric

Returns a numeric comparison object. See Test::Proto::Compare::Numeric.

AUTHOR

Begun by Daniel Perrett, <perrettdl at googlemail.com>

CONTRIBUTORS

Michael Schwern

SEE ALSO

Data::DPath

Data::Sah

Data::Verifier

Test::Deep

Validation::Class

BUGS

Please report any bugs or feature requests to the github issues tracker at 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.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Test::Proto

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2012-2013 Daniel Perrett.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.