Lite/Signature.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 98 CC 14 SCAR 42.2
CC
14
Cov
0%
Matches
132.0
MethodName
6.0
ReturnType
6.0
CRAP 68.1
1 package RPC::Lite::Signature;
2  
3 use strict;
4  
5 =pod
6  
7 =head1 NAME
8  
9 RPC::Lite::Signature - Object representation of method signatures.
10  
11 =head1 SYNOPSIS
12  
13 # create a signature for the method 'MethodName' that returns
14 # an int and takes an int, an array and a struct as arguments.
15 my $signature =
16 RPC::Lite::Signature->new( 'MethodName=int:int,array,struct' );
17
18 # prints "MethodName=int:int,array,struct
19 print $signature->AsString();
20
21 # change the signature
22 $signature->FromString( 'OtherMethod=array:int,struct' );
23  
24 # returns false after the update call to FromString above
25 my $result = $signature->Matches( 'MethodName=int:int,array,struct' );
26  
27 =head1 DESCRIPTION
28  
29 RPC::Lite::Signature is an object representation for method signatures.
30 For the most part, it should be used internally by RPC::Lite.
31  
32 =over 12
33  
34 =cut
35  
36 sub new
37 {
38 my $class = shift;
39 my $data = shift;
40  
41 my $self = bless {}, $class;
42  
43 $self->FromString( $data );
44  
45 return $self;
46 }
47  
48 =pod
49  
50 =item C<MethodName>
51  
52 Returns the method name the signature concerns.
53  
54 =item C<ReturnType>
55  
56 Returns a string specifying the return type.
57  
58 =item C<ArgumentTypeList>
59  
60 Returns an array reference to the list of argument types.
61  
62 =cut
63  
64 sub MethodName { $_[0]->{methodname} = $_[1] if @_ > 1; $_[0]->{methodname} }
65 sub ReturnType { $_[0]->{returntype} = $_[1] if @_ > 1; $_[0]->{returntype} }
66 sub ArgumentTypeList { $_[0]->{argumenttypelist} = $_[1] if @_ > 1; $_[0]->{argumenttypelist} }
67  
68 =pod
69  
70 =item C<AsString>
71  
72 Returns a string representation of the signature.
73  
74 =cut
75  
76 sub AsString
77 {
78 my $self = shift;
79  
80 return $self->MethodName . "=" . $self->ReturnType . ":" . join(',', @{$self->ArgumentTypeList});
81 }
82  
83 =pod
84  
85 =item C<FromString>
86  
87 Takes a string and sets the signatures properties from that string.
88  
89 =cut
90  
91 sub FromString
92 {
93 my $self = shift;
94 my $data = shift;
95  
96 my ( $methodName, $returnType, $argumentTypeList ) = $self->__ParseString( $data );
97  
98 $self->MethodName( $methodName );
99 $self->ReturnType( $returnType );
100 $self->ArgumentTypeList( $argumentTypeList );
101  
102 }
103  
104 =pod
105  
106 =item C<Matches>
107  
108 Returns true if this signature matches the signature passed as an argument.
109 The argument can either be a string (a method specification) or another
110 RPC::Lite::Signature object.
111  
112 =cut
113  
114 sub Matches
115 {
116 my $self = shift;
117 my $otherSignature = shift;
118
119 # string
120 if ( !ref( $otherSignature ) )
121 {
122 my ( $otherMethodName, $otherMethodReturnType, $otherMethodArgumentTypeList ) = $self->__ParseString( $otherSignature );
123
124 return 0 if ( $self->MethodName ne $otherMethodName );
125
126 return 0 if ( $self->ReturnType ne $otherMethodReturnType );
127
128 # make sure they take the same number of args
129 return 0 if ( @{ $self->ArgumentTypeList() } != @$otherMethodArgumentTypeList );
130
131 my $argumentIndex = 0;
132 foreach my $argumentType ( @{ $otherMethodArgumentTypeList } )
133 {
134 return 0 if ( $argumentType ne $self->ArgumentTypeList()->[ $argumentIndex ] );
135 ++$argumentIndex;
136 }
137 }
138 elsif ( ref( $otherSignature ) eq 'RPC::Lite::Signature' )
139 {
140 return 0 if ( $self->MethodName() ne $otherSignature->MethodName() );
141
142 return 0 if ( $self->ReturnType() ne $otherSignature->ReturnType() );
143
144 return 0 if ( @{ $self->ArgumentTypeList() } != @{ $otherSignature->ArgumentTypeList() } );
145
146 my $argumentIndex = 0;
147 foreach my $argumentType ( @{ $self->ArgumentTypeList() } )
148 {
149 return 0 if ( $argumentType ne $otherSignature->ArgumentTypeList()->[ $argumentIndex ] );
150 ++$argumentIndex;
151 }
152
153 }
154  
155 return 1;
156 }
157  
158 sub __ParseString
159 {
160 my $self = shift;
161 my $string = shift;
162
163 $string =~ s/\s+//g; # remove whitespace
164  
165 my ( $methodName, $returnType, $argumentTypeListString ) = $string =~ /^(.*?)=(.*?):(.*)$/;
166  
167 $returnType = lc( $returnType );
168 $argumentTypeListString = lc( $argumentTypeListString );
169  
170 my @argumentTypeList = split( /,/, $argumentTypeListString );
171  
172 return ( $methodName, $returnType, \@argumentTypeList );
173 }
174  
175 1;