2006-11-28 20:33:22 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'test/unit'
|
2006-12-03 23:56:47 +00:00
|
|
|
require File.dirname(__FILE__) + '/../../lib/sass'
|
|
|
|
require 'sass/engine'
|
2006-11-28 20:33:22 +00:00
|
|
|
|
|
|
|
class SassEngineTest < Test::Unit::TestCase
|
2006-12-25 00:17:02 +00:00
|
|
|
EXCEPTION_MAP = {
|
2007-01-31 04:30:30 +00:00
|
|
|
"!a = 1 + " => 'Constant arithmetic error: "1 +"',
|
|
|
|
"!a = 1 + 2 +" => 'Constant arithmetic error: "1 + 2 +"',
|
2007-02-04 03:39:48 +00:00
|
|
|
"!a = hello \"world\"" => 'Constant arithmetic error: "hello \\"world\\""',
|
2007-02-04 03:44:44 +00:00
|
|
|
"!a = \"b" => 'Unterminated string: "\\"b"',
|
2007-01-31 04:30:30 +00:00
|
|
|
"!a = #aaa - a" => 'Undefined operation: "#afafaf minus a"',
|
|
|
|
"!a = #aaa / a" => 'Undefined operation: "#afafaf div a"',
|
|
|
|
"!a = #aaa * a" => 'Undefined operation: "#afafaf times a"',
|
|
|
|
"!a = #aaa % a" => 'Undefined operation: "#afafaf mod a"',
|
|
|
|
"!a = 1 - a" => 'Undefined operation: "1 minus a"',
|
|
|
|
"!a = 1 * a" => 'Undefined operation: "1 times a"',
|
|
|
|
"!a = 1 / a" => 'Undefined operation: "1 div a"',
|
|
|
|
"!a = 1 % a" => 'Undefined operation: "1 mod a"',
|
|
|
|
":" => 'Invalid attribute: ":"',
|
|
|
|
": a" => 'Invalid attribute: ": a"',
|
|
|
|
":= a" => 'Invalid attribute: ":= a"',
|
|
|
|
"a\n :b" => 'Invalid attribute: ":b "',
|
|
|
|
":a" => 'Attributes aren\'t allowed at the root of a document.',
|
|
|
|
"!" => 'Invalid constant: "!"',
|
|
|
|
"!a" => 'Invalid constant: "!a"',
|
|
|
|
"! a" => 'Invalid constant: "! a"',
|
|
|
|
"!a b" => 'Invalid constant: "!a b"',
|
|
|
|
"a\n\t:b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.",
|
|
|
|
"a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.",
|
|
|
|
"a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.",
|
2006-12-25 00:17:02 +00:00
|
|
|
}
|
2006-11-28 20:33:22 +00:00
|
|
|
|
|
|
|
def test_basic_render
|
|
|
|
renders_correctly "basic"
|
|
|
|
end
|
2006-12-25 00:17:02 +00:00
|
|
|
|
|
|
|
def test_exceptions
|
|
|
|
EXCEPTION_MAP.each do |key, value|
|
|
|
|
begin
|
|
|
|
Sass::Engine.new(key).render
|
2007-01-28 10:28:19 +00:00
|
|
|
rescue Sass::SyntaxError => err
|
|
|
|
assert_equal(value, err.message)
|
2007-01-31 04:39:23 +00:00
|
|
|
assert(err.sass_line, "Line: #{key}")
|
|
|
|
assert_match(/\(sass\):[0-9]+/, err.backtrace[0], "Line: #{key}")
|
2007-01-28 10:28:19 +00:00
|
|
|
else
|
2007-01-31 04:30:30 +00:00
|
|
|
assert(false, "Exception not raised for '#{key}'!")
|
2007-01-28 10:28:19 +00:00
|
|
|
end
|
2006-12-25 00:17:02 +00:00
|
|
|
end
|
|
|
|
end
|
2007-01-31 04:30:30 +00:00
|
|
|
|
|
|
|
def test_exception_line
|
|
|
|
to_render = "rule\n :attr val\n :broken\n"
|
|
|
|
begin
|
|
|
|
Sass::Engine.new(to_render).render
|
|
|
|
rescue Sass::SyntaxError => err
|
|
|
|
assert_equal(3, err.sass_line)
|
|
|
|
else
|
|
|
|
assert(false, "Exception not raised for '#{to_render}'!")
|
|
|
|
end
|
|
|
|
end
|
2006-12-25 00:17:02 +00:00
|
|
|
|
|
|
|
private
|
2006-11-28 20:33:22 +00:00
|
|
|
|
|
|
|
def renders_correctly(name)
|
|
|
|
sass_file = load_file(name, "sass")
|
|
|
|
css_file = load_file(name, "css")
|
2006-12-04 02:47:37 +00:00
|
|
|
css_result = Sass::Engine.new(sass_file).render
|
2006-11-28 20:33:22 +00:00
|
|
|
#puts css_result.collect { |a| a.inspect }.join("\n ")
|
2006-12-04 08:40:23 +00:00
|
|
|
assert_equal css_file, css_result
|
2006-11-28 20:33:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_file(name, type = "sass")
|
|
|
|
@result = ''
|
2006-12-03 22:37:17 +00:00
|
|
|
File.new(File.dirname(__FILE__) + "/#{type == 'sass' ? 'templates' : 'results'}/#{name}.#{type}").each_line { |l| @result += l }
|
2006-11-28 20:33:22 +00:00
|
|
|
@result
|
|
|
|
end
|
|
|
|
end
|