Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Jul 29, 2010
1 parent 6374019 commit b02b00e
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Rakefile
@@ -0,0 +1,9 @@
require 'rake/testtask'

task :default => :test

Rake::TestTask.new do |t|
t.libs += ['lib', 'test']
t.test_files = FileList['test/*_test.rb']
t.verbose = true
end
58 changes: 58 additions & 0 deletions test/css_test.rb
@@ -0,0 +1,58 @@
require File.expand_path('../helper', __FILE__)

class CSSTest < Test::Unit::TestCase
FIXTURE_CSS = <<'CODE'
.a-class {
background-color: red;
background-position: 0;
}
div#an-id { color: #FFFFFF; }
CODE

def test_default_arguments
if jruby?
args = command_arguments(default_css_options)
assert_equal([-1], args)
else
args = command_arguments(default_css_options)
assert_equal(%w< --charset utf-8 >, args)
end
end

def test_arguments
if jruby?
args = command_arguments(:type => 'css')
assert_equal([-1], args)

args = command_arguments(:type => 'css', :line_break => 80)
assert_equal([80], args)

args = command_arguments(:type => 'css', :non_existent => true)
assert_equal([-1], args)
else
args = command_arguments(:type => 'css')
assert_equal(%w< --type css >, args)

args = command_arguments(:type => 'css', :line_break => 80)
assert_equal(%w< --type css --line-break 80 >, args)

args = command_arguments(:type => 'css', :non_existent => true)
assert_equal(%w< --type css >, args)
end
end

def test_default_compress
assert_equal (<<'CODE').chomp, compress_css(FIXTURE_CSS)
.a-class{background-color:red;background-position:0 0;}div#an-id{color:#FFF;}
CODE
end

def test_line_break_option_should_insert_line_breaks
options = { :line_break => 0 }
assert_equal (<<'CODE').chomp, compress_css(FIXTURE_CSS, options)
.a-class{background-color:red;background-position:0 0;}
div#an-id{color:#FFF;}
CODE
end
end
9 changes: 9 additions & 0 deletions test/helper.rb
@@ -0,0 +1,9 @@
lib = File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'test/unit'
require 'yuicompressor'

class Test::Unit::TestCase
include YUICompressor
end
96 changes: 96 additions & 0 deletions test/js_test.rb
@@ -0,0 +1,96 @@
require File.expand_path('../helper', __FILE__)

class JSTest < Test::Unit::TestCase
FIXTURE_JS = <<'CODE'
// here's a comment
var Foo = { "a": 1 };
Foo["bar"] = (function(baz) {
/* here's a
multiline comment */
if (false) {
doSomething();
} else {
for (var index = 0; index < baz.length; index++) {
doSomething(baz[index]);
}
}
})("hello");
CODE

def test_default_arguments
if jruby?
args = command_arguments(default_js_options)
assert_equal([-1], args)
else
args = command_arguments(default_js_options)
assert_equal(%w< --charset utf-8 >, args)
end
end

def test_arguments
if jruby?
args = command_arguments(:type => 'js')
assert_equal([-1, false, false, false, true], args)

args = command_arguments(:type => 'js', :optimize => true)
assert_equal([-1, false, false, false, false], args)

args = command_arguments(:type => 'js', :munge => true)
assert_equal([-1, true, false, false, true], args)

args = command_arguments(:type => 'js', :non_existent => true)
assert_equal([-1, false, false, false, true], args)
else
args = command_arguments(:type => 'js')
assert_equal(%w< --type js --nomunge --disable-optimizations >, args)

args = command_arguments(:type => 'js', :optimize => true)
assert_equal(%w< --type js --nomunge >, args)

args = command_arguments(:type => 'js', :munge => true)
assert_equal(%w< --type js --disable-optimizations >, args)

args = command_arguments(:type => 'js', :non_existent => true)
assert_equal(%w< --type js --nomunge --disable-optimizations >, args)
end
end

def test_default_compress
assert_equal (<<'CODE').chomp, compress_js(FIXTURE_JS)
var Foo={a:1};Foo.bar=(function(baz){if(false){doSomething()}else{for(var index=0;index<baz.length;index++){doSomething(baz[index])}}})("hello");
CODE
end

def test_line_break_option_should_insert_line_breaks
options = { :line_break => 0 }
assert_equal (<<'CODE').chomp, compress_js(FIXTURE_JS, options)
var Foo={a:1};
Foo.bar=(function(baz){if(false){doSomething()
}else{for(var index=0;
index<baz.length;
index++){doSomething(baz[index])
}}})("hello");
CODE
end

def test_munge_option_should_munge_local_variable_names
options = { :munge => true }
assert_equal (<<'CODE').chomp, compress_js(FIXTURE_JS, options)
var Foo={a:1};Foo.bar=(function(b){if(false){doSomething()}else{for(var a=0;a<b.length;a++){doSomething(b[a])}}})("hello");
CODE
end

def test_optimize_option_should_not_modify_property_accesses_or_object_literal_keys_when_false
options = { :optimize => false }
assert_equal (<<'CODE').chomp, compress_js(FIXTURE_JS, options)
var Foo={"a":1};Foo["bar"]=(function(baz){if(false){doSomething()}else{for(var index=0;index<baz.length;index++){doSomething(baz[index])}}})("hello");
CODE
end

def test_preserve_semicolons_option_should_preserve_semicolons
options = { :preserve_semicolons => true }
assert_equal (<<'CODE').chomp, compress_js(FIXTURE_JS, options)
var Foo={a:1};Foo.bar=(function(baz){if(false){doSomething();}else{for(var index=0;index<baz.length;index++){doSomething(baz[index]);}}})("hello");
CODE
end
end

0 comments on commit b02b00e

Please sign in to comment.