blib/lib/Text/Nunjucks/Tests.pm

filter: Statementstmt 61.1% 123 / 201 Branchbran 50.0% 18 / 36 Conditioncond 50.0% 15 / 30 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 69.7% 53 / 76 Podpod 0.0% 0 / 46 totaltotal 53.6% 216 / 403 CC 29 SCAR 10.0
CC
29
Cov
58%
mapping
10.5
mapping
10.5
number
4.1
CRAP 2.7
« blib/lib/Text/Nunjucks/Context.pm ↑ Summary blib/lib/Text/Nunjucks/Loader.pm »
1 package Text::Nunjucks::Tests;
2 # ABSTRACT: 1:1 port of Nunjucks src/tests.js -- the built-in `is` tests
3  
4 15 our $VERSION = '0.001';
5  
6 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use builtin qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # tests.js is a bag of `exports.<name> = fn` test functions. The port keeps
15 # upstream's order and names (snake_case: nullTest -> null_test, undefinedTest
16 # -> undefined_test); each test is a class method dispatched through $self,
17 # like Filters, so a test named like a Perl builtin (defined, ne, lt) never
18 # resolves to the builtin. `exports` builds the { name => coderef } bag, aliases
19 # included, that Environment.init registers with add_test. A test is called the
20 # way the compiler calls it, env.getTest(name)(value, args...) with no `this`
21 # (ADR 0009), and its result is compared === true (op_seq), so every test
22 # returns a JS boolean.
23 #
24 # JS operators (===, %, <, !, ...) go through Runtime, never Perl's native ones
25 # (ADR 0001 kinds, ADR 0006 null, ADR 0007 numbers); value kinds come from Lib.
26 # The native calls upstream makes on a value it never checked -- lower's
27 # value.toLowerCase(), iterable's value[Symbol.iterator] -- go through Runtime
28 # js_method_call / js_has_symbol_iterator, which throw V8's TypeError (ADR
29 # 0010). A JS Map, Set or generator has no Perl image (k44).
30  
31 78 ▶sub _lib { 'Text::Nunjucks::Lib' }
Subroutine _lib: called CC=1 SCAR=0.0
32 626 ▶sub _runtime { 'Text::Nunjucks::Runtime' }
Subroutine _runtime: called CC=1 SCAR=0.0
33  
34 #### callable
35  
36 # Returns `true` if the object is a function, otherwise `false`.
37 15 ▶sub callable {
Subroutine callable: called CC=2 SCAR=9.2
Pod undocumented
38 0 ▶ my ( $self, $value ) = @_;
Subroutine callable: not called CC=2 SCAR=9.2
Pod undocumented
39 0 ▶ return $self->_lib->is_function($value) ? true : false; # typeof value === 'function'
Branch: 2 branches
BranchTrueFalseTotal
$self->_lib->is_function($value) ? : 8 7 15
$self->_lib->is_function($value) ? : 0 0 0
40 }
41  
42 #### defined
43  
44 # Returns `true` if the object is strictly not `undefined` (null is defined,
45 # ADR 0006).
46 71 ▶sub defined {
Subroutine defined: called CC=2 SCAR=9.2
Pod undocumented
47 0 ▶ my ( $self, $value ) = @_;
Subroutine defined: not called CC=2 SCAR=9.2
Pod undocumented
48 0 ▶ return defined($value) ? true : false; # value !== undefined
Branch: 2 branches
BranchTrueFalseTotal
defined $value ? : 60 11 71
defined $value ? : 0 0 0
49 }
50  
51 #### divisibleby
52  
53 # Returns `true` if the operand (one) is divisble by the test's argument (two).
54 76 ▶sub divisibleby {
Subroutine divisibleby: called CC=1 SCAR=1.2
Pod undocumented
55 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine divisibleby: not called CC=1 SCAR=1.2
Pod undocumented
56 0 my $rt = $self->_runtime;
57 0 return $rt->op_seq( $rt->op_mod( $one, $two ), 0 );
58 }
59  
60 #### escaped
61  
62 # Returns true if the string has been escaped (i.e., is a SafeString).
63 12 ▶sub escaped {
Subroutine escaped: called CC=2 SCAR=9.2
Pod undocumented
64 0 ▶ my ( $self, $value ) = @_;
Subroutine escaped: not called CC=2 SCAR=9.2
Pod undocumented
65 0 ▶ return $self->_runtime->_is_safe_string($value) ? true : false; # value instanceof SafeString
Branch: 2 branches
BranchTrueFalseTotal
$self->_runtime->_is_safe_string($value) ? : 5 7 12
$self->_runtime->_is_safe_string($value) ? : 0 0 0
66 }
67  
68 #### equalto
69  
70 # Returns `true` if the arguments are strictly equal.
71 45 ▶sub equalto {
Subroutine equalto: called CC=1 SCAR=1.2
Pod undocumented
72 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine equalto: not called CC=1 SCAR=1.2
Pod undocumented
73 0 return $self->_runtime->op_seq( $one, $two );
74 }
75  
76 #### even
77  
78 # Returns `true` if the value is evenly divisible by 2.
79 55 ▶sub even {
Subroutine even: called CC=1 SCAR=1.2
Pod undocumented
80 0 ▶ my ( $self, $value ) = @_;
Subroutine even: not called CC=1 SCAR=1.2
Pod undocumented
81 0 my $rt = $self->_runtime;
82 0 return $rt->op_seq( $rt->op_mod( $value, 2 ), 0 );
83 }
84  
85 #### falsy
86  
87 # Returns `true` if the value is falsy - '', 0, false, undefined, NaN or null
88 # (JS falsiness: '0', [] and {} are truthy).
89 25 ▶sub falsy {
Subroutine falsy: called CC=1 SCAR=1.2
Pod undocumented
90 0 ▶ my ( $self, $value ) = @_;
Subroutine falsy: not called CC=1 SCAR=1.2
Pod undocumented
91 0 return $self->_runtime->op_not($value); # !value
92 }
93  
94 #### ge
95  
96 # Returns `true` if the operand (one) is greater or equal to the test's
97 # argument (two).
98 8 ▶sub ge {
Subroutine ge: called CC=1 SCAR=1.2
Pod undocumented
99 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine ge: not called CC=1 SCAR=1.2
Pod undocumented
100 0 return $self->_runtime->op_ge( $one, $two );
101 }
102  
103 #### greaterthan
104  
105 # Returns `true` if the operand (one) is greater than the test's argument (two).
106 21 ▶sub greaterthan {
Subroutine greaterthan: called CC=1 SCAR=1.2
Pod undocumented
107 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine greaterthan: not called CC=1 SCAR=1.2
Pod undocumented
108 0 return $self->_runtime->op_gt( $one, $two );
109 }
110  
111 #### le
112  
113 # Returns `true` if the operand (one) is less than or equal to the test's
114 # argument (two).
115 8 ▶sub le {
Subroutine le: called CC=1 SCAR=1.2
Pod undocumented
116 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine le: not called CC=1 SCAR=1.2
Pod undocumented
117 0 return $self->_runtime->op_le( $one, $two );
118 }
119  
120 #### lessthan
121  
122 # Returns `true` if the operand (one) is less than the test's passed argument
123 # (two).
124 17 ▶sub lessthan {
Subroutine lessthan: called CC=1 SCAR=1.2
Pod undocumented
125 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine lessthan: not called CC=1 SCAR=1.2
Pod undocumented
126 0 return $self->_runtime->op_lt( $one, $two );
127 }
128  
129 #### lower
130  
131 # Returns `true` if the string is lowercased. value.toLowerCase() on a value
132 # without it throws V8's TypeError (oracle: {{ 5 is lower }} -> "TypeError:
133 # value.toLowerCase is not a function", {{ null is lower }} -> "TypeError:
134 # Cannot read properties of null (reading 'toLowerCase')"); a SafeString's
135 # result is a primitive, never === the SafeString.
136 31 ▶sub lower {
Subroutine lower: called CC=1 SCAR=1.2
Pod undocumented
137 0 ▶ my ( $self, $value ) = @_;
Subroutine lower: not called CC=1 SCAR=1.2
Pod undocumented
138 0 my $rt = $self->_runtime;
139 0 return $rt->op_seq( $rt->js_method_call( $value, 'value', 'toLowerCase' ), $value );
140 }
141  
142 #### ne
143  
144 # Returns `true` if the operand (one) is not strictly equal to the test's
145 # argument (two).
146 7 ▶sub ne {
Subroutine ne: called CC=1 SCAR=1.2
Pod undocumented
147 0 ▶ my ( $self, $one, $two ) = @_;
Subroutine ne: not called CC=1 SCAR=1.2
Pod undocumented
148 0 return $self->_runtime->op_sne( $one, $two );
149 }
150  
151 #### nullTest
152  
153 # Returns true if the value is strictly equal to `null` (the None sentinel,
154 # ADR 0006).
155 25 ▶sub null_test {
Subroutine null_test: called CC=1 SCAR=1.2
Pod undocumented
156 0 ▶ my ( $self, $value ) = @_;
Subroutine null_test: not called CC=1 SCAR=1.2
Pod undocumented
157 0 my $rt = $self->_runtime;
158 0 return $rt->op_seq( $value, $rt->None );
159 }
160  
161 #### number
162  
163 # Returns true if value is a number (ADR 0001: created as a number; NaN and
164 # Infinity are numbers, a numeric string is not).
165 31 ▶sub number {
Subroutine number: called CC=3 SCAR=14.2
Pod undocumented
166 0 ▶ my ( $self, $value ) = @_;
Subroutine number: not called CC=3 SCAR=14.2
Pod undocumented
167 0 ▶ return ( defined($value) && builtin::created_as_number($value) ) ? true : false; # typeof value === 'number'
Branch: 2 branches
BranchTrueFalseTotal
defined $value && &builtin::created_as_number($value) ? : 13 18 31
defined $value && &builtin::created_as_number($value) ? : 0 0 0
Condition50%3/6
defined $value and &builtin::created_as_number($value)
!ll&&!rl&&r
21613
defined $value and &builtin::created_as_number($value)
!ll&&!rl&&r
000
MC/DC100%2/2
defined $value and &builtin::created_as_number($value)
defined $value &builtin::created_as_number($value)
MC/DC0%0/2
defined $value and &builtin::created_as_number($value)
defined $value &builtin::created_as_number($value)
Truth table100%3/3
A and B
A= defined $valueB= &builtin::created_as_number($value)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $valueB= &builtin::created_as_number($value)
ABresult
0X0
100
111
168 }
169  
170 #### odd
171  
172 # Returns `true` if the value is *not* evenly divisible by 2.
173 107 ▶sub odd {
Subroutine odd: called CC=1 SCAR=1.2
Pod undocumented
174 0 ▶ my ( $self, $value ) = @_;
Subroutine odd: not called CC=1 SCAR=1.2
Pod undocumented
175 0 my $rt = $self->_runtime;
176 0 return $rt->op_seq( $rt->op_mod( $value, 2 ), 1 );
177 }
178  
179 #### string
180  
181 # Returns `true` if the value is a string, `false` if not (ADR 0001; a
182 # SafeString is an object).
183 23 ▶sub string {
Subroutine string: called CC=2 SCAR=9.2
Pod undocumented
184 0 ▶ my ( $self, $value ) = @_;
Subroutine string: not called CC=2 SCAR=9.2
Pod undocumented
185 0 ▶ return $self->_lib->is_string($value) ? true : false; # typeof value === 'string'
Branch: 2 branches
BranchTrueFalseTotal
$self->_lib->is_string($value) ? : 7 16 23
$self->_lib->is_string($value) ? : 0 0 0
186 }
187  
188 #### truthy
189  
190 # Returns `true` if the value is not in the list of things considered falsy:
191 # '', null, undefined, 0, NaN and false.
192 96 ▶sub truthy {
Subroutine truthy: called CC=1 SCAR=1.2
Pod undocumented
193 0 ▶ my ( $self, $value ) = @_;
Subroutine truthy: not called CC=1 SCAR=1.2
Pod undocumented
194 0 my $rt = $self->_runtime;
195 0 return $rt->op_not( $rt->op_not($value) ); # !!value
196 }
197  
198 #### undefinedTest
199  
200 # Returns `true` if the value is undefined.
201 21 ▶sub undefined_test {
Subroutine undefined_test: called CC=2 SCAR=9.2
Pod undocumented
202 0 ▶ my ( $self, $value ) = @_;
Subroutine undefined_test: not called CC=2 SCAR=9.2
Pod undocumented
203 0 ▶ return !defined($value) ? true : false; # value === undefined
Branch: 2 branches
BranchTrueFalseTotal
!defined($value) ? : 8 13 21
!defined($value) ? : 0 0 0
204 }
205  
206 #### upper
207  
208 # Returns `true` if the string is uppercased; value.toUpperCase() throws as
209 # lower's value.toLowerCase() does.
210 35 ▶sub upper {
Subroutine upper: called CC=1 SCAR=1.2
Pod undocumented
211 0 ▶ my ( $self, $value ) = @_;
Subroutine upper: not called CC=1 SCAR=1.2
Pod undocumented
212 0 my $rt = $self->_runtime;
213 0 return $rt->op_seq( $rt->js_method_call( $value, 'value', 'toUpperCase' ), $value );
214 }
215  
216 #### iterable
217  
218 # Returns `true` if the value implements the `Symbol.iterator` method. Upstream
219 # falls back to Array.isArray(value) || typeof value === 'string' where the
220 # engine has no Symbol; every engine the port mirrors has it, so only the first
221 # branch is ported. undefined and null throw (oracle: {{ x is iterable }} ->
222 # "TypeError: undefined is not iterable (cannot read property
223 # Symbol(Symbol.iterator))"); a SafeString is iterable (String.prototype).
224 27 ▶sub iterable {
Subroutine iterable: called CC=2 SCAR=9.2
Pod undocumented
225 0 ▶ my ( $self, $value ) = @_;
Subroutine iterable: not called CC=2 SCAR=9.2
Pod undocumented
226 0 ▶ return $self->_runtime->js_has_symbol_iterator($value) ? true : false; # !!value[Symbol.iterator]
Branch: 2 branches
BranchTrueFalseTotal
$self->_runtime->js_has_symbol_iterator($value) ? : 12 11 23
$self->_runtime->js_has_symbol_iterator($value) ? : 0 0 0
227 }
228  
229 #### mapping
230  
231 # Returns `true` if the value is an object hash or an ES6 Map: typeof value ===
232 # 'object', so a regex, a SafeString, an engine-built object and any blessed
233 # object count; an array and a function do not. typeof 'object' is a reference
234 # that is not a coderef once undefined and null are out (ADR 0001: every JS
235 # primitive is a non-reference scalar, null the None sentinel of ADR 0006).
236 # The `!(value instanceof Set)` half has nothing to exclude: Perl has no Set
237 # (k44).
238 32 ▶sub mapping {
Subroutine mapping: called CC=6 SCAR=23.5
Pod undocumented
239 0 ▶ my ( $self, $value ) = @_;
Subroutine mapping: not called CC=6 SCAR=23.5
Pod undocumented
240 # only maps and object hashes
241 0 ▶ my $bool = defined($value) # value !== undefined
Condition50%12/24
defined $value && !$self->_runtime->_is_none($value) && ref $value && !$self->_lib->is_function($value) && !$self->_lib->is_array($value)
!ll&&!rl&&r
13613
defined $value and not $self->_runtime->_is_none($value) and ref $value and not $self->_lib->is_function($value)
!ll&&!rl&&r
11219
defined $value and not $self->_runtime->_is_none($value) and ref $value
!ll&&!rl&&r
4721
defined $value and not $self->_runtime->_is_none($value)
!ll&&!rl&&r
1328
defined $value && !$self->_runtime->_is_none($value) && ref $value && !$self->_lib->is_function($value) && !$self->_lib->is_array($value)
!ll&&!rl&&r
000
defined $value and not $self->_runtime->_is_none($value) and ref $value and not $self->_lib->is_function($value)
!ll&&!rl&&r
000
defined $value and not $self->_runtime->_is_none($value) and ref $value
!ll&&!rl&&r
000
defined $value and not $self->_runtime->_is_none($value)
!ll&&!rl&&r
000
MC/DC100%5/5
defined $value && !$self->_runtime->_is_none($value) && ref $value && !$self->_lib->is_function($value) && !$self->_lib->is_array($value)
defined $value not $self->_runtime->_is_none($value) ref $value not $self->_lib->is_function($value) !$self->_lib->is_array($value)
MC/DC0%0/5
defined $value && !$self->_runtime->_is_none($value) && ref $value && !$self->_lib->is_function($value) && !$self->_lib->is_array($value)
defined $value not $self->_runtime->_is_none($value) ref $value not $self->_lib->is_function($value) !$self->_lib->is_array($value)
Truth table100%6/6
A and not(B) and C and not(D) && not(E)
A= defined $valueB= not $self->_runtime->_is_none($value)C= ref $valueD= not $self->_lib->is_function($value)E= !$self->_lib->is_array($value)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
Truth table0%0/6
A and not(B) and C and not(D) && not(E)
A= defined $valueB= not $self->_runtime->_is_none($value)C= ref $valueD= not $self->_lib->is_function($value)E= !$self->_lib->is_array($value)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
242 && !$self->_runtime->_is_none($value) # value !== null
243 && ref($value) && !$self->_lib->is_function($value) # typeof value === 'object'
244 && !$self->_lib->is_array($value); # !Array.isArray(value)
245 0 ▶ return $bool ? true : false;
Branch: 2 branches
BranchTrueFalseTotal
$bool ? : 13 19 32
$bool ? : 0 0 0
246 }
247  
248 #### exports -- { name => coderef }, in tests.js's order, aliases included
249  
250 # tests.js's functions are module-level: every Environment registers the same
251 # ones (environment.js: lib._entries(tests).forEach(... this.addTest ...)). So
252 # the coderefs are built once per class and kept here; each call hands out a
253 # fresh hash of them, which Environment.init only reads.
254 15 my %exports_of;
255  
256 5122 ▶sub exports {
Subroutine exports: called CC=2 SCAR=7.8
Pod undocumented
257 0 ▶ my ($self) = @_;
Subroutine exports: not called CC=2 SCAR=7.8
Pod undocumented
258 0 ▶ return { %{ $exports_of{$self} } } if $exports_of{$self};
Branch: 2 branches
BranchTrueFalseTotal
if $exports_of{$self} 5108 14 5122
if $exports_of{$self} 0 0 0
259 my $exports = $exports_of{$self} = {
260 15 ▶ callable => sub { $self->callable(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 71 ▶ defined => sub { $self->defined(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
262 76 ▶ divisibleby => sub { $self->divisibleby(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
263 12 ▶ escaped => sub { $self->escaped(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
264 45 ▶ equalto => sub { $self->equalto(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
265 0 };
266  
267 # Aliases
268 0 $exports->{eq} = $exports->{equalto};
269 0 $exports->{sameas} = $exports->{equalto};
270  
271 0 ▶ $exports->{even} = sub { $self->even(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
272 0 ▶ $exports->{falsy} = sub { $self->falsy(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
273 0 ▶ $exports->{ge} = sub { $self->ge(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
274 0 ▶ $exports->{greaterthan} = sub { $self->greaterthan(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
275  
276 # alias
277 0 $exports->{gt} = $exports->{greaterthan};
278  
279 0 ▶ $exports->{le} = sub { $self->le(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
280 0 ▶ $exports->{lessthan} = sub { $self->lessthan(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
281  
282 # alias
283 0 $exports->{lt} = $exports->{lessthan};
284  
285 0 ▶ $exports->{lower} = sub { $self->lower(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
286 0 ▶ $exports->{ne} = sub { $self->ne(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
287 0 ▶ $exports->{null} = sub { $self->null_test(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
288 0 ▶ $exports->{number} = sub { $self->number(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
289 0 ▶ $exports->{odd} = sub { $self->odd(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
290 0 ▶ $exports->{string} = sub { $self->string(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
291 0 ▶ $exports->{truthy} = sub { $self->truthy(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
292 0 ▶ $exports->{undefined} = sub { $self->undefined_test(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
293 0 ▶ $exports->{upper} = sub { $self->upper(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
294 0 ▶ $exports->{iterable} = sub { $self->iterable(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
295 0 ▶ $exports->{mapping} = sub { $self->mapping(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
296  
297 0 return {%$exports};
298 }
299  
300 15 1;
301  
302 __END__
« blib/lib/Text/Nunjucks/Context.pm ↑ Summary blib/lib/Text/Nunjucks/Loader.pm »