line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Magpie::Util; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Test::Magpie::Util::VERSION = '0.11'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Internal utility functions for Test::Magpie |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
4818
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
35
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# smartmatch dependencies |
11
|
1
|
|
|
1
|
|
28
|
use 5.010001; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
44
|
|
12
|
1
|
|
|
1
|
|
1855
|
use experimental qw( smartmatch ); |
|
1
|
|
|
|
|
957
|
|
|
1
|
|
|
|
|
6
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
69
|
use Exporter qw( import ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
15
|
1
|
|
|
1
|
|
6
|
use Scalar::Util qw( blessed looks_like_number refaddr ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
108
|
|
16
|
1
|
|
|
1
|
|
1654
|
use Moose::Util qw( find_meta ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
19
|
|
|
|
|
|
|
extract_method_name |
20
|
|
|
|
|
|
|
get_attribute_value |
21
|
|
|
|
|
|
|
has_caller_package |
22
|
|
|
|
|
|
|
match |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub extract_method_name { |
27
|
|
|
|
|
|
|
my ($method_name) = @_; |
28
|
|
|
|
|
|
|
$method_name =~ s/.*:://; |
29
|
|
|
|
|
|
|
return $method_name; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub get_attribute_value { |
34
|
|
|
|
|
|
|
my ($object, $attribute) = @_; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
return find_meta($object) |
37
|
|
|
|
|
|
|
->find_attribute_by_name($attribute) |
38
|
|
|
|
|
|
|
->get_value($object); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub has_caller_package { |
43
|
|
|
|
|
|
|
my $package= shift; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $level = 1; |
46
|
|
|
|
|
|
|
while (my ($caller) = caller $level++) { |
47
|
|
|
|
|
|
|
return 1 if $caller eq $package; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub match { |
54
|
|
|
|
|
|
|
my ($a, $b) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# This function uses smart matching, but we need to limit the scenarios |
57
|
|
|
|
|
|
|
# in which it is used because of its quirks. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# ref types must match |
60
|
|
|
|
|
|
|
return if ref($a) ne ref($b); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# objects match only if they are the same object |
63
|
|
|
|
|
|
|
if (blessed($a) || ref($a) eq 'CODE') { |
64
|
|
|
|
|
|
|
return refaddr($a) == refaddr($b); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# don't smartmatch on arrays because it recurses |
68
|
|
|
|
|
|
|
# which leads to the same quirks that we want to avoid |
69
|
|
|
|
|
|
|
if (ref($a) eq 'ARRAY') { |
70
|
|
|
|
|
|
|
return if $#{$a} != $#{$b}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# recurse to handle nested structures |
73
|
|
|
|
|
|
|
foreach (0 .. $#{$a}) { |
74
|
|
|
|
|
|
|
return if !match( $a->[$_], $b->[$_] ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
return 1; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# smartmatch only matches hash keys |
80
|
|
|
|
|
|
|
# but we want to match the values too |
81
|
|
|
|
|
|
|
if (ref($a) eq 'HASH') { |
82
|
|
|
|
|
|
|
return unless $a ~~ $b; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
foreach (keys %$a) { |
85
|
|
|
|
|
|
|
return if !match( $a->{$_}, $b->{$_} ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
return 1; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# avoid smartmatch doing number matches on strings |
91
|
|
|
|
|
|
|
# e.g. '5x' ~~ 5 is true |
92
|
|
|
|
|
|
|
return if looks_like_number($a) xor looks_like_number($b); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return $a ~~ $b; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=pod |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=encoding utf-8 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 NAME |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Test::Magpie::Util - Internal utility functions for Test::Magpie |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 FUNCTIONS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 extract_method_name |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
$method_name = extract_method_name($full_method_name) |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
From a fully qualified method name such as Foo::Bar::baz, will return |
116
|
|
|
|
|
|
|
just the method name (in this example, baz). |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 get_attribute_value |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$value = get_attribute_value($object, $attr_name) |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Gets value of Moose attributes that have no accessors by accessing the class' |
123
|
|
|
|
|
|
|
underlying meta-object. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 has_caller_package |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$bool = has_caller_package($package_name) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Returns whether the given C<$package> is in the current call stack. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 match |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$bool = match($a, $b) |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Match 2 values for equality. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 AUTHORS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=over 4 |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item * |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Oliver Charles <oliver.g.charles@googlemail.com> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item * |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Steven Lee <stevenwh.lee@gmail.com> |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=back |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Oliver Charles. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
156
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |