uglifier_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # encoding: UTF-8
  2. require 'stringio'
  3. require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  4. describe "Uglifier" do
  5. it "minifies JS" do
  6. source = File.open("lib/uglify.js", "r:UTF-8").read
  7. minified = Uglifier.new.compile(source)
  8. minified.length.should < source.length
  9. lambda {
  10. Uglifier.new.compile(minified)
  11. }.should_not raise_error
  12. end
  13. it "throws an exception when compilation fails" do
  14. lambda {
  15. Uglifier.new.compile(")(")
  16. }.should raise_error(Uglifier::Error)
  17. end
  18. it "doesn't omit null character in strings" do
  19. Uglifier.new.compile('var foo="\0bar"').should match(/(\0|\\0)/)
  20. end
  21. it "doesn't try to mangle $super by default to avoid breaking PrototypeJS" do
  22. Uglifier.new.compile('function foo($super) {return $super}').should include("$super")
  23. end
  24. it "adds trailing semicolon to minified source" do
  25. source = "function id(i) {return i;};"
  26. Uglifier.new.compile(source)[-1].should eql(";"[0])
  27. end
  28. describe "Copyright Preservation" do
  29. before :all do
  30. @source = <<-EOS
  31. /* Copyright Notice */
  32. /* (c) 2011 */
  33. // INCLUDED
  34. function identity(p) { return p; }
  35. EOS
  36. @minified = Uglifier.compile(@source, :copyright => true)
  37. end
  38. it "preserves copyright notice" do
  39. @minified.should match /Copyright Notice/
  40. end
  41. it "handles multiple copyright blocks" do
  42. @minified.should match /\(c\) 2011/
  43. end
  44. it "does include different comment types" do
  45. @minified.should match /INCLUDED/
  46. end
  47. it "puts comments on own lines" do
  48. @minified.split("\n").should have(4).items
  49. end
  50. it "omits copyright notification if copyright parameter is set to false" do
  51. Uglifier.compile(@source, :copyright => false).should_not match /Copyright/
  52. end
  53. end
  54. it "does additional squeezing when unsafe options is true" do
  55. unsafe_input = "function a(b){b.toString();}"
  56. Uglifier.new(:unsafe => true).compile(unsafe_input).length.should < Uglifier.new(:unsafe => false).compile(unsafe_input).length
  57. end
  58. it "mangles variables only if mangle is set to true" do
  59. code = "function longFunctionName(){};"
  60. Uglifier.new(:mangle => false).compile(code).length.should == code.length
  61. end
  62. it "squeezes code only if squeeze is set to true" do
  63. code = "function a(a){if(a) { return 0; } else { return 1; }}"
  64. Uglifier.compile(code, :squeeze => false).length.should > Uglifier.compile(code, :squeeze => true).length
  65. end
  66. it "should allow top level variables to be mangled" do
  67. code = "var foo = 123"
  68. Uglifier.compile(code, :toplevel => true).should_not include("var foo")
  69. end
  70. it "allows variables to be excluded from mangling" do
  71. code = "var foo = {bar: 123};"
  72. Uglifier.compile(code, :except => ["foo"], :toplevel => true).should include("var foo")
  73. end
  74. it "allows disabling of function name mangling" do
  75. code = "function sample() {var bar = 1; function foo() { return bar; }}"
  76. mangled = Uglifier.compile(code, :mangle => :vars)
  77. mangled.should include("foo()")
  78. mangled.should_not include("bar")
  79. end
  80. it "honors max line length" do
  81. code = "var foo = 123;var bar = 123456"
  82. Uglifier.compile(code, :max_line_length => 8).split("\n").length.should == 2
  83. end
  84. it "lifts vars to top of the scope" do
  85. code = "function something() { var foo = 123; foo = 1234; var bar = 123456; return foo + bar}"
  86. Uglifier.compile(code, :lift_vars => true).should match /var \w,\w/
  87. end
  88. it "can be configured to output only ASCII" do
  89. code = "function emoji() { return '\\ud83c\\ude01'; }"
  90. Uglifier.compile(code, :ascii_only => true).should include("\\ud83c\\ude01")
  91. end
  92. it "escapes </script when asked to" do
  93. code = "function test() { return '</script>';}"
  94. Uglifier.compile(code, :inline_script => true).should_not include("</script>")
  95. end
  96. it "quotes keys" do
  97. code = "var a = {foo: 1}"
  98. Uglifier.compile(code, :quote_keys => true).should include('"foo"')
  99. end
  100. describe "Input Formats" do
  101. let(:code) { "function hello() { return 'hello world'; }" }
  102. it "handles strings" do
  103. lambda {
  104. Uglifier.new.compile(code).should_not be_empty
  105. }.should_not raise_error
  106. end
  107. it "handles IO objects" do
  108. lambda {
  109. Uglifier.new.compile(StringIO.new(code)).should_not be_empty
  110. }.should_not raise_error
  111. end
  112. end
  113. end