From dd6fb6473a1eb24d61c6a9dd36956c89ab3c01da Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Thu, 4 Dec 2008 12:35:00 -0800 Subject: [PATCH] Added unit tests for YUI and did some cleanup/restructuring of the test fixtures as well as a way to validate generated css. --- .gitignore | 5 +- .../compass/{reset.sass => _reset.sass} | 0 test/compass_test.rb | 72 +++++++++++++------ .../default/sass}/ie.sass | 0 .../default/sass}/print.sass | 0 .../default/sass}/screen.sass | 0 test/fixtures/stylesheets/yui/css/mixins.css | 16 +++++ test/fixtures/stylesheets/yui/sass/base.sass | 3 + test/fixtures/stylesheets/yui/sass/fonts.sass | 3 + .../yui/sass}/grids.sass | 0 .../fixtures/stylesheets/yui/sass/mixins.sass | 16 +++++ 11 files changed, 90 insertions(+), 25 deletions(-) rename frameworks/compass/stylesheets/compass/{reset.sass => _reset.sass} (100%) rename test/fixtures/{default/templates => stylesheets/default/sass}/ie.sass (100%) rename test/fixtures/{default/templates => stylesheets/default/sass}/print.sass (100%) rename test/fixtures/{default/templates => stylesheets/default/sass}/screen.sass (100%) create mode 100644 test/fixtures/stylesheets/yui/css/mixins.css create mode 100644 test/fixtures/stylesheets/yui/sass/base.sass create mode 100644 test/fixtures/stylesheets/yui/sass/fonts.sass rename test/fixtures/{yui/templates => stylesheets/yui/sass}/grids.sass (100%) create mode 100644 test/fixtures/stylesheets/yui/sass/mixins.sass diff --git a/.gitignore b/.gitignore index 89c1c4ac..90614b61 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ tmp/* built_examples/* test/tmp -test/fixtures/*/saved -test/fixtures/*/tmp +test/fixtures/stylesheets/*/tmp +test/fixtures/stylesheets/*/saved +test/fixtures/stylesheets/empty pkg/* diff --git a/frameworks/compass/stylesheets/compass/reset.sass b/frameworks/compass/stylesheets/compass/_reset.sass similarity index 100% rename from frameworks/compass/stylesheets/compass/reset.sass rename to frameworks/compass/stylesheets/compass/_reset.sass diff --git a/test/compass_test.rb b/test/compass_test.rb index a953d557..b0c844eb 100644 --- a/test/compass_test.rb +++ b/test/compass_test.rb @@ -4,35 +4,40 @@ require 'compass' class CompassTest < Test::Unit::TestCase def setup - mkdir_clean absolutize("tmp") - mkdir_clean absolutize("tmp/blueprint") - mkdir_clean tempfile_loc("default") - mkdir_clean tempfile_loc("yui") + setup_fixtures :default, :yui, :empty @original_options = Sass::Plugin.options end + def setup_fixtures(*folders) + folders.each do |folder| + FileUtils.mkdir_p stylesheet_fixtures(folder) + mkdir_clean tempfile_loc(folder) + end + end + def teardown - FileUtils.rm_rf absolutize("tmp") - FileUtils.rm_rf absolutize("tmp/blueprint") - FileUtils.rm_rf tempfile_loc("default") - FileUtils.rm_rf tempfile_loc("yui") + teardown_fixtures :default, :yui, :empty Sass::Plugin.options = @original_options end - def test_blueprint_generates_no_files - Sass::Plugin.options[:template_location][template_loc('default')] = tempfile_loc('default') - Sass::Plugin.update_stylesheets - - Dir.new(absolutize("tmp/blueprint")).each do |f| - fail "This file is not expected: #{f}" unless f == "." || f == ".." + def teardown_fixtures(*folders) + folders.each do |folder| + FileUtils.rm_rf tempfile_loc(folder) + end + end + + 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 def test_default - with_templates('default') do - each_css_file(tempfile_loc('default')) do |css_file| - assert_no_errors css_file, 'default' + with_templates(:default) do + each_css_file(tempfile_loc(:default)) do |css_file| + assert_no_errors css_file, :default end end end @@ -41,6 +46,7 @@ class CompassTest < Test::Unit::TestCase each_css_file(tempfile_loc('yui')) do |css_file| assert_no_errors css_file, 'yui' end + assert_renders_correctly :mixins end end private @@ -49,10 +55,25 @@ class CompassTest < Test::Unit::TestCase 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 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) old_template_loc = Sass::Plugin.options[:template_location] Sass::Plugin.options[:template_location] = old_template_loc.dup + @current_template_folder = folder begin Sass::Plugin.options[:template_location][template_loc(folder)] = tempfile_loc(folder) Compass::Frameworks::ALL.each do |framework| @@ -61,6 +82,7 @@ class CompassTest < Test::Unit::TestCase Sass::Plugin.update_stylesheets yield ensure + @current_template_folder = nil Sass::Plugin.options[:template_location] = old_template_loc end rescue @@ -88,20 +110,24 @@ class CompassTest < Test::Unit::TestCase end end + def stylesheet_fixtures(folder) + absolutize("fixtures/stylesheets/#{folder}") + end + def tempfile_loc(folder) - absolutize("fixtures/#{folder}/tmp") + "#{stylesheet_fixtures(folder)}/tmp" end def template_loc(folder) - absolutize("fixtures/#{folder}/templates") + "#{stylesheet_fixtures(folder)}/sass" end def result_loc(folder) - absolutize("fixtures/#{folder}/results") + "#{stylesheet_fixtures(folder)}/css" end def save_loc(folder) - absolutize("fixtures/#{folder}/saved") + "#{stylesheet_fixtures(folder)}/saved" end def absolutize(path) diff --git a/test/fixtures/default/templates/ie.sass b/test/fixtures/stylesheets/default/sass/ie.sass similarity index 100% rename from test/fixtures/default/templates/ie.sass rename to test/fixtures/stylesheets/default/sass/ie.sass diff --git a/test/fixtures/default/templates/print.sass b/test/fixtures/stylesheets/default/sass/print.sass similarity index 100% rename from test/fixtures/default/templates/print.sass rename to test/fixtures/stylesheets/default/sass/print.sass diff --git a/test/fixtures/default/templates/screen.sass b/test/fixtures/stylesheets/default/sass/screen.sass similarity index 100% rename from test/fixtures/default/templates/screen.sass rename to test/fixtures/stylesheets/default/sass/screen.sass diff --git a/test/fixtures/stylesheets/yui/css/mixins.css b/test/fixtures/stylesheets/yui/css/mixins.css new file mode 100644 index 00000000..006b0693 --- /dev/null +++ b/test/fixtures/stylesheets/yui/css/mixins.css @@ -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; } \ No newline at end of file diff --git a/test/fixtures/stylesheets/yui/sass/base.sass b/test/fixtures/stylesheets/yui/sass/base.sass new file mode 100644 index 00000000..8c0bc40b --- /dev/null +++ b/test/fixtures/stylesheets/yui/sass/base.sass @@ -0,0 +1,3 @@ +@import yui/modules/base.sass + ++yui-base \ No newline at end of file diff --git a/test/fixtures/stylesheets/yui/sass/fonts.sass b/test/fixtures/stylesheets/yui/sass/fonts.sass new file mode 100644 index 00000000..a73ec8e9 --- /dev/null +++ b/test/fixtures/stylesheets/yui/sass/fonts.sass @@ -0,0 +1,3 @@ +@import yui/modules/fonts.sass + ++yui-base-fonts \ No newline at end of file diff --git a/test/fixtures/yui/templates/grids.sass b/test/fixtures/stylesheets/yui/sass/grids.sass similarity index 100% rename from test/fixtures/yui/templates/grids.sass rename to test/fixtures/stylesheets/yui/sass/grids.sass diff --git a/test/fixtures/stylesheets/yui/sass/mixins.sass b/test/fixtures/stylesheets/yui/sass/mixins.sass new file mode 100644 index 00000000..d0dbea12 --- /dev/null +++ b/test/fixtures/stylesheets/yui/sass/mixins.sass @@ -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) +