| 1 | package Test::Proto::Where; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use Test::Proto::Common(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | our @EXPORT = qw(&test_subject &where &otherwise); | |||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Test::Proto::Where - provide case switching using Test::Proto | ||||||||||
| 11 | |||||||||||
| 12 | =head1 VERSION | ||||||||||
| 13 | |||||||||||
| 14 | 0.001 | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | 3 | our $VERSION = '0.001'; | |||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | print test_subject {foo=>'bar'} => | ||||||||||
| 23 | where [], sub{ 'Empty array' }, | ||||||||||
| 24 | where pHash, sub{ 'A hash' }, | ||||||||||
| 25 | otherwise sub { 'Something else' }; | ||||||||||
| 26 | |||||||||||
| 27 | Uses Test::Proto and its upgrading feature to implement a dispatch. | ||||||||||
| 28 | |||||||||||
| 29 | Note: This module is presently B<EXPERIMENTAL>: it is a working proof of concept. | ||||||||||
| 30 | |||||||||||
| 31 | =head1 SYNTAX | ||||||||||
| 32 | |||||||||||
| 33 | =head3 test_subject | ||||||||||
| 34 | |||||||||||
| 35 | Takes as its first argument a prototype, which must not be a list of bare array/hash. It then takes one or more where/otherwise statements, as described below. If it does not get the arguments it requires, it will C<die>. | ||||||||||
| 36 | |||||||||||
| 37 | If you are taking the first argument from a function or method call, you should use scalar to force scalar context, like this: | ||||||||||
| 38 | |||||||||||
| 39 | test_subject scalar($obj->method) => | ||||||||||
| 40 | where ... | ||||||||||
| 41 | |||||||||||
| 42 | Note also that because test_subject takes where and otherwise as arugments, if you are enclosing the first argument in brackets you must enclose all the arguments in brackets, other wise perl will be confused and think you are only passing the first argument. | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | 6 | ▶ | sub test_subject ($$) { | ||||||||
|
Subroutine
test_subject: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 47 | 6 | my $subject = shift; | |||||||||
| 48 | 6 | my $where = shift; | |||||||||
| 49 | 6 | ▶ | die('Missing where') unless defined $where; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 6 | ▶ | die('Expected where or otherwise') if ref $where ne 'Test::Proto::Where'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 6 | return $where->{run}->($subject); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | =head3 where | ||||||||||
| 55 | |||||||||||
| 56 | C<where> is followed by a test, then an instruction. If the test passes, the instruction is carried out and no other 'where' or 'otherwise' statements are executed. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 8 | ▶ | sub where ($&;$) { | ||||||||
|
Subroutine
where: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 61 | 8 | my $self = { | |||||||||
| 62 | proto => shift, | ||||||||||
| 63 | code => shift, | ||||||||||
| 64 | type => 'where', | ||||||||||
| 65 | fallback => shift | ||||||||||
| 66 | }; | ||||||||||
| 67 | 8 | ▶ | $self->{run} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||
| 68 | 8 | my $subject = shift; | |||||||||
| 69 | 8 | ▶ | return $self->{code}->($subject) if Test::Proto::Common::upgrade( $self->{proto} )->validate($subject); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 3 | ▶ | return unless defined $self->{fallback}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 3 | ▶ | die('Expected where or otherwise') if ref $self->{fallback} ne 'Test::Proto::Where'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 3 | return $self->{fallback}->{run}->($subject); | |||||||||
| 73 | 8 | }; | |||||||||
| 74 | 8 | ▶ | die('where needs code') unless defined $self->{code}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 8 | bless $self, __PACKAGE__; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | =head3 otherwise | ||||||||||
| 79 | |||||||||||
| 80 | C<otherwise> is followed an instruction. If no preceding where tests have passed, this instruction will be executed. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 4 | ▶ | sub otherwise (&) { | ||||||||
|
Subroutine
otherwise: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 85 | 4 | my $self = { | |||||||||
| 86 | code => shift, | ||||||||||
| 87 | type => 'otherwise' | ||||||||||
| 88 | }; | ||||||||||
| 89 | 4 | $self->{run} = $self->{code}; | |||||||||
| 90 | 4 | bless $self, __PACKAGE__; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # test_subject scalar foo(), where pArray, {}, otherwise {}; | ||||||||||
| 94 | |||||||||||