File: 08-ternary-operators.t

package info (click to toggle)
libjavascript-packer-perl 2.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,280 kB
  • sloc: javascript: 16,839; perl: 868; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 2,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 5;
use File::Spec;
use lib File::Spec->catdir(qw(lib));

use JavaScript::Packer;

my $packer = JavaScript::Packer->init();

my $input = qq~
// Test ternary operator with + and - literals
options.position = {
  my: "left top",
  at: "left" + ( left >= 0 ? "+" : "" ) + left + " " +
       "top" + ( top >= 0 ? "+" : "" ) + top,
  of: that.window
};

// Another variation
o.position = {
  my: "left top",
  at: "left" + ( 0 <= s ? "+" : "" ) + s + " top" + ( 0 <= i ? "+" : "" ) + i,
  of: n.window
};
~;

my $expected_clean = 'options.position={my:"left top",at:"left"+(left>=0?"+":"")+left+" top"+(top>=0?"+":"")+top,of:that.window};o.position={my:"left top",at:"left"+(0<=s?"+":"")+s+" top"+(0<=i?"+":"")+i,of:n.window};';

$packer->minify( \$input, { compress => 'clean' } );
is($input, $expected_clean, 'Ternary operator with "+" literal is preserved');

# Try with nested conditional to handle more complex cases
my $nested_input = qq~
// Test nested ternary with + and - literals
options.format = ( value > 0 ? "+" : value < 0 ? "-" : "" ) + Math.abs(value);
~;

my $expected_nested = 'options.format=(value>0?"+":value<0?"-":"")+Math.abs(value);';

$packer->minify( \$nested_input, { compress => 'clean' } );
is($nested_input, $expected_nested, 'Nested ternary with "+"/"-" literals is preserved');

# Test with single quotes
my $single_quotes = qq~
// Test ternary with single quotes
options.prefix = ( value >= 0 ? '+' : '-' ) + Math.abs(value);
~;

my $expected_single = 'options.prefix=(value>=0?\'+\':\'-\')+Math.abs(value);';

$packer->minify( \$single_quotes, { compress => 'clean' } );
is($single_quotes, $expected_single, 'Ternary with single-quoted "+"/"-" literals is preserved');

# Test with more complex expressions
my $complex = qq~
// Test more complex expressions
var result = x > 10 ? "+" + (y * 2) : "-" + Math.min(5, z);
~;

my $expected_complex = 'var result=x>10?"+"+(y*2):"-"+Math.min(5,z);';

$packer->minify( \$complex, { compress => 'clean' } );
is($complex, $expected_complex, 'Complex expressions with "+"/"-" literals are preserved');

# Test with template literals (backticks)
my $template = qq~
// Test with template literals
var sign = value > 0 ? \`+\` : \`-\`;
~;

my $expected_template = 'var sign=value>0?`+`:`-`;';

$packer->minify( \$template, { compress => 'clean' } );
is($template, $expected_template, 'Ternary with template literals is preserved');