1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Tests - and requisite fixes - for Sassy importing.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@448 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-03-25 08:20:33 +00:00
parent a9e4883c68
commit a540b2a4bd
7 changed files with 57 additions and 65 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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