blib/lib/Mason/Plugin/WithEncoding/Test/Class.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 4 totaltotal 89.6% 69 / 77 CC 7 SCAR 9.7
CC
7
Cov
94%
content_for_tests
5.0
add_comp
3.1
BEGIN
1.0
CRAP 2.6
« blib/lib/Mason/Plugin/WithEncoding.pm ↑ Summary blib/lib/Mason/Plugin/WithEncoding/t/UTF8.pm »
1 package Mason::Plugin::WithEncoding::Test::Class;
2 4 $Mason::Plugin::WithEncoding::Test::Class::VERSION = '0.2';
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # http://www.effectiveperlprogramming.com/2011/07/fix-testbuilders-unicode-issue/
6 4 ▶binmode Test::More->builder->output(), ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 binmode Test::More->builder->failure_output(), ':encoding(UTF-8)';
8  
9 4 ▶use Test::Class::Most parent => 'Poet::Test::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Poet::Tools qw(dirname mkpath trim write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶sub mech {
Subroutine mech: called CC=1 SCAR=0.0
Pod undocumented
15 2 my $self = shift;
16 2 my $poet = shift;
17 2 my $mech = $self->SUPER::mech( env => $poet );
18 2 @{ $mech->requests_redirectable } = ();
19 2 return $mech;
20 }
21  
22 10 ▶sub add_comp {
Subroutine add_comp: called CC=3 SCAR=11.2
Pod undocumented
23 10 my ( $self, %params ) = @_;
24 10 ▶ my $path = $params{path} or die "must pass path";
Branch: 1 branch
BranchTrueFalseTotal
my $path = $params{"path"} or die "must pass path" 10 0 10
25 10 ▶ my $src = $params{src} or die "must pass src";
Branch: 1 branch
BranchTrueFalseTotal
my $src = $params{"src"} or die "must pass src" 10 0 10
26 10 my $file = $params{poet}->comps_dir . $path;
27 10 mkpath( dirname($file), 0, 0775 ); ## no critic (ValuesAndExpressions::ProhibitLeadingZeros)
28 10 write_file( $file, $src );
29 }
30  
31 # Encode the content because it is leaving Perl (which uses its own
32 # internal character representation) and being sent to the system for
33 # storage. When Mason reads the component source back again, the 'use utf8;'
34 # added to comp headers will decode the source back into internal Perly format.
35 # Then the WithEncoding plugin will encode content sent back out, as utf8 (in this
36 # case, but in general as whatever is configured).
37 2 ▶sub add_comps {
Subroutine add_comps: called CC=1 SCAR=0.0
Pod undocumented
38 2 my $self = shift;
39 2 my $poet = shift;
40 # Don't encode the path because, hmm, not sure. Because it 'just works' as-is.
41 2 $self->add_comp(path => '/♥♥♥.mc', src => encode('UTF-8', $self->content_for_tests('utf8')), poet => $poet);
42 2 $self->add_comp(path => '/utf8.mc', src => encode('UTF-8', $self->content_for_tests('utf8')), poet => $poet);
43 2 $self->add_comp(path => '/plain.mc', src => encode('UTF-8', $self->content_for_tests('plain')), poet => $poet);
44 2 $self->add_comp(path => '/dies.mc', src => encode('UTF-8', $self->content_for_tests('dies')), poet => $poet);
45 2 $self->add_comp(path => '/json.mc', src => encode('UTF-8', $self->content_for_tests('json')), poet => $poet);
46 }
47  
48 10 ▶sub content_for_tests {
Subroutine content_for_tests: called CC=5 SCAR=16.2
Pod undocumented
49 10 my ($self, $want) = @_;
50  
51 10 my $src_utf8 = <<UTF8;
52 % sub { uc(\$_[0]) } {{
53 a quick brown fox jumps over the lazy dog.
54  
55 διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα.
56 árvíztűrő tükörfúrógép.
57 dość gróźb fuzją, klnę, pych i małżeństw!
58 эх, чужак, общий съём цен шляп (юфть) – вдрызг!
59 kŕdeľ šťastných ďatľov učí pri ústí váhu mĺkveho koňa obhrýzať kôru a žrať čerstvé mäso.
60 zwölf boxkämpfer jagen viktor quer über den großen sylter deich.
61  
62 % }}
63  
64 QUERY STRING FROM REQ: <% \$m->req->query_string %>
65  
66 % use URI::Escape;
67 QUERY STRING UNESCAPED: <% \$m->req->query_string %>
68  
69 UTF8
70  
71 # I think if everything was running correctly, this wouldn't die:
72 10 my $src_utf8_dies = <<UTF8;
73 <% \$.args->{♥} %>
74 UTF8
75  
76 10 my $src_plain = <<ASCII;
77 % sub { uc(\$_[0]) } {{
78  
79 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ut ante
80 mollis, ultricies arcu ut, convallis libero. Fusce a felis sapien. Aliquam
81 aliquam felis ut justo aliquam, non sollicitudin tellus porta. Etiam
82 sollicitudin, mi eu vulputate sagittis, elit arcu molestie leo, eget finibus
83 tortor risus ut quam. Aenean id dolor eros. Vestibulum dictum, sem vitae
84 molestie feugiat, enim quam ultricies metus, quis dapibus sapien orci ut risus.
85  
86 % }}
87  
88 QUERY STRING FROM REQ: <% \$m->req->query_string %>
89  
90 % use URI::Escape;
91 QUERY STRING UNESCAPED: <% uri_unescape(\$m->req->query_string) %>
92  
93 ASCII
94  
95 10 my $src_json = <<SRC;
96 <\%init>
97 my \$data_for_json = {
98 foo => 'bar',
99 baz => [qw(barp beep)],
100 9 => { one => 1, ex => 'EKS' },
101 heart => '♥',
102 };
103 </\%init>
104 % \$m->send_json(\$data_for_json);
105 SRC
106  
107  
108  
109 10 ▶ return $src_utf8 if $want eq 'utf8';
Branch: 1 branch
BranchTrueFalseTotal
if $want eq "utf8" 4 6 10
110 6 ▶ return $src_plain if $want eq 'plain';
Branch: 1 branch
BranchTrueFalseTotal
if $want eq "plain" 2 4 6
111 4 ▶ return $src_utf8_dies if $want eq 'dies';
Branch: 1 branch
BranchTrueFalseTotal
if $want eq "dies" 2 2 4
112 2 ▶ return $src_json if $want eq 'json';
Branch: 1 branch
BranchTrueFalseTotal
if $want eq "json" 2 0 2
113 0 die "No content for '$want'";
114 }
115  
116 4 1;
« blib/lib/Mason/Plugin/WithEncoding.pm ↑ Summary blib/lib/Mason/Plugin/WithEncoding/t/UTF8.pm »