blib/lib/Text/Sass/Expr.pm

filter: Statementstmt 74.1% 92 / 124 Branchbran 61.1% 22 / 36 Conditioncond 65.0% 13 / 20 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 73.9% 17 / 23 Podpod 100% 5 / 5 totaltotal 69.4% 152 / 219 CC 27 SCAR 24.2
CC
27
Cov
71%
expr
22.2
__ANON__
20.0
__ANON__
4.4
CRAP 11.2
« blib/lib/Text/Sass.pm ↑ Summary Build »
1 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2 # vim:ts=8:sw=2:et:sta:sts=2
3 #########
4 # Author: rmp
5 # Last Modified: $Date: 2012-09-12 09:42:30 +0100 (Wed, 12 Sep 2012) $
6 # Id: $Id: Expr.pm 71 2012-09-12 08:42:30Z zerojinx $
7 # $HeadURL: https://text-sass.svn.sourceforge.net/svnroot/text-sass/trunk/lib/Text/Sass/Expr.pm $
8 #
9 package Text::Sass::Expr;
10 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 31 our $VERSION = q[1.0.4];
16  
17 31 Readonly::Scalar our $SHADE_MAX => 255;
18  
19 # yes, this should be tokenised and probably use overloading
20  
21 our $OPS = {
22 2 ▶ q[-] => sub { my ($x, $y) = @_; return $x - $y; },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 0 ▶ q[+] => sub { my ($x, $y) = @_; return $x + $y; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 8 ▶ q[/] => sub { my ($x, $y) = @_; return $x / $y; },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 0 ▶ q[*] => sub { my ($x, $y) = @_; return $x * $y; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 2 ▶ q[#-] => sub { my ($x, $y) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.8
27 2 my ($xr, $xg, $xb) = @{$x};
28 2 my ($yr, $yg, $yb) = @{$y};
29 2 my $nr = $xr-$yr;
30 2 my $ng = $xg-$yg;
31 2 my $nb = $xb-$yb;
32 2 ▶ if($nr < 0) { $nr = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nr < 0) 0 2 2
33 2 ▶ if($ng < 0) { $ng = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ng < 0) 0 2 2
34 2 ▶ if($nb < 0) { $nb = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nb < 0) 0 2 2
35 2 return rgb_to_hex(undef,[$nr, $ng, $nb]);
36 },
37 0 ▶ q[#+] => sub { my ($x, $y) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
38 0 my ($xr, $xg, $xb) = @{$x};
39 0 my ($yr, $yg, $yb) = @{$y};
40 0 my $nr = $xr+$yr;
41 0 my $ng = $xg+$yg;
42 0 my $nb = $xb+$yb;
43 0 ▶ if($nr > $SHADE_MAX) { $nr = $SHADE_MAX; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nr > $Text::Sass::Expr::SHADE_MAX) 0 0 0
44 0 ▶ if($ng > $SHADE_MAX) { $ng = $SHADE_MAX; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ng > $Text::Sass::Expr::SHADE_MAX) 0 0 0
45 0 ▶ if($nb > $SHADE_MAX) { $nb = $SHADE_MAX; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nb > $Text::Sass::Expr::SHADE_MAX) 0 0 0
46 0 return rgb_to_hex(undef,[$nr, $ng, $nb]);
47 },
48 31 };
49  
50 31 Readonly::Scalar our $MM2CM => 0.1;
51 31 Readonly::Scalar our $CM2MM => 10;
52 31 Readonly::Scalar our $IN2CM => 2.54;
53 31 Readonly::Scalar our $CM2IN => 1/2.54;
54 31 Readonly::Scalar our $IN2MM => 25.4;
55 31 Readonly::Scalar our $MM2IN => 1/25.4;
56 31 Readonly::Scalar our $PC2PT => 12;
57 31 Readonly::Scalar our $PT2PC => 1/12;
58  
59 our $CONV = {
60 1 ▶ q[mm:cm] => sub { my ($v) = @_; return $v*$MM2CM; },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 ▶ q[cm:mm] => sub { my ($v) = @_; return $v*$CM2MM; },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 ▶ q[in:cm] => sub { my ($v) = @_; return $v*$IN2CM; },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1 ▶ q[cm:in] => sub { my ($v) = @_; return $v*$CM2IN; },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 1 ▶ q[in:mm] => sub { my ($v) = @_; return $v*$IN2MM; },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 0 ▶ q[mm:in] => sub { my ($v) = @_; return $v*$MM2IN; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ q[pc|pt] => sub { my ($v) = @_; return $v*$PC2PT; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 ▶ q[pt|pc] => sub { my ($v) = @_; return $v*$PT2PC; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 31 };
69  
70 sub expr {
71 15 ▶ my ($pkg, $part1, $op, $part2) = @_;
Subroutine expr: called CC=19 SCAR=31.0
Pod documented
72  
73 15 $part1 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi;
74 15 $part2 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi;
75  
76 15 my ($p1, $u1) = @{$pkg->units($part1)};
77 15 my ($p2, $u2) = @{$pkg->units($part2)};
78 15 ▶ return if(!defined $p1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p1 2 13 15
79  
80 13 ▶ if(!$u1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u1) 1 12 13
81 1 $u1 = q[];
82 }
83  
84 13 ▶ if(!$u2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u2) 8 5 13
85 8 $u2 = q[];
86 }
87  
88 13 ▶ if(!$u1 && $u2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $u1 and $u2) 0 13 13
Condition66%2/3
not $u1 and $u2
!ll&&!rl&&r
1210
MC/DC0%0/2
not $u1 and $u2
not $u1 $u2
Truth table66%2/3
not(A) and B
A= not $u1B= $u2
ABresult
0X0
100
111
89 0 $u1 = $u2;
90 }
91  
92 13 ▶ if(!$u2 && $u1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $u2 and $u1) 7 6 13
Condition100%3/3
not $u2 and $u1
!ll&&!rl&&r
517
MC/DC100%2/2
not $u2 and $u1
not $u2 $u1
Truth table100%3/3
not(A) and B
A= not $u2B= $u1
ABresult
0X0
100
111
93 7 $u2 = $u1;
94 }
95  
96 13 ▶ if($u1 ne $u2 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($u1 ne $u2 and $u1 ne "#" and $u2 ne "#") 2 11 13
Condition66%4/6
$u1 ne $u2 and $u1 ne "#" and $u2 ne "#"
!ll&&!rl&&r
1102
$u1 ne $u2 and $u1 ne "#"
!ll&&!rl&&r
1102
MC/DC33%1/3
$u1 ne $u2 and $u1 ne "#" and $u2 ne "#"
$u1 ne $u2 $u1 ne "#" $u2 ne "#"
Truth table50%2/4
A and B and C
A= $u1 ne $u2B= $u1 ne "#"C= $u2 ne "#"
ABCresult
0XX0
10X0
1100
1111
97 $u1 ne q[#] &&
98 $u2 ne q[#]) {
99 2 $p2 = $pkg->convert($p2, $u2, $u1);
100 2 $u2 = $u1;
101 }
102  
103 13 ▶ if(!exists $OPS->{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Text::Sass::Expr::OPS->{$op}) 1 12 13
104 1 ▶ if ($op =~ /^\w/smx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ /^\w/msx) { } 1 0 1
elsif ($op =~ /\S{2,}/msx) { } 0 0 0
105 1 return;
106 }
107 elsif ($op =~ /\S{2,}/smx) {
108 0 return;
109 }
110 0 croak qq[Cannot "$op"];
111 }
112  
113 12 ▶ if($u1 eq q[#]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u1 eq "#") 2 10 12
114 2 my $cb = $OPS->{"#$op"};
115  
116 2 ▶ return sprintf q[#%s], $cb->($p1||[0,0,0], $p2||[0,0,0]);
Condition50%2/4
$p1 || [0, 0, 0]
l!l
20
$p2 || [0, 0, 0]
l!l
20
MC/DC0%0/1
$p1 || [0, 0, 0]
$p1
MC/DC0%0/1
$p2 || [0, 0, 0]
$p2
Truth table50%1/2
A
A= $p1
Aresult
11
00
Truth table50%1/2
A
A= $p2
Aresult
11
00
117 }
118  
119 10 ▶ return sprintf q[%s%s], $OPS->{$op}->($p1||0, $p2||0), $u1;
Condition50%2/4
$p1 || 0
l!l
100
$p2 || 0
l!l
100
MC/DC0%0/1
$p1 || 0
$p1
MC/DC0%0/1
$p2 || 0
$p2
Truth table50%1/2
A
A= $p1
Aresult
11
00
Truth table50%1/2
A
A= $p2
Aresult
11
00
120 }
121  
122 sub units {
123 88 ▶ my ($pkg, $token) = @_;
Subroutine units: called CC=2 SCAR=6.9
Pod documented
124  
125 88 ▶ if($token =~ /^[#]/smx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^[#]/msx) 30 58 88
126 30 $token =~ s/^[#]//smx;
127 30 return [$pkg->hex_to_rgb($token), q[#]];
128 }
129  
130 58 my ($val, $units) = $token =~ /([\d.]+)(px|pt|pc|em|ex|mm|cm|in|%|)/smx;
131  
132 58 return [$val, $units];
133 }
134  
135 sub rgb_to_hex {
136 2 ▶ my ($pkg, $triple_ref) = @_;
Subroutine rgb_to_hex: called CC=1 SCAR=0.0
Pod documented
137 2 return sprintf q[%02x%02x%02x], @{$triple_ref};
138 }
139  
140 sub hex_to_rgb {
141 30 ▶ my ($pkg, $hex) = @_;
Subroutine hex_to_rgb: called CC=1 SCAR=0.0
Pod documented
142  
143 30 my ($r, $g, $b) = unpack q[A2A2A2], $hex;
144 30 return [hex $r, hex $g, hex $b];
145 }
146  
147 sub convert {
148 5 ▶ my ($pkg, $val, $from, $to) = @_;
Subroutine convert: called CC=2 SCAR=7.4
Pod documented
149  
150 5 my $fromto = "$from:$to";
151 5 ▶ if(!exists $CONV->{$fromto}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Text::Sass::Expr::CONV->{$fromto}) 0 5 5
152 0 croak qq[Cannot convert from $from to $to];
153 }
154  
155 5 return $CONV->{$fromto}->($val);
156 }
157  
158 31 1;
159 __END__
« blib/lib/Text/Sass.pm ↑ Summary Build »