File Coverage

blib/lib/Test/Proto/Object.pm
Criterion Covered Total %
statement 48 48 100.0
branch 4 4 100.0
condition 5 6 83.3
subroutine 14 14 100.0
pod 5 5 100.0
total 76 77 98.7


line stmt bran cond sub pod time code
1             package Test::Proto::Object;
2 5     5   31112 use 5.008;
  5         16  
  5         181  
3 5     5   27 use strict;
  5         23  
  5         144  
4 5     5   27 use warnings;
  5         10  
  5         145  
5 5     5   802 use Moo;
  5         15320  
  5         40  
6             extends 'Test::Proto::Base';
7             with 'Test::Proto::Role::Value';
8             with 'Test::Proto::Role::ArrayRef';
9             with 'Test::Proto::Role::HashRef';
10 5     5   3442 use Test::Proto::Common;
  5         11  
  5         3271  
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, takes the method, and checks if it exists (using C).
24              
25             B, 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).
28              
29             =cut
30              
31             sub method {
32 4     4 1 33 my ($self) = shift;
33 4 100 66     40 if ( ( !exists $_[1] ) or ( !defined ref $_[1] ) or ( ( ref $_[1] ) ne 'ARRAY' ) ) {
      100        
34 3         14 $self->method_exists(@_);
35             }
36             else {
37 1         6 $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             sub method_void_context {
52 2     2 1 17 my ( $self, $method, $args, $reason ) = @_;
53 2         15 $self->add_test(
54             'method_void_context',
55             {
56             method => $method,
57             args => $args,
58             },
59             $reason
60             );
61             }
62              
63             define_test "method_void_context" => sub {
64 2     2   5 my ( $self, $data, $reason ) = @_; # self is the runner
65 2         5 my $args = $data->{args};
66 2         6 my $method = $data->{method};
67 2         56 $self->subject->$method(@$args);
68 1         11 return $self->pass; # void context so we pass unless it dies.
69             };
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             sub method_scalar_context {
82 1     1 1 8 my ( $self, $method, $args, $expected, $reason ) = @_;
83 1         9 $self->add_test(
84             'method_scalar_context',
85             {
86             method => $method,
87             args => $args,
88             expected => $expected
89             },
90             $reason
91             );
92             }
93              
94             define_test "method_scalar_context" => sub {
95 1     1   3 my ( $self, $data, $reason ) = @_; # self is the runner
96 1         4 my $args = $data->{args};
97 1         2 my $method = $data->{method};
98 1         5 my $expected = upgrade( $data->{expected} );
99 1         22 my $response = $self->subject->$method(@$args);
100 1         11 return $expected->validate( $response, $self );
101             };
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             sub method_list_context {
114 2     2 1 12 my ( $self, $method, $args, $expected, $reason ) = @_;
115 2         20 $self->add_test(
116             'method_list_context',
117             {
118             method => $method,
119             args => $args,
120             expected => $expected
121             },
122             $reason
123             );
124             }
125              
126             define_test method_list_context => sub {
127 2     2   4 my ( $self, $data, $reason ) = @_; # self is the runner
128 2         6 my $args = $data->{args};
129 2         5 my $method = $data->{method};
130 2         11 my $expected = upgrade( $data->{expected} );
131 2         53 my $response = [ $self->subject->$method(@$args) ];
132 2         36 return $expected->validate( $response, $self );
133             };
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, and does not pick up methods available via C.
140              
141             =cut
142              
143             sub method_exists {
144 5     5 1 23 my ( $self, $method, $args, $expected, $reason ) = @_;
145 5         27 $self->add_test( 'method_exists', { method => $method, }, $reason );
146             }
147              
148             define_test method_exists => sub {
149 5     5   10 my ( $self, $data, $reason ) = @_; # self is the runner
150 5         13 my $method = $data->{method};
151 5 100       125 return $self->pass if ( $self->subject->can($method) );
152 2         12 return $self->fail;
153             };
154              
155             =head1 OTHER INFORMATION
156              
157             For author, version, bug reports, support, etc, please see L.
158              
159             =cut
160              
161             1;