File Coverage

blib/lib/Locale/Maketext/Utils/Mock.pm
Criterion Covered Total %
statement 53 56 94.6
branch 11 14 78.5
condition 3 3 100.0
subroutine 14 14 100.0
pod 2 2 100.0
total 83 89 93.2


line stmt bran cond sub pod time code
1             package Locale::Maketext::Utils::Mock;
2              
3 6     6   131964 use strict;
  6         18  
  6         284  
4 6     6   38 use warnings;
  6         10  
  6         380  
5 6     6   3027 use Locale::Maketext::Utils (); # brings in Locales.pm
  6         20  
  6         214  
6 6     6   47 use base 'Locale::Maketext::Utils';
  6         30  
  6         1276  
7              
8             $Locale::Maketext::Utils::Mock::VERSION = '0.1';
9              
10             package Locale::Maketext::Utils::Mock::en;
11 6     6   47 use base 'Locale::Maketext::Utils::Mock';
  6         13  
  6         2268  
12             our %Lexicon;
13              
14             package Locale::Maketext::Utils::Mock;
15              
16             our %Lexicon;
17              
18             sub init_mock_locales {
19 5     5 1 882 my $cnt = 0;
20 5         13 for my $loc_tag ( map { Locales::normalize_tag($_) } @_ ) {
  10         154  
21 10 50       2537 next if !$loc_tag;
22 10 100 100     63 next unless $loc_tag =~ m/^i_/ || Locales->new($loc_tag);
23              
24 5         36564 $cnt++;
25 5     1   455 eval "package Locale::Maketext::Utils::Mock::$loc_tag;use base 'Locale::Maketext::Utils::Mock';our \%Lexicon;package Locale::Maketext::Utils::Mock;";
  1     1   8  
  1     1   1  
  1     1   152  
  1     1   8  
  1         2  
  1         161  
  1         10  
  1         1  
  1         138  
  1         10  
  1         2  
  1         141  
  1         8  
  1         2  
  1         154  
26 5 50       25 if ($@) {
27 0         0 $cnt--;
28 0         0 require Carp;
29 0         0 Carp::carp($@);
30             }
31              
32             }
33              
34 5         1161 return $cnt;
35             }
36              
37             sub create_method {
38 10     10 1 28 my ( $class, $def_hr ) = @_;
39 10 100       30 if ( @_ == 2 ) {
40 8 100       26 $class = ref($class) if ref($class);
41             }
42             else {
43 2         3 $def_hr = $class; # was a function call
44 2         4 $class = 'Locale::Maketext::Utils::Mock';
45             }
46 10 50       34 return if ref($def_hr) ne 'HASH';
47 6     6   49 no strict 'refs';
  6         12  
  6         1156  
48 10         11 for my $m ( sort keys %{$def_hr} ) {
  10         41  
49 16 100   5   94 *{ $class . "::$m" } = ref( $def_hr->{$m} ) eq 'CODE' ? $def_hr->{$m} : sub { return "I am $m()." };
  16         148  
  5         30  
50             }
51             }
52              
53             1;
54              
55             __END__
56              
57             =encoding utf-8
58              
59             =head1 NAME
60              
61             Locale::Maketext::Utils::Mock - mock locale object
62              
63             =head1 VERSION
64              
65             This document describes Locale::Maketext::Utils::Mock version 0.1
66              
67             =head1 SYNOPSIS
68              
69             use Locale::Maketext::Utils::Mock;
70             my $lh = Locale::Maketext::Utils::Mock->get_handle();
71              
72             =head1 DESCRIPTION
73              
74             Often we need to create a class so we can do a Locale::Maketext[::Utils] testing but Locale::Maketext is not designed for direct use. Instead you need to create a class with a lexicon and at least the 'en' subclass of that.
75              
76             This module does all the work for you and behaves like a typical L<Locale::Maketext::Utils> object.
77              
78             You can also add additional locales at will.
79              
80             =head1 INTERFACE
81              
82             =head2 get_handle()
83              
84             Requires the L<Locale::Maketext::Utils::Mock> based locale handle.
85              
86             =head2 init_mock_locales()
87              
88             If you want more than 'en' you can initialize them using init_mock_locales();
89              
90             It takes a list of locales tags, passes them through L<Locales::normalize_tag()|Locales/Utility functions> and creates the subclass for each one that can be used to create a L<Locales> object or that is an “i_” tag (e.g. i_yoda).
91              
92             use Locale::Maketext::Utils::Mock ();
93             Locale::Maketext::Utils::Mock->init_mock_locales('fr', 'it', 'en_gb');
94             my $it = Locale::Maketext::Utils::Mock->get_handle('it'); # Locale::Maketext::Utils::Mock::it object.
95             my $ja = Locale::Maketext::Utils::Mock->get_handle('js'); # Locale::Maketext::Utils::Mock::en object since there is no 'ja' subclass.
96              
97             It can be called as a function, a class method, or object method.
98              
99             It returns the number of subclasses successfully created.
100              
101             =head2 create_method()
102              
103             Takes a hashref whose keys are the methods to make for mocked objects.
104              
105             The values are either a coderef for the method or undef. Undef will result in a coderef that returns “I am $key()”.
106              
107             use Locale::Maketext::Utils::Mock ();
108             Locale::Maketext::Utils::Mock->create_method({ method_x => undef, method_y => sub { return "YABBA DABBA $_[1]" } });
109             my $lh = Locale::Maketext::Utils::Mock->get_handle();
110             print $lh->maketext("Blah [method_x] blah [method_y,_1] blah.", 42); $ Blah I am method_x() blah YABBA DABBA 42 blah.
111              
112             =head1 DIAGNOSTICS
113              
114             init_mock_locales() carp()s if there was a problem createing the subclass.
115              
116             =head1 CONFIGURATION AND ENVIRONMENT
117              
118             Locale::Maketext::Utils::Mock requires no configuration files or environment variables.
119              
120             =head1 DEPENDENCIES
121              
122             L<Locale::Maketext::Utils>
123              
124             =head1 INCOMPATIBILITIES
125              
126             None reported.
127              
128             =head1 BUGS AND LIMITATIONS
129              
130             No bugs have been reported.
131              
132             Please report any bugs or feature requests to
133             C<bug-locale-maketext-utils-mock@rt.cpan.org>, or through the web interface at
134             L<http://rt.cpan.org>.
135              
136             =head1 TODO
137              
138             Depending on need/demand:
139              
140             1. Add a way to define lexicon for specifical locales.
141              
142             2. ? ability to create object in it's own class (so subclass modification are separate) ?
143              
144             =head1 AUTHOR
145              
146             Daniel Muey C<< <http://drmuey.com/cpan_contact.pl> >>
147              
148             =head1 LICENCE AND COPYRIGHT
149              
150             Copyright (c) 2012, Daniel Muey C<< <http://drmuey.com/cpan_contact.pl> >>. All rights reserved.
151              
152             This module is free software; you can redistribute it and/or
153             modify it under the same terms as Perl itself. See L<perlartistic>.
154              
155             =head1 DISCLAIMER OF WARRANTY
156              
157             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
158             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
159             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
160             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
161             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
162             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
163             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
164             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
165             NECESSARY SERVICING, REPAIR, OR CORRECTION.
166              
167             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
168             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
169             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
170             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
171             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
172             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
173             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
174             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
175             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
176             SUCH DAMAGES.