| 1 | #!perl -T |
| 2 | ######################### |
| 3 | |
| 4 | use Test::More tests => 19; |
| 5 | use JavaScript::Minifier qw(minify); |
| 6 | |
| 7 | ### This is mainly to align the tests to 's' scripts, because s1 is |
| 8 | ### somehow missing |
| 9 | can_ok('JavaScript::Minifier', 'minify'); |
| 10 | |
| 11 | ######################### |
| 12 | |
| 13 | sub filesMatch { |
| 14 | my $file1 = shift; |
| 15 | my $file2 = shift; |
| 16 | my $a; |
| 17 | my $b; |
| 18 | |
| 19 | while (1) { |
| 20 | $a = getc($file1); |
| 21 | $b = getc($file2); |
| 22 | |
| 23 | if (!defined($a) && !defined($b)) { # both files end at same place |
| 24 | return 1; |
| 25 | } |
| 26 | elsif (!defined($b) || # file2 ends first |
| 27 | !defined($a) || # file1 ends first |
| 28 | $a ne $b) { # a and b not the same |
| 29 | return 0; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | sub minTest { |
| 35 | my $filename = shift; |
| 36 | |
| 37 | open(INFILE, 't/scripts/' . $filename . '.js') or die("couldn't open file"); |
| 38 | open(GOTFILE, '>t/scripts/' . $filename . '-got.js') or die("couldn't open file"); |
| 39 | minify(input => *INFILE, outfile => *GOTFILE); |
| 40 | close(INFILE); |
| 41 | close(GOTFILE); |
| 42 | |
| 43 | open(EXPECTEDFILE, 't/scripts/' . $filename . '-expected.js') or die("couldn't open file"); |
| 44 | open(GOTFILE, 't/scripts/' . $filename . '-got.js') or die("couldn't open file"); |
| 45 | ok(filesMatch(GOTFILE, EXPECTEDFILE), 'testing ' . $filename); |
| 46 | close(EXPECTEDFILE); |
| 47 | close(GOTFILE); |
| 48 | } |
| 49 | |
| 50 | minTest('s2'); # missing semi-colons |
| 51 | minTest('s3'); # //@ |
| 52 | minTest('s4'); # /*@*/ |
| 53 | minTest('s5'); # // |
| 54 | minTest('s6'); # /**/ |
| 55 | minTest('s7'); # blocks of comments |
| 56 | minTest('s8'); # + + - - |
| 57 | minTest('s9'); # alphanum |
| 58 | minTest('s10'); # }]) |
| 59 | minTest('s11'); # string and regexp literals |
| 60 | minTest('s12'); # other characters |
| 61 | minTest('s13'); # comment at start |
| 62 | minTest('s14'); # slash following square bracket |
| 63 | # ... is division not RegExp |
| 64 | minTest('s15'); # newline-at-end-of-file |
| 65 | # -> not there so don't add |
| 66 | minTest('s16'); # newline-at-end-of-file |
| 67 | # -> it's there so leave it alone |
| 68 | |
| 69 | is(minify(input => 'var x = 2;'), 'var x=2;', 'string literal input and ouput'); |
| 70 | is(minify(input => "var x = 2;\n;;;alert('hi');\nvar x = 2;", stripDebug => 1), 'var x=2;var x=2;', 'scriptDebug option'); |
| 71 | is(minify(input => 'var x = 2;', copyright => "BSD"), '/* BSD */var x=2;', 'copyright option'); |