-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathclosure_compiler_test.rb
More file actions
99 lines (82 loc) · 3.35 KB
/
closure_compiler_test.rb
File metadata and controls
99 lines (82 loc) · 3.35 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'test_helper'
class ClosureCompilerTest < Minitest::Test
ORIGINAL = <<-EOS
window.hello = function(name) { return console.log('hello ' + name ); };
window.hello.squared = function(num) { return num * num; };
window.hello('world');
EOS
COMPILED_WHITESPACE = <<-EOS.strip
window.hello=function(name){return console.log("hello "+name)};window.hello.squared=function(num){return num*num};window.hello("world");
EOS
COMPILED_SIMPLE = <<-EOS.strip
window.hello=function(a){return console.log("hello "+a)};window.hello.squared=function(a){return a*a};window.hello("world");
EOS
COMPILED_ADVANCED = <<-EOS.strip
window.g=function(){console.log(\"hello world\")};window.g.h=function(a){return a*a};window.g();
EOS
def test_whitespace_compression
js = Compiler.new(:compilation_level => "WHITESPACE_ONLY").compile(ORIGINAL).strip
assert_equal COMPILED_WHITESPACE, js
end
def test_simple_compression
js = Compiler.new.compile(ORIGINAL).strip
assert_equal COMPILED_SIMPLE, js
end
def test_advanced_compression
js = Compiler.new(:compilation_level => "ADVANCED_OPTIMIZATIONS").compile(ORIGINAL).strip
assert_equal COMPILED_ADVANCED, js
end
def test_block_syntax
result = ''
Compiler.new(:compilation_level => "ADVANCED_OPTIMIZATIONS").compile(ORIGINAL) do |code|
while buffer = code.read(3)
result << buffer
end
end
assert_equal COMPILED_ADVANCED, result.strip
end
def test_jar_and_java_specifiation
jar = Dir['vendor/closure-compiler-*.jar'].first
unless java = ( `which java` rescue nil )
java = `where java` rescue nil # works on newer windows
end
if java
compiler = Compiler.new(:java => java.strip, :jar_file => jar)
assert_equal COMPILED_SIMPLE, compiler.compress(ORIGINAL).strip
else
puts "could not `which/where java` skipping test"
end
end
def test_exceptions
assert_raises(Closure::Error) do
Compiler.new.compile('1++')
end
assert_raises(Closure::Error) do
Compiler.new.compile('obj = [1 2, 3]')
end
end
def test_stderr_reading
js = Compiler.new.compile(File.read('test/fixtures/precompressed.js'))
assert js == File.read('test/fixtures/precompressed-compiled.js')
end
def test_permissions
assert File.executable?(COMPILER_JAR)
end
def test_serialize_options
options = { 'externs' => 'library1.js', "compilation_level" => "ADVANCED_OPTIMIZATIONS" }
# ["--externs", "library1.js", "--compilation_level", "ADVANCED_OPTIMIZATIONS"]
# although Hash in 1.8 might change the order to :
# ["--compilation_level", "ADVANCED_OPTIMIZATIONS", "--externs", "library1.js"]
expected_options = options.to_a.map { |arr| [ "--#{arr[0]}", arr[1] ] }.flatten
assert_equal expected_options, Closure::Compiler.new.send(:serialize_options, options)
end
def test_serialize_options_for_arrays
compiler = Closure::Compiler.new('externs' => ['library1.js', "library2.js"])
assert_equal ["--externs", "library1.js", "--externs", "library2.js"], compiler.send(:serialize_options, 'externs' => ['library1.js', "library2.js"])
end
def test_compiling_array_of_file_paths
files = ['test/fixtures/file1.js', 'test/fixtures/file2.js']
result = Closure::Compiler.new().compile_files(files)
assert_equal File.read('test/fixtures/file1-file2-compiled.js'), result
end
end