blib/lib/Venus/Role/Mockable.pm

filter: Statementstmt 100% 25 / 25 Branchbran n/a Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 90.0% 36 / 40 SCAR 6.9
CC
2
Cov
96%
mock
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
« blib/lib/Venus/Role/Patchable.pm ↑ Summary blib/lib/Venus/Role/Tryable.pm »
1 package Venus::Role::Mockable;
2  
3 96 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 96 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # METHODS
11  
12 sub mock {
13 4 ▶ my ($self, $name, $code) = @_;
Subroutine mock: called CC=2 SCAR=6.9
Pod documented
14  
15 96 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 96 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
130
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
19  
20 4 my $orig = $class->can($name);
21  
22 4 *{"${class}::${name}"} = my $mock = $code->($orig);
23  
24 4 return $mock;
25 }
26  
27 # EXPORTS
28  
29 sub EXPORT {
30 97 ▶ ['mock']
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
31 }
32  
33 96 1;
34  
35  
36  
37 =head1 NAME
38  
39 Venus::Role::Mockable - Mockable Role
40  
41 =cut
42  
43 =head1 ABSTRACT
44  
45 Mockable Role for Perl 5
46  
47 =cut
48  
49 =head1 SYNOPSIS
50  
51 package Example;
52  
53 use Venus::Class 'with';
54  
55 with 'Venus::Role::Mockable';
56  
57 sub execute {
58 [1..4];
59 }
60  
61 package main;
62  
63 my $example = Example->new;
64  
65 # my $mock = $example->mock(execute => sub {
66 # my ($next) = @_;
67 #
68 # return sub {
69 # [@{$next->()}, @_]
70 # }
71 # });
72  
73 # sub { ... }
74  
75 =cut
76  
77 =head1 DESCRIPTION
78  
79 This package provides a mechanism for mocking subroutines.
80  
81 =cut
82  
83 =head1 METHODS
84  
85 This package provides the following methods:
86  
87 =cut
88  
89 =head2 mock
90  
91 mock(string $name, coderef $code) (coderef)
92  
93 The mock method mocks the subroutine specified using the callback given. The
94 coderef provided will be passed the original subroutine coderef as its first
95 argument. The coderef provided should always return a coderef that will serve
96 as the subroutine mock.
97  
98 I<Since C<2.32>>
99  
100 =over 4
101  
102 =item mock example 1
103  
104 package main;
105  
106 my $example = Example->new;
107  
108 my $mock = $example->mock(execute => sub {
109 my ($next) = @_;
110  
111 return sub {
112 [@{$next->()}, @_]
113 }
114 });
115  
116 # sub { ... }
117  
118 # $example->execute;
119  
120 # [1..4]
121  
122 # $example->execute(5, 6);
123  
124 # [1..6]
125  
126 =back
127  
128 =cut
129  
130 =head1 AUTHORS
131  
132 Awncorp, C<awncorp@cpan.org>
133  
134 =cut
135  
136 =head1 LICENSE
137  
138 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
139  
140 This program is free software, you can redistribute it and/or modify it under
141 the terms of the Apache license version 2.0.
142  
143 =cut
« blib/lib/Venus/Role/Patchable.pm ↑ Summary blib/lib/Venus/Role/Tryable.pm »