2008-08-23 17:00:46 +00:00
require File . dirname ( __FILE__ ) + '/test_helper'
require 'fileutils'
require 'compass'
class CompassTest < Test :: Unit :: TestCase
def setup
2008-12-06 01:48:26 +00:00
setup_fixtures :blueprint , :yui , :empty
2008-11-29 09:15:12 +00:00
@original_options = Sass :: Plugin . options
2008-08-23 17:00:46 +00:00
end
2008-12-04 20:35:00 +00:00
def setup_fixtures ( * folders )
folders . each do | folder |
FileUtils . mkdir_p stylesheet_fixtures ( folder )
mkdir_clean tempfile_loc ( folder )
end
end
2008-08-23 17:00:46 +00:00
def teardown
2008-12-06 01:48:26 +00:00
teardown_fixtures :blueprint , :yui , :empty
2008-11-29 09:15:12 +00:00
Sass :: Plugin . options = @original_options
2008-08-23 17:00:46 +00:00
end
2008-12-04 20:35:00 +00:00
def teardown_fixtures ( * folders )
folders . each do | folder |
FileUtils . rm_rf tempfile_loc ( folder )
end
end
2008-08-23 17:00:46 +00:00
2008-12-04 20:35:00 +00:00
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
2008-08-23 17:00:46 +00:00
end
end
2008-12-06 01:48:26 +00:00
def test_blueprint
with_templates ( :blueprint ) do
each_css_file ( tempfile_loc ( :blueprint ) ) do | css_file |
assert_no_errors css_file , :blueprint
2008-08-23 17:00:46 +00:00
end
2008-12-06 01:48:26 +00:00
assert_renders_correctly :typography
2008-08-23 17:00:46 +00:00
end
end
2008-11-29 09:15:12 +00:00
def test_yui
with_templates ( 'yui' ) do
each_css_file ( tempfile_loc ( 'yui' ) ) do | css_file |
assert_no_errors css_file , 'yui'
end
2008-12-04 20:35:00 +00:00
assert_renders_correctly :mixins
2008-11-29 09:15:12 +00:00
end
end
2008-12-17 18:07:54 +00:00
def test_compass
with_templates ( 'compass' ) do
each_css_file ( tempfile_loc ( 'compass' ) ) do | css_file |
assert_no_errors css_file , 'compass'
end
assert_renders_correctly :reset , :layout , :utilities
end
end
2008-08-23 17:00:46 +00:00
private
def assert_no_errors ( css_file , folder )
file = css_file [ ( tempfile_loc ( folder ) . size + 1 ) .. - 1 ]
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
2008-12-04 20:35:00 +00:00
def assert_renders_correctly ( * arguments )
options = arguments . last . is_a? ( Hash ) ? arguments . pop : { }
2008-12-17 18:07:54 +00:00
for name in arguments
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 } \n line: #{ 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
2008-12-04 20:35:00 +00:00
end
end
2008-08-23 17:00:46 +00:00
def with_templates ( folder )
2008-11-29 09:15:12 +00:00
old_template_loc = Sass :: Plugin . options [ :template_location ]
2009-01-13 16:08:27 +00:00
Sass :: Plugin . options [ :template_location ] = if old_template_loc . is_a? ( Hash )
old_template_loc . dup
else
Hash . new
end
2008-12-04 20:35:00 +00:00
@current_template_folder = folder
2008-08-23 17:00:46 +00:00
begin
Sass :: Plugin . options [ :template_location ] [ template_loc ( folder ) ] = tempfile_loc ( folder )
Compass :: Frameworks :: ALL . each do | framework |
Sass :: Plugin . options [ :template_location ] [ framework . stylesheets_directory ] = tempfile_loc ( folder )
end
Sass :: Plugin . update_stylesheets
yield
ensure
2008-12-04 20:35:00 +00:00
@current_template_folder = nil
2008-08-23 17:00:46 +00:00
Sass :: Plugin . options [ :template_location ] = old_template_loc
end
rescue
save_output ( folder )
raise
end
def each_css_file ( dir )
Dir . glob ( " #{ dir } /**/*.css " ) . each do | css_file |
yield css_file
end
end
def save_output ( dir )
FileUtils . rm_rf ( save_loc ( dir ) )
FileUtils . cp_r ( tempfile_loc ( dir ) , save_loc ( dir ) )
end
def mkdir_clean ( dir )
begin
FileUtils . mkdir dir
rescue Errno :: EEXIST
FileUtils . rm_r dir
FileUtils . mkdir dir
end
end
2008-12-04 20:35:00 +00:00
def stylesheet_fixtures ( folder )
absolutize ( " fixtures/stylesheets/ #{ folder } " )
end
2008-08-23 17:00:46 +00:00
def tempfile_loc ( folder )
2008-12-04 20:35:00 +00:00
" #{ stylesheet_fixtures ( folder ) } /tmp "
2008-08-23 17:00:46 +00:00
end
def template_loc ( folder )
2008-12-04 20:35:00 +00:00
" #{ stylesheet_fixtures ( folder ) } /sass "
2008-08-23 17:00:46 +00:00
end
def result_loc ( folder )
2008-12-04 20:35:00 +00:00
" #{ stylesheet_fixtures ( folder ) } /css "
2008-08-23 17:00:46 +00:00
end
def save_loc ( folder )
2008-12-04 20:35:00 +00:00
" #{ stylesheet_fixtures ( folder ) } /saved "
2008-08-23 17:00:46 +00:00
end
def absolutize ( path )
if path . blank?
File . dirname ( __FILE__ )
elsif path [ 0 ] == ?/
" #{ File . dirname ( __FILE__ ) } #{ path } "
else
" #{ File . dirname ( __FILE__ ) } / #{ path } "
end
end
end