blib/lib/Test/Mockify/MethodCallCounter.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 27 | 27 | 100.0 |
branch | 2 | 2 | 100.0 |
condition | n/a | ||
subroutine | 7 | 7 | 100.0 |
pod | 0 | 4 | 0.0 |
total | 36 | 40 | 90.0 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | #======================================================================================== | ||||||
2 | # §package MethodCallCounter | ||||||
3 | # §state public | ||||||
4 | #---------------------------------------------------------------------------------------- | ||||||
5 | # §description encapsulate the Call Counter for Mockify | ||||||
6 | #======================================================================================== | ||||||
7 | package Test::Mockify::MethodCallCounter; | ||||||
8 | |||||||
9 | 2 | 2 | 7 | use Test::Mockify::Tools qw ( Error ); | |||
2 | 2 | ||||||
2 | 72 | ||||||
10 | |||||||
11 | 2 | 2 | 6 | use strict; | |||
2 | 3 | ||||||
2 | 340 | ||||||
12 | #======================================================================================== | ||||||
13 | # §function new | ||||||
14 | # §state public | ||||||
15 | #---------------------------------------------------------------------------------------- | ||||||
16 | # §syntax new( ); | ||||||
17 | #---------------------------------------------------------------------------------------- | ||||||
18 | # §description constructor | ||||||
19 | #---------------------------------------------------------------------------------------- | ||||||
20 | # §return $self | self | MethodCallCounter | ||||||
21 | #======================================================================================== | ||||||
22 | sub new { | ||||||
23 | 45 | 45 | 0 | 41 | my $class = shift; | ||
24 | 45 | 59 | my $self = bless {}, $class; | ||||
25 | 45 | 177 | return $self; | ||||
26 | } | ||||||
27 | #======================================================================================== | ||||||
28 | # §function addMethod | ||||||
29 | # §state public | ||||||
30 | #---------------------------------------------------------------------------------------- | ||||||
31 | # §syntax addMethod( $MethodName ); | ||||||
32 | #---------------------------------------------------------------------------------------- | ||||||
33 | # §description add the Method '$MethodName' to the counter | ||||||
34 | #---------------------------------------------------------------------------------------- | ||||||
35 | # §input $MethodName | name of method | string | ||||||
36 | #======================================================================================== | ||||||
37 | sub addMethod { | ||||||
38 | 40 | 40 | 0 | 36 | my $self = shift; | ||
39 | 40 | 37 | my ( $MethodName ) = @_; | ||||
40 | |||||||
41 | 40 | 54 | $self->{$MethodName} = 0; | ||||
42 | |||||||
43 | 40 | 51 | return; | ||||
44 | } | ||||||
45 | #======================================================================================== | ||||||
46 | # §function increment | ||||||
47 | # §state public | ||||||
48 | #---------------------------------------------------------------------------------------- | ||||||
49 | # §syntax increment( $MethodName ); | ||||||
50 | #---------------------------------------------------------------------------------------- | ||||||
51 | # §description increment the the counter for the Method '$MethodName' | ||||||
52 | #---------------------------------------------------------------------------------------- | ||||||
53 | # §input $MethodName | name of method | string | ||||||
54 | #======================================================================================== | ||||||
55 | sub increment { | ||||||
56 | 46 | 46 | 0 | 44 | my $self = shift; | ||
57 | 46 | 36 | my ( $MethodName ) = @_; | ||||
58 | |||||||
59 | 46 | 62 | $self->_testIfMethodWasAdded( $MethodName ); | ||||
60 | 46 | 46 | $self->{$MethodName} += 1; | ||||
61 | |||||||
62 | 46 | 77 | return; | ||||
63 | } | ||||||
64 | #======================================================================================== | ||||||
65 | # §function getAmountOfCalls | ||||||
66 | # §state public | ||||||
67 | #---------------------------------------------------------------------------------------- | ||||||
68 | # §syntax getAmountOfCalls( $MethodName ); | ||||||
69 | #---------------------------------------------------------------------------------------- | ||||||
70 | # §description returns the amount of calls for the method '$MethodName' | ||||||
71 | # throws error if the method was not added to Mockify | ||||||
72 | #---------------------------------------------------------------------------------------- | ||||||
73 | # §input $MethodName | name of method | string | ||||||
74 | # §return $AmountOfCalls | Amount of calles | integer | ||||||
75 | #======================================================================================== | ||||||
76 | sub getAmountOfCalls { | ||||||
77 | 11 | 11 | 0 | 9 | my $self = shift; | ||
78 | 11 | 11 | my ( $MethodName ) = @_; | ||||
79 | |||||||
80 | 11 | 13 | $self->_testIfMethodWasAdded( $MethodName ); | ||||
81 | 10 | 11 | my $AmountOfCalls = $self->{ $MethodName }; | ||||
82 | |||||||
83 | 10 | 17 | return $AmountOfCalls; | ||||
84 | } | ||||||
85 | #---------------------------------------------------------------------------------------- | ||||||
86 | sub _testIfMethodWasAdded { | ||||||
87 | 57 | 57 | 43 | my $self = shift; | |||
88 | 57 | 38 | my ( $MethodName ) = @_; | ||||
89 | |||||||
90 | 57 | 100 | 140 | if( not exists $self->{ $MethodName } ){ | |||
91 | 1 | 12 | Error( "The Method: '$MethodName' was not added to Mockify" ); | ||||
92 | } | ||||||
93 | } | ||||||
94 | 1; |