NAME

Test::Proto::Common - Provides common functions for Test::Proto development

SYNOPSIS

use Test::Proto::Common; # exports all functions automatically

Provides functions used to build a Prototype class.

FUNCTIONS

All these functions are for use in prototype classes, not in scripts.

define_test

define_test 'is_uppercase', sub {
	my ($self, $data, $reason) = @_; # self is the runner, NOT the prototype
	if ($self->subject =~ !/[a-z]/){ 
		return $self->pass;
	}
	return $self->fail;
};

Adds a test definition to the class. This allows you to create user-facing test methods which interact with the test definition. The name you provide is the name of the test definition, which usually matches the test method (but is not required to).

Optionally, you can set the package to which this method is to be added as a third argument.

define_simple_test

Adds a test definition to the class. In this case, the subroutine passed evaluates the subject against the expected data.

simple_test

simple_test 'lc_eq', sub {
	return lc ($_[0]) eq $_[1];
};

...

p->lc_eq('yes')->ok('Yes');

Adds a test method to the class. The first argument is the name of that method, the second argument is the code to be executed - however, the code should return only a true or false value, and is passed only the test subject and the expected value, not the runner or full data.

The test method itself takes one argument, the expected value.

upgrade

upgrade('NONE'); # returns Test::Proto::Base->new()->eq('NONE')
upgrade(1); # returns Test::Proto::Base->new()->num_eq(1)
upgrade(['foo']); # returns Test::Proto::ArrayRef->new()->array_eq(['foo'])
upgrade({'foo'=>'bar'}); # returns Test::Proto::HashRef->new()->hash_of({'foo'=>'bar'})
upgrade(sub {return $_ * 2 == 4}); Test::Proto::Base->new()->try(...)

Returns a Prototype which corresponds to the data in the first argument.

If the first argument is already a prototype, this does nothing.

Use this when you have a parameter and want to validate data against it, but you do not know if it is a prototype or 'natural data'.

upgrade_comparison

upgrade_comparison(sub {lc shift cmp lc shift}, 'Lowercase Comparison');

This creates a Test::Proto::Compare object using the code provided in the first argument. The second argument, if present, is used as the summary.

If the first argument is either of the strings 'cmp' or '<=>', it will return the appropriate string or numeric comparison, as these are special-cased.

chainable

around 'attr', 'other_attr', \&Test::Proto::Common::chainable;

...

$object->attr(2)->some_method;

Use this to make a Moo attribute chainable.