| 1 | package Test::Proto::CodeRef; | ||
| 2 | 5 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 5 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | extends 'Test::Proto::Base'; | |
| 7 | 4 | with( 'Test::Proto::Role::Value', ); | |
| 8 | 5 | ▶ | use Test::Proto::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Test::Proto::CodeRef - Test a coderef's behaviour | ||
| 13 | |||
| 14 | =head1 METHODS | ||
| 15 | |||
| 16 | =head3 call | ||
| 17 | |||
| 18 | $p->call(['test.txt','>'], [$fh])->ok($subject); | ||
| 19 | |||
| 20 | Takes two arguments: first, the arguments to pass to the code, second the expected return value. Passes the arguments to the test subject, and tests the return value against the expected value. | ||
| 21 | |||
| 22 | The arguments and return value should be arrayrefs; the code is evaluated in list context. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | 3 | ▶ | sub call { |
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||
| 27 | 3 | my ($self) = shift; | |
| 28 | 3 | $self->call_list_context(@_); | |
| 29 | } | ||
| 30 | |||
| 31 | =head3 call_void_context | ||
| 32 | |||
| 33 | $p->call_void_context(['test.txt','>'])->ok($subject); | ||
| 34 | |||
| 35 | Takes one argument: the arguments to use with the method, as an arrayref. Calls the method on the test subject, with the arguments. This test will always pass, unless the code dies, or is not code. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | 1 | ▶ | sub call_void_context { |
|
Subroutine
call_void_context: called CC=1 SCAR=0.0
Pod
documented
| |||
| 40 | 1 | my ( $self, $args, $reason ) = @_; | |
| 41 | 1 | $self->add_test( 'call_void_context', { args => $args, }, $reason ); | |
| 42 | } | ||
| 43 | |||
| 44 | 1 | ▶ | define_test "call_void_context" => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Subroutine
_TEST_call_void_context: not called CC=1 SCAR=1.2
| |||
| 45 | 0 | my ( $self, $data, $reason ) = @_; # self is the runner | |
| 46 | 0 | my $args = $data->{args}; | |
| 47 | 0 | $self->subject->(@$args); | |
| 48 | 0 | return $self->pass; #~ void context so we pass unless it dies. | |
| 49 | 4 | }; | |
| 50 | |||
| 51 | =head3 call_scalar_context | ||
| 52 | |||
| 53 | $p->call_scalar_context(['test.txt','>'], $true)->ok($subject); | ||
| 54 | |||
| 55 | Takes two arguments: first, the arguments to pass to the code, second the expected return value. Passes the arguments to the test subject, and tests the return value against the expected value. | ||
| 56 | |||
| 57 | The arguments should be an arrayref, and the expected value should be a prototype evaluating the returned scalar, as the method is evaluated in scalar context. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | 1 | ▶ | sub call_scalar_context { |
|
Subroutine
call_scalar_context: called CC=1 SCAR=0.0
Pod
documented
| |||
| 62 | 1 | my ( $self, $args, $expected, $reason ) = @_; | |
| 63 | 1 | $self->add_test( | |
| 64 | 'call_scalar_context', | ||
| 65 | { | ||
| 66 | args => $args, | ||
| 67 | expected => $expected | ||
| 68 | }, | ||
| 69 | $reason | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | |||
| 73 | 1 | ▶ | define_test "call_scalar_context" => sub { |
|
Subroutine
_TEST_call_scalar_context: called CC=1 SCAR=0.0
| |||
| 74 | 0 | my ( $self, $data, $reason ) = @_; # self is the runner | |
| 75 | 1 | my $args = $data->{args}; | |
| 76 | 1 | my $expected = upgrade( $data->{expected} ); | |
| 77 | 1 | my $response = $self->subject->(@$args); | |
| 78 | 1 | return $expected->validate( $response, $self ); | |
| 79 | 4 | }; | |
| 80 | |||
| 81 | =head3 call_list_context | ||
| 82 | |||
| 83 | $p->call_list_context(['test.txt','>'], [$true])->ok($subject); | ||
| 84 | |||
| 85 | Takes two arguments: first, the arguments to pass to the code, second the expected return value. Passes the arguments to the test subject, and tests the return value against the expected value. | ||
| 86 | |||
| 87 | The arguments and return value should be arrayrefs; the code is evaluated in list context. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | 5 | ▶ | sub call_list_context { |
|
Subroutine
call_list_context: called CC=1 SCAR=0.0
Pod
documented
| |||
| 92 | 5 | my ( $self, $args, $expected, $reason ) = @_; | |
| 93 | 5 | $self->add_test( | |
| 94 | 'call_list_context', | ||
| 95 | { | ||
| 96 | args => $args, | ||
| 97 | expected => $expected | ||
| 98 | }, | ||
| 99 | $reason | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | 5 | ▶ | define_test call_list_context => sub { |
|
Subroutine
_TEST_call_list_context: called CC=1 SCAR=0.0
| |||
| 104 | 5 | my ( $self, $data, $reason ) = @_; # self is the runner | |
| 105 | 5 | my $args = $data->{args}; | |
| 106 | 5 | my $expected = upgrade( $data->{expected} ); | |
| 107 | 5 | my $response = [ $self->subject->(@$args) ]; | |
| 108 | 5 | return $expected->validate( $response, $self ); | |
| 109 | 4 | }; | |
| 110 | |||
| 111 | =head1 OTHER INFORMATION | ||
| 112 | |||
| 113 | For author, version, bug reports, support, etc, please see L<Test::Proto>. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | 4 | 1; | |