blib/lib/Venus/Role/Mockable.pm

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