From a540b2a4bda4fae4688df91de44fffdbee45a9cb Mon Sep 17 00:00:00 2001 From: nex3 Date: Sun, 25 Mar 2007 08:20:33 +0000 Subject: [PATCH] Tests - and requisite fixes - for Sassy importing. git-svn-id: svn://hamptoncatlin.com/haml/trunk@448 7063305b-7217-0410-af8c-cdc13e5119b9 --- Rakefile | 1 + lib/haml/engine.rb | 21 +------------ lib/sass/engine.rb | 64 ++++++++++++++++++---------------------- lib/sass/plugin.rb | 2 +- test/haml/engine_test.rb | 9 ++---- test/sass/engine_test.rb | 18 +++++++++++ test/sass/plugin_test.rb | 7 +++-- 7 files changed, 57 insertions(+), 65 deletions(-) diff --git a/Rakefile b/Rakefile index fd89be6c..b2abf66b 100644 --- a/Rakefile +++ b/Rakefile @@ -103,6 +103,7 @@ unless ARGV[0] == 'benchmark' rdoc.rdoc_files.include('README') rdoc.rdoc_files.include('lib/**/*.rb') rdoc.rdoc_files.exclude('lib/haml/buffer.rb') + rdoc.rdoc_files.exclude('lib/haml/util.rb') rdoc.rdoc_files.exclude('lib/sass/tree/*') end diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 0e90cab2..0ca1da0f 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -2,6 +2,7 @@ require 'haml/helpers' require 'haml/buffer' require 'haml/filters' require 'haml/error' +require 'haml/util' module Haml # This is the class where all the parsing and processing of the Haml @@ -722,23 +723,3 @@ END end end end - -class Hash # :nodoc: - # Same as Hash#merge!, but recursively merges sub-hashes. - def rec_merge!(other) - other.each do |key, value| - myval = self[key] - if value.is_a?(Hash) && myval.is_a?(Hash) - myval.rec_merge!(value) - else - self[key] = value - end - end - self - end - - def rec_merge(other) - toret = self.clone - toret.rec_merge! other - end -end diff --git a/lib/sass/engine.rb b/lib/sass/engine.rb index e5181183..f7761e6c 100644 --- a/lib/sass/engine.rb +++ b/lib/sass/engine.rb @@ -5,6 +5,7 @@ require 'sass/tree/comment_node' require 'sass/tree/attr_node' require 'sass/constant' require 'sass/error' +require 'haml/util' module Sass # This is the class where all the parsing and processing of the Sass @@ -184,7 +185,7 @@ module Sass if child == :constant raise SyntaxError.new("Constants may only be declared at the root of a document.", @line) elsif child.is_a? Array - raise SyntaxError.new("The import directive may only be used at the root of a document.", @line) + raise SyntaxError.new("Import directives may only be used at the root of a document.", @line) elsif child.is_a? Tree::Node child.line = @line node << child @@ -261,7 +262,7 @@ module Sass when "import" import(value) else - raise SyntaxError.new("Unknown compiler directive: #{"@{directive} #{value}".dump}") + raise SyntaxError.new("Unknown compiler directive: #{"@#{directive} #{value}".dump}", @line) end end @@ -280,6 +281,8 @@ module Sass engine = Sass::Engine.new(file.read, @options) end + engine.constants.merge! @constants + begin root = engine.render_to_tree rescue Sass::SyntaxError => err @@ -290,7 +293,7 @@ module Sass child.filename = filename nodes << child end - @constants.merge! engine.constants + @constants = engine.constants end end @@ -298,44 +301,35 @@ module Sass end def find_file_to_import(filename) - has_ext = + was_sass = false + original_filename = filename new_filename = nil - if filename.include?('.') - @options[:load_paths].each do |path| - full_path = File.join(path, filename) + if filename[-5..-1] == ".sass" + filename = filename[0...-5] + was_sass = true + elsif filename[-4..-1] == ".css" + return filename + end - if File.readable?(full_path) - new_filename = full_path - break - end + @options[:load_paths].each do |path| + full_path = File.join(path, filename) + '.sass' + + if File.readable?(full_path) + new_filename = full_path + break + end + end + + if new_filename.nil? + if was_sass + raise SyntaxError.new("File to import not found or unreadable: #{original_filename}", @line) + else + return filename + '.css' end else - @options[:load_paths].each do |path| - full_path = File.join(path, filename) + '.sass' - - if File.readable?(full_path) - new_filename = full_path - break - end - end - - unless new_filename - @options[:load_paths].each do |path| - full_path = File.join(path, filename) + '.css' - - if File.readable?(full_path) - new_filename = full_path - break - end - end - end + new_filename end - - unless new_filename - raise SyntaxError.new("File to import not found or unreadable: #{filename}") - end - new_filename end end end diff --git a/lib/sass/plugin.rb b/lib/sass/plugin.rb index b241482f..da2a910d 100644 --- a/lib/sass/plugin.rb +++ b/lib/sass/plugin.rb @@ -47,7 +47,7 @@ module Sass filename = template_filename(name) l_options = @@options.dup l_options[:filename] = filename - l_options[:load_paths] ||= [l_options[:template_location], l_options[:css_location]] + l_options[:load_paths] = (l_options[:load_paths] || []) + [l_options[:template_location]] engine = Engine.new(File.read(filename), l_options) begin result = engine.render diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index 3b682e86..efcab248 100644 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -93,13 +93,10 @@ class EngineTest < Test::Unit::TestCase end def test_rec_merge - hash1 = {1=>2, 3=>{5=>7, 8=>9}} - hash1_2 = hash1.clone - hash2 = {4=>5, 3=>{5=>2, 16=>12}} - hash3 = {1=>2, 4=>5, 3=>{5=>2, 8=>9, 16=>12}} + hash1 = {1=>2, 3=>{5=>7, 8=>9}, 10=>[1, 2, 3]} + hash2 = {4=>5, 3=>{5=>2, 16=>12}, 10=>[4, 5, 6]} + hash3 = {1=>2, 4=>5, 3=>{5=>2, 8=>9, 16=>12}, 10=>[1, 2, 3, 4, 5, 6]} - assert_equal(hash3, hash1.rec_merge(hash2)) - assert_equal(hash1_2, hash1) hash1.rec_merge!(hash2) assert_equal(hash3, hash1) end diff --git a/test/sass/engine_test.rb b/test/sass/engine_test.rb index ed978994..6bd8b072 100644 --- a/test/sass/engine_test.rb +++ b/test/sass/engine_test.rb @@ -36,6 +36,10 @@ class SassEngineTest < Test::Unit::TestCase "& a\n :b c" => "Base-level rules cannot contain the parent-selector-referencing character '&'", "a\n :b\n c" => "Illegal nesting: Only attributes may be nested beneath attributes.", "!a = b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath constants.", + "@import foo.sass" => "File to import not found or unreadable: foo.sass", + "@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.", + "foo\n @import templates/basic" => "Import directives may only be used at the root of a document.", + "@foo bar boom" => "Unknown compiler directive: \"@foo bar boom\"", } def test_basic_render @@ -72,6 +76,20 @@ class SassEngineTest < Test::Unit::TestCase assert(false, "Exception not raised for '#{to_render}'!") end end + + def test_imported_exception + [1, 2].each do |i| + i = nil if i == 1 + begin + Sass::Engine.new("@import bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render + rescue Sass::SyntaxError => err + assert_equal(2, err.sass_line) + assert_match(/bork#{i}\.sass$/, err.sass_filename) + else + assert(false, "Exception not raised for imported template: bork#{i}") + end + end + end private diff --git a/test/sass/plugin_test.rb b/test/sass/plugin_test.rb index a1636916..d9f11f78 100644 --- a/test/sass/plugin_test.rb +++ b/test/sass/plugin_test.rb @@ -8,13 +8,14 @@ RAILS_ENV = 'testing' require 'sass/plugin' class SassPluginTest < Test::Unit::TestCase - @@templates = %w{ complex constants parent_ref } + @@templates = %w{ complex constants parent_ref import } def setup Sass::Plugin.options = { :template_location => File.dirname(__FILE__) + '/templates', :css_location => File.dirname(__FILE__) + '/tmp', - :style => :compact + :style => :compact, + :load_paths => [File.dirname(__FILE__) + '/results'], } Sass::Plugin.options[:always_update] = true @@ -22,7 +23,7 @@ class SassPluginTest < Test::Unit::TestCase end def teardown - File.delete(*Dir[tempfile_loc('*')]) + #File.delete(*Dir[tempfile_loc('*')]) end def test_templates_should_render_correctly