lib/Text/Sass/Expr.pm

filter: Statementstmt 74.1% 92 / 124 Branchbran 61.1% 22 / 36 Conditioncond 70.0% 14 / 20 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 73.9% 17 / 23 Podpod 100% 5 / 5 totaltotal 70.3% 154 / 219 CC 27 SCAR 24.0
CC
27
Cov
71%
expr
21.5
__ANON__
20.0
__ANON__
4.4
CRAP 11.0
« lib/Text/Sass.pm ↑ Summary script/sass2css.pl »
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 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 35 our $VERSION = q[1.1.0];
16  
17 35 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 0 ▶ if($nr < 0) { $nr = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nr < 0) 0 2 2
33 0 ▶ if($ng < 0) { $ng = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ng < 0) 0 2 2
34 0 ▶ 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 35 };
49  
50 35 Readonly::Scalar our $MM2CM => 0.1;
51 35 Readonly::Scalar our $CM2MM => 10;
52 35 Readonly::Scalar our $IN2CM => 2.54;
53 35 Readonly::Scalar our $CM2IN => 1/2.54;
54 35 Readonly::Scalar our $IN2MM => 25.4;
55 35 Readonly::Scalar our $MM2IN => 1/25.4;
56 35 Readonly::Scalar our $PC2PT => 12;
57 35 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 35 };
69  
70 17 ▶sub expr {
Subroutine expr: called CC=19 SCAR=30.7
Pod documented
71 17 my ($pkg, $part1, $op, $part2) = @_;
72  
73 17 $part1 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi;
74 17 $part2 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi;
75  
76 17 my ($p1, $u1) = @{$pkg->units($part1)};
77 17 my ($p2, $u2) = @{$pkg->units($part2)};
78 17 ▶ return if(!defined $p1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p1 2 15 17
79  
80 15 ▶ if(!$u1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u1) 1 14 15
81 1 $u1 = q[];
82 }
83  
84 15 ▶ if(!$u2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u2) 8 7 15
85 8 $u2 = q[];
86 }
87  
88 15 ▶ if(!$u1 && $u2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $u1 and $u2) 0 15 15
Condition66%2/3
not $u1 and $u2
!ll&&!rl&&r
1410
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 15 ▶ if(!$u2 && $u1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $u2 and $u1) 7 8 15
Condition100%3/3
not $u2 and $u1
!ll&&!rl&&r
717
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 15 ▶ if($u1 ne $u2 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($u1 ne $u2 and $u1 ne "#" and $u2 ne "#") 2 13 15
Condition83%5/6
$u1 ne $u2 and $u1 ne "#" and $u2 ne "#"
!ll&&!rl&&r
1122
$u1 ne $u2 and $u1 ne "#"
!ll&&!rl&&r
1104
MC/DC66%2/3
$u1 ne $u2 and $u1 ne "#" and $u2 ne "#"
$u1 ne $u2 $u1 ne "#" $u2 ne "#"
Truth table75%3/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 15 ▶ if(!exists $OPS->{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Text::Sass::Expr::OPS->{$op}) 3 12 15
104 3 ▶ if ($op =~ /^\w/smx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ /^\w/msx) { } 3 0 3
elsif ($op =~ /\S{2,}/msx) { } 0 0 0
105 3 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 92 ▶sub units {
Subroutine units: called CC=2 SCAR=6.9
Pod documented
123 92 my ($pkg, $token) = @_;
124  
125 92 ▶ if($token =~ /^[#]/smx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^[#]/msx) 32 60 92
126 32 $token =~ s/^[#]//smx;
127 32 return [$pkg->hex_to_rgb($token), q[#]];
128 }
129  
130 60 my ($val, $units) = $token =~ /([\d.]+)(px|pt|pc|em|ex|mm|cm|in|%|)/smx;
131  
132 60 return [$val, $units];
133 }
134  
135 2 ▶sub rgb_to_hex {
Subroutine rgb_to_hex: called CC=1 SCAR=0.0
Pod documented
136 2 my ($pkg, $triple_ref) = @_;
137 2 return sprintf q[%02x%02x%02x], @{$triple_ref};
138 }
139  
140 32 ▶sub hex_to_rgb {
Subroutine hex_to_rgb: called CC=1 SCAR=0.0
Pod documented
141 32 my ($pkg, $hex) = @_;
142  
143 32 my ($r, $g, $b) = unpack q[A2A2A2], $hex;
144 32 return [hex $r, hex $g, hex $b];
145 }
146  
147 5 ▶sub convert {
Subroutine convert: called CC=2 SCAR=7.4
Pod documented
148 5 my ($pkg, $val, $from, $to) = @_;
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 35 1;
159 __END__
« lib/Text/Sass.pm ↑ Summary script/sass2css.pl »