| 1 | package Test::Proto::Object; | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 5 | extends 'Test::Proto::Base'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | with 'Test::Proto::Role::Value'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | with 'Test::Proto::Role::ArrayRef'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | with 'Test::Proto::Role::HashRef'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Test::Proto::Common; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Test::Proto::Object - Test an object's behaviour | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =head3 method | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | $p->method('open')->ok($subject); # i.e. method_exists | ||||||||||||||||||||||||||||||||||||||||||
| 21 | $p->method('open', ['test.txt','>'], [$fh])->ok($subject); # method_list_context | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | B<With one argument>, takes the method, and checks if it exists (using C<method_exists>). | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | B<With three arguments>, takes takes a method, the arguments to use with the method, and the expected return value. Calls the method on the test subject, with the arguments, and tests the return value. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | The arguments and return value should be arrayrefs; the method is evaluated in list context (it uses C<method_list_context>). | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | my ($self) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | if ( ( !exists $_[1] ) or ( !defined ref $_[1] ) or ( ( ref $_[1] ) ne 'ARRAY' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not exists $_[1] && defined CORE::ref $_[1] or CORE::ref $_[1] ne "ARRAY"
exists $_[1] && defined CORE::ref $_[1]
MC/DC66%2/3
not exists $_[1] && defined CORE::ref $_[1] or CORE::ref $_[1] ne "ARRAY"
exists $_[1]
defined CORE::ref $_[1]
CORE::ref $_[1] ne "ARRAY"
Truth table75%3/4
not(A && B) or C
A= exists $_[1]B= defined CORE::ref $_[1]C= CORE::ref $_[1] ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | $self->method_exists(@_); | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | $self->method_list_context(@_); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | =head3 method_void_context | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | $p->method_void_context('open', ['test.txt','>'])->ok($subject); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | Takes two arguments, a method, and the arguments to use with the method. Calls the method on the test subject, with the arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | The arguments should be na arrayref; the method is evaluated in void context. This test will always pass, unless the method dies (or does not exist). | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | sub method_void_context { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method_void_context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | my ( $self, $method, $args, $reason ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | $self->add_test( | |||||||||||||||||||||||||||||||||||||||||
| 54 | 'method_void_context', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | args => $args, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | $reason | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | define_test "method_void_context" => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_TEST_method_void_context: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | my ( $self, $data, $reason ) = @_; # self is the runner | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my $args = $data->{args}; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | my $method = $data->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | $self->subject->$method(@$args); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | return $self->pass; # void context so we pass unless it dies. | |||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head3 method_scalar_context | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | $p->method_scalar_context('open', ['test.txt','>'], $true)->ok($subject); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | Takes three arguments, a method, the arguments to use with the method, and the expected return value. Calls the method on the test subject, with the arguments, and tests the return value. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 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. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | sub method_scalar_context { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method_scalar_context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | my ( $self, $method, $args, $expected, $reason ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | $self->add_test( | |||||||||||||||||||||||||||||||||||||||||
| 84 | 'method_scalar_context', | ||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | args => $args, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | expected => $expected | ||||||||||||||||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | $reason | ||||||||||||||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | define_test "method_scalar_context" => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_TEST_method_scalar_context: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my ( $self, $data, $reason ) = @_; # self is the runner | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my $args = $data->{args}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | my $method = $data->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | my $expected = upgrade( $data->{expected} ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $response = $self->subject->$method(@$args); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | return $expected->validate( $response, $self ); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head3 method_list_context | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | $p->method_list_context('open', ['test.txt','>'], [$true])->ok($subject); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | Takes three arguments, a method, the arguments to use with the method, and the expected return value. Calls the method on the test subject, with the arguments, and tests the return value. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | The arguments and return value should be arrayrefs; the method is evaluated in list context. | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | sub method_list_context { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method_list_context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | my ( $self, $method, $args, $expected, $reason ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | $self->add_test( | |||||||||||||||||||||||||||||||||||||||||
| 116 | 'method_list_context', | ||||||||||||||||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | args => $args, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | expected => $expected | ||||||||||||||||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | $reason | ||||||||||||||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | define_test method_list_context => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my ( $self, $data, $reason ) = @_; # self is the runner | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | my $args = $data->{args}; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $method = $data->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my $expected = upgrade( $data->{expected} ); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $response = [ $self->subject->$method(@$args) ]; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return $expected->validate( $response, $self ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =head3 method_exists | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | $p->method_exists('open')->ok($subject); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | Determines if the named method is present on the test subject. Implemented with C<can>, and does not pick up methods available via C<AUTOLOAD>. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 5 | ▶ | sub method_exists { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 5 | my ( $self, $method, $args, $expected, $reason ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 5 | $self->add_test( 'method_exists', { method => $method, }, $reason ); | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 5 | ▶ | define_test method_exists => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 5 | my ( $self, $data, $reason ) = @_; # self is the runner | |||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | my $method = $data->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 5 | ▶ | return $self->pass if ( $self->subject->can($method) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | return $self->fail; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =head1 OTHER INFORMATION | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | For author, version, bug reports, support, etc, please see L<Test::Proto>. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||