Test::Proto::ArrayRef - Test Prototype for Array References.
Test::Proto::ArrayRef->new->ok([1,2,3]); # ok Test::Proto::ArrayRef->new->ok([]); # also ok Test::Proto::ArrayRef->new->is_deeply([1,2,3])->ok([1,2,3]); # ok Test::Proto::ArrayRef->new->is_empty->ok([]); # also ok Test::Proto::ArrayRef->new->ok(undef); # not ok Test::Proto::ArrayRef->new->ok(1,2,3); # not ok
This is a test prototype which requires that the value it is given is defined and is an arrayref. It provides methods for interacting with arrayrefs. (To test lists/arrays, make them arrayrefs and test them with this module)
See Test::Proto::Base for documentation on common methods. All methods which add a test return the prototype and the last argument is optional and contains the reason for running the test (for the output). The thing which is being tested is unaffected except where noted.
$prototype->array_length(3, 'is 3 elements long')->ok([1,2,3]);
This method adds a test equivalent to Perl's builtin scalar. The first argument is upgraded and validated with the result of the scalar operation.
scalar
$prototype->is_empty->ok([]);
This method adds a test which passes if the arrayref is empty.
$prototype->map(sub{ord($_[0])}, [97,98,99])->ok(['a','b','c']);
This method adds a test in which each arrayref element is passed into the coderef provided and the output added to a new arrayref which is tested against the prototype. It functions like Perl's map.
map
$prototype->grep(sub{ord($_[0])>97}, [98,99])->ok(['a','b','c']);
This method adds a test in which each arrayref element is passed into the test provided (if it is a coderef, it will be upgraded) and, if successful, the arrayref element will be added to a new arrayref which is tested against the prototype. It functions like Perl's grep.
grep
$prototype->all(sub{ord($_[0])>96})->ok(['a','b','c']);
This method adds a test in which each arrayref element is passed into the test provided (if it is a coderef, it will be upgraded). The test will pass if all elements pass the test.
$prototype->first_match(sub{ord($_[0])>97},'b')->ok(['a','b','c']);
This method adds a test in which each arrayref element is passed into the test provided (if it is a coderef, it will be upgraded). The first element which passes this test will then be passed into the second test.
$prototype->last_match(sub{ord($_[0])>97},'c')->ok(['a','b','c']);
This method adds a test in which each arrayref element is passed into the test provided (if it is a coderef, it will be upgraded). The last element which passes this test will then be passed into the second test.
$prototype->range('1..3', [9,8,7])->ok([10..1]);
This method selects a subrange and passes that to a new test.
$prototype->reduce(sub {$_[0] .= $_[1] }, 'abc')->ok(['a','b','c']);
Reduce works by passing elements to a function sequentially and always in pairs; the result of the last function plus the next item in the array, like List::Util's reduce.
reduce
$prototype->contains_only([pSeries(pSeries(1,2)->repeat(1,5),3)])->ok([1,2,1,2,3]);
This method allows DTD-like validation of an arrayref. See also Test::Proto::Series.
$prototype->begins_with([pSeries(1,2)])->ok([1,2,1,2,3]);
As contains_only, but only checks the beginning of an array.
contains_only
$prototype->sort(sub{lc $_[0] cmp lc $_[1]}, ['a','B','c'])->ok(['B','a','c']);
This method sorts the arrayref using the first argument as the comparison code and passes the result to a new test.
$prototype->sort(sub{$_[0] cmp $_[1]}, sub {lc $_[0]}, ['a','B','c'])->ok(['B','a','c']);
This method sorts the arrayref using a schwarzian transform using the first argument as the comparison code and the second as the normaliser, and passes the result to a new test.
$prototype->uniq(sub{lc $_[0] cmp lc $_[1]}, ['B','c'])->ok(['B','b','c']);
This method reduces the arrayref to only those elements which are not repeated, and passes the result to a new test.
The first argument is a comparison operator which should return 0 if both arguments are identical for this purpose. For any matching pairs found, the first element is always kept, and the order is unchanged.
$prototype->min(sub{lc $_[0] cmp lc $_[1]}, 'a')->ok(['B','a','c']);
This method picks the element with the lowest value (according to the comparison operator provided).
For any matching pairs found, the first element is used.
$prototype->max(sub{lc $_[0] cmp lc $_[1]}, 'a')->ok(['B','a','c']);
This method picks the element with the highest value (according to the comparison operator provided).
$prototype->enumerate( [[1,'a'],[2,'B'],[3,'c']])->ok(['a','B','c']);
This method turns each element of the arrayref into an arrayref containing the index and the value and passes the result to a new test.
For author, version, bug reports, support, etc, please see Test::Proto.
To install Test::Proto, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Proto
CPAN shell
perl -MCPAN -e shell install Test::Proto
For more information on module installation, please visit the detailed CPAN module installation guide.