File: JavaScript-Minifier.t

package info (click to toggle)
libjavascript-minifier-perl 1.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 404 kB
  • sloc: perl: 477; javascript: 142; makefile: 7
file content (71 lines) | stat: -rw-r--r-- 2,195 bytes parent folder | download | duplicates (3)
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
#!perl -T
#########################

use Test::More tests => 19;
use JavaScript::Minifier qw(minify);

### This is mainly to align the tests to 's' scripts, because s1 is
### somehow missing
can_ok('JavaScript::Minifier', 'minify');

#########################

sub filesMatch {
  my $file1 = shift;
  my $file2 = shift;
  my $a;
  my $b;

  while (1) {
    $a = getc($file1);
    $b = getc($file2);

    if (!defined($a) && !defined($b)) { # both files end at same place
      return 1;
    }
    elsif (!defined($b) || # file2 ends first
           !defined($a) || # file1 ends first
           $a ne $b) {     # a and b not the same
      return 0;
    }
  }
}

sub minTest {
  my $filename = shift;

  open(INFILE, 't/scripts/' . $filename . '.js') or die("couldn't open file");
  open(GOTFILE, '>t/scripts/' . $filename . '-got.js') or die("couldn't open file");
    minify(input => *INFILE, outfile => *GOTFILE);
  close(INFILE);
  close(GOTFILE);

  open(EXPECTEDFILE, 't/scripts/' . $filename . '-expected.js') or die("couldn't open file");
  open(GOTFILE, 't/scripts/' . $filename . '-got.js') or die("couldn't open file");
    ok(filesMatch(GOTFILE, EXPECTEDFILE), 'testing ' . $filename);
  close(EXPECTEDFILE);
  close(GOTFILE);
}

minTest('s2');    # missing semi-colons
minTest('s3');    # //@
minTest('s4');    # /*@*/
minTest('s5');    # //
minTest('s6');    # /**/
minTest('s7');    # blocks of comments
minTest('s8');    # + + - -
minTest('s9');    # alphanum
minTest('s10');  # }])
minTest('s11');  # string and regexp literals
minTest('s12');  # other characters
minTest('s13');  # comment at start
minTest('s14');  # slash following square bracket
                 # ... is division not RegExp
minTest('s15');  # newline-at-end-of-file
                 # -> not there so don't add
minTest('s16');  # newline-at-end-of-file
                 # -> it's there so leave it alone

is(minify(input => 'var x = 2;'), 'var x=2;', 'string literal input and ouput');
is(minify(input => "var x = 2;\n;;;alert('hi');\nvar x = 2;", stripDebug => 1), 'var x=2;var x=2;', 'scriptDebug option');
is(minify(input => 'var x = 2;', copyright => "BSD"), '/* BSD */var x=2;', 'copyright option');