Added unit tests for YUI and did some cleanup/restructuring of the test fixtures as well as a way to validate generated css.

This commit is contained in:
Chris Eppstein 2008-12-04 12:35:00 -08:00
parent 31370e20d8
commit dd6fb6473a
11 changed files with 90 additions and 25 deletions

5
.gitignore vendored
View File

@ -2,6 +2,7 @@
tmp/* tmp/*
built_examples/* built_examples/*
test/tmp test/tmp
test/fixtures/*/saved test/fixtures/stylesheets/*/tmp
test/fixtures/*/tmp test/fixtures/stylesheets/*/saved
test/fixtures/stylesheets/empty
pkg/* pkg/*

View File

@ -4,35 +4,40 @@ require 'compass'
class CompassTest < Test::Unit::TestCase class CompassTest < Test::Unit::TestCase
def setup def setup
mkdir_clean absolutize("tmp") setup_fixtures :default, :yui, :empty
mkdir_clean absolutize("tmp/blueprint")
mkdir_clean tempfile_loc("default")
mkdir_clean tempfile_loc("yui")
@original_options = Sass::Plugin.options @original_options = Sass::Plugin.options
end end
def setup_fixtures(*folders)
folders.each do |folder|
FileUtils.mkdir_p stylesheet_fixtures(folder)
mkdir_clean tempfile_loc(folder)
end
end
def teardown def teardown
FileUtils.rm_rf absolutize("tmp") teardown_fixtures :default, :yui, :empty
FileUtils.rm_rf absolutize("tmp/blueprint")
FileUtils.rm_rf tempfile_loc("default")
FileUtils.rm_rf tempfile_loc("yui")
Sass::Plugin.options = @original_options Sass::Plugin.options = @original_options
end end
def test_blueprint_generates_no_files def teardown_fixtures(*folders)
Sass::Plugin.options[:template_location][template_loc('default')] = tempfile_loc('default') folders.each do |folder|
Sass::Plugin.update_stylesheets FileUtils.rm_rf tempfile_loc(folder)
end
Dir.new(absolutize("tmp/blueprint")).each do |f| end
fail "This file is not expected: #{f}" unless f == "." || f == ".."
def test_blueprint_generates_no_files
with_templates(:empty) do
Dir.new(tempfile_loc(:empty)).each do |f|
fail "This file should not have been generated: #{f}" unless f == "." || f == ".."
end
end end
end end
def test_default def test_default
with_templates('default') do with_templates(:default) do
each_css_file(tempfile_loc('default')) do |css_file| each_css_file(tempfile_loc(:default)) do |css_file|
assert_no_errors css_file, 'default' assert_no_errors css_file, :default
end end
end end
end end
@ -41,6 +46,7 @@ class CompassTest < Test::Unit::TestCase
each_css_file(tempfile_loc('yui')) do |css_file| each_css_file(tempfile_loc('yui')) do |css_file|
assert_no_errors css_file, 'yui' assert_no_errors css_file, 'yui'
end end
assert_renders_correctly :mixins
end end
end end
private private
@ -49,10 +55,25 @@ class CompassTest < Test::Unit::TestCase
msg = "Syntax Error found in #{file}. Results saved into #{save_loc(folder)}/#{file}" msg = "Syntax Error found in #{file}. Results saved into #{save_loc(folder)}/#{file}"
assert_equal 0, open(css_file).readlines.grep(/Sass::SyntaxError/).size, msg assert_equal 0, open(css_file).readlines.grep(/Sass::SyntaxError/).size, msg
end end
def assert_renders_correctly(*arguments)
options = arguments.last.is_a?(Hash) ? arguments.pop : {}
name = arguments.shift
actual_result_file = "#{tempfile_loc(@current_template_folder)}/#{name}.css"
expected_result_file = "#{result_loc(@current_template_folder)}/#{name}.css"
actual_lines = File.read(actual_result_file).split("\n")
expected_lines = File.read(expected_result_file).split("\n")
expected_lines.zip(actual_lines).each_with_index do |pair, line|
message = "template: #{name}\nline: #{line + 1}"
assert_equal(pair.first, pair.last, message)
end
if expected_lines.size < actual_lines.size
assert(false, "#{actual_lines.size - expected_lines.size} Trailing lines found in #{actual_result_file}.css: #{actual_lines[expected_lines.size..-1].join('\n')}")
end
end
def with_templates(folder) def with_templates(folder)
old_template_loc = Sass::Plugin.options[:template_location] old_template_loc = Sass::Plugin.options[:template_location]
Sass::Plugin.options[:template_location] = old_template_loc.dup Sass::Plugin.options[:template_location] = old_template_loc.dup
@current_template_folder = folder
begin begin
Sass::Plugin.options[:template_location][template_loc(folder)] = tempfile_loc(folder) Sass::Plugin.options[:template_location][template_loc(folder)] = tempfile_loc(folder)
Compass::Frameworks::ALL.each do |framework| Compass::Frameworks::ALL.each do |framework|
@ -61,6 +82,7 @@ class CompassTest < Test::Unit::TestCase
Sass::Plugin.update_stylesheets Sass::Plugin.update_stylesheets
yield yield
ensure ensure
@current_template_folder = nil
Sass::Plugin.options[:template_location] = old_template_loc Sass::Plugin.options[:template_location] = old_template_loc
end end
rescue rescue
@ -88,20 +110,24 @@ class CompassTest < Test::Unit::TestCase
end end
end end
def stylesheet_fixtures(folder)
absolutize("fixtures/stylesheets/#{folder}")
end
def tempfile_loc(folder) def tempfile_loc(folder)
absolutize("fixtures/#{folder}/tmp") "#{stylesheet_fixtures(folder)}/tmp"
end end
def template_loc(folder) def template_loc(folder)
absolutize("fixtures/#{folder}/templates") "#{stylesheet_fixtures(folder)}/sass"
end end
def result_loc(folder) def result_loc(folder)
absolutize("fixtures/#{folder}/results") "#{stylesheet_fixtures(folder)}/css"
end end
def save_loc(folder) def save_loc(folder)
absolutize("fixtures/#{folder}/saved") "#{stylesheet_fixtures(folder)}/saved"
end end
def absolutize(path) def absolutize(path)

View File

@ -0,0 +1,16 @@
.font-size-26px {
font-size: 200%; }
.font-size-baseline {
font-size: 300%; }
.em-sizing {
width: 1em;
margin: 9em; }
.em-sizing-hack {
width: 1em;
*width: 0.975em; }

View File

@ -0,0 +1,3 @@
@import yui/modules/base.sass
+yui-base

View File

@ -0,0 +1,3 @@
@import yui/modules/fonts.sass
+yui-base-fonts

View File

@ -0,0 +1,16 @@
@import yui/modules/fonts.sass
@import yui/modules/grids.sass
.font-size-26px
+font-size(26px)
.font-size-baseline
+font-size(30px, 10px)
.em-sizing
+em-size("width", 13px)
+em-size("margin", 99px, 11px)
.em-sizing-hack
+em-size-hacked("width", 13px)