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

avoid some warnings thrown by the Ruby interpreter

This commit is contained in:
Mislav Marohnić 2008-08-30 02:40:59 +02:00 committed by Nathan Weizenbaum
parent fceeccea32
commit 429314d2b0
11 changed files with 49 additions and 34 deletions

View file

@ -1,7 +1,8 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# The command line Haml parser. # The command line Haml parser.
require File.dirname(__FILE__) + '/../lib/haml' $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'haml'
require 'haml/exec' require 'haml/exec'
opts = Haml::Exec::Haml.new(ARGV) opts = Haml::Exec::Haml.new(ARGV)

View file

@ -68,6 +68,7 @@ module Haml
:escape_html => false :escape_html => false
} }
@options.merge! options @options.merge! options
@index = 0
unless [:xhtml, :html4, :html5].include?(@options[:format]) unless [:xhtml, :html4, :html5].include?(@options[:format])
raise Haml::Error, "Invalid format #{@options[:format].inspect}" raise Haml::Error, "Invalid format #{@options[:format].inspect}"
@ -77,8 +78,8 @@ module Haml
@to_close_stack = [] @to_close_stack = []
@output_tabs = 0 @output_tabs = 0
@template_tabs = 0 @template_tabs = 0
@index = 0
@flat_spaces = -1 @flat_spaces = -1
@flat = false
@newlines = 0 @newlines = 0
@precompiled = '' @precompiled = ''
@merged_text = '' @merged_text = ''

View file

@ -1,4 +1,3 @@
require File.dirname(__FILE__) + '/../haml'
require 'optparse' require 'optparse'
require 'fileutils' require 'fileutils'

View file

@ -32,6 +32,7 @@ module Haml
def self.included(base) # :nodoc: def self.included(base) # :nodoc:
Filters.defined[base.name.split("::").last.downcase] = base Filters.defined[base.name.split("::").last.downcase] = base
base.extend(base) base.extend(base)
base.instance_variable_set "@lazy_requires", nil
end end
# Takes a string, the source text that should be passed to the filter, # Takes a string, the source text that should be passed to the filter,

View file

@ -397,7 +397,7 @@ module Haml
def capture_haml_with_buffer(local_buffer, *args, &block) def capture_haml_with_buffer(local_buffer, *args, &block)
position = local_buffer.length position = local_buffer.length
block.call *args block.call(*args)
captured = local_buffer.slice!(position..-1) captured = local_buffer.slice!(position..-1)

View file

@ -112,6 +112,8 @@ END
Line = Struct.new(:text, :unstripped, :index, :spaces, :tabs) Line = Struct.new(:text, :unstripped, :index, :spaces, :tabs)
def precompile def precompile
@haml_comment = @dont_indent_next_line = @dont_tab_up_next_text = false
@indentation = nil
old_line = Line.new old_line = Line.new
@template.split(/\r\n|\r|\n/).each_with_index do |text, index| @template.split(/\r\n|\r|\n/).each_with_index do |text, index|
@next_line = line = Line.new(text.strip, text.lstrip.chomp, index) @next_line = line = Line.new(text.strip, text.lstrip.chomp, index)

View file

@ -8,7 +8,7 @@ module Sass::Constant # :nodoc:
def parse(value) def parse(value)
first, second, unit = value.scan(Literal::NUMBER)[0] first, second, unit = value.scan(Literal::NUMBER)[0]
@value = first.empty? ? second.to_i : "#{first}#{second}".to_f @value = first.empty? ? second.to_i : "#{first}#{second}".to_f
@unit = unit unless unit.empty? @unit = unit.empty? ? nil : unit
end end
def plus(other) def plus(other)

View file

@ -2,7 +2,7 @@
require File.dirname(__FILE__) + '/../test_helper' require File.dirname(__FILE__) + '/../test_helper'
class EngineTest < Test::Unit::TestCase class EngineTest < Test::Unit::TestCase
# A map of erroneous Sass documents to the error messages they should produce. # A map of erroneous Haml documents to the error messages they should produce.
# The error messages may be arrays; # The error messages may be arrays;
# if so, the second element should be the line number that should be reported for the error. # if so, the second element should be the line number that should be reported for the error.
# If this isn't provided, the tests will assume the line number should be the last line of the document. # If this isn't provided, the tests will assume the line number should be the last line of the document.
@ -34,13 +34,7 @@ END
"%a/ b" => "Self-closing tags can't have content.", "%a/ b" => "Self-closing tags can't have content.",
" %p foo" => "Indenting at the beginning of the document is illegal.", " %p foo" => "Indenting at the beginning of the document is illegal.",
" %p foo" => "Indenting at the beginning of the document is illegal.", " %p foo" => "Indenting at the beginning of the document is illegal.",
"- end" => <<END.rstrip, "- end" => "You don't need to use \"- end\" in Haml. Use indentation instead:\n- if foo?\n %strong Foo!\n- else\n Not foo.",
You don't need to use "- end" in Haml. Use indentation instead:
- if foo?
%strong Foo!
- else
Not foo.
END
" \n\t\n %p foo" => ["Indenting at the beginning of the document is illegal.", 3], " \n\t\n %p foo" => ["Indenting at the beginning of the document is illegal.", 3],
# Regression tests # Regression tests
@ -64,7 +58,17 @@ END
def render(text, options = {}, &block) def render(text, options = {}, &block)
scope = options.delete(:scope) || Object.new scope = options.delete(:scope) || Object.new
locals = options.delete(:locals) || {} locals = options.delete(:locals) || {}
Haml::Engine.new(text, options).to_html(scope, locals, &block) engine(text, options).to_html(scope, locals, &block)
end
def engine(text, options = {})
unless options[:filename]
# use caller method name as fake filename. useful for debugging
i = -1
caller[i+=1] =~ /`(.+?)'/ until $1 and $1.index('test_') == 0
options[:filename] = "(#{$1})"
end
Haml::Engine.new(text, options)
end end
def test_empty_render_should_remain_empty def test_empty_render_should_remain_empty
@ -99,7 +103,7 @@ END
end end
def test_multi_render def test_multi_render
engine = Haml::Engine.new("%strong Hi there!") engine = engine("%strong Hi there!")
assert_equal("<strong>Hi there!</strong>\n", engine.to_html) assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
assert_equal("<strong>Hi there!</strong>\n", engine.to_html) assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
assert_equal("<strong>Hi there!</strong>\n", engine.to_html) assert_equal("<strong>Hi there!</strong>\n", engine.to_html)
@ -270,14 +274,14 @@ SOURCE
render("\n\n = abc", :filename => 'test', :line => 2) render("\n\n = abc", :filename => 'test', :line => 2)
rescue Exception => e rescue Exception => e
assert_kind_of Haml::SyntaxError, e assert_kind_of Haml::SyntaxError, e
assert_match /test:4/, e.backtrace.first assert_match(/test:4/, e.backtrace.first)
end end
begin begin
render("\n\n= 123\n\n= nil[]", :filename => 'test', :line => 2) render("\n\n= 123\n\n= nil[]", :filename => 'test', :line => 2)
rescue Exception => e rescue Exception => e
assert_kind_of NoMethodError, e assert_kind_of NoMethodError, e
assert_match /test:6/, e.backtrace.first assert_match(/test:6/, e.backtrace.first)
end end
end end
@ -364,12 +368,15 @@ SOURCE
def test_exceptions def test_exceptions
EXCEPTION_MAP.each do |key, value| EXCEPTION_MAP.each do |key, value|
begin begin
render(key) render(key, :filename => "(exception test for #{key.inspect})")
rescue Exception => err rescue Exception => err
value = [value] unless value.is_a?(Array) value = [value] unless value.is_a?(Array)
expected_message, line_no = value
line_no ||= key.split("\n").length
line_reported = err.backtrace[0].gsub(/\(.+\):/, '').to_i
assert_equal(value.first, err.message, "Line: #{key}") assert_equal(expected_message, err.message, "Line: #{key}")
assert_equal(value[1] || key.split("\n").length, err.backtrace[0].gsub('(haml):', '').to_i, "Line: #{key}") assert_equal(line_no, line_reported, "Line: #{key}")
else else
assert(false, "Exception not raised for\n#{key}") assert(false, "Exception not raised for\n#{key}")
end end
@ -379,7 +386,7 @@ SOURCE
def test_exception_line def test_exception_line
render("a\nb\n!!!\n c\nd") render("a\nb\n!!!\n c\nd")
rescue Haml::SyntaxError => e rescue Haml::SyntaxError => e
assert_equal("(haml):4", e.backtrace[0]) assert_equal("(test_exception_line):4", e.backtrace[0])
else else
assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce an exception') assert(false, '"a\nb\n!!!\n c\nd" doesn\'t produce an exception')
end end
@ -387,7 +394,7 @@ SOURCE
def test_exception def test_exception
render("%p\n hi\n %a= undefined\n= 12") render("%p\n hi\n %a= undefined\n= 12")
rescue Exception => e rescue Exception => e
assert_match("(haml):3", e.backtrace[0]) assert_match("(test_exception):3", e.backtrace[0])
else else
# Test failed... should have raised an exception # Test failed... should have raised an exception
assert(false) assert(false)
@ -396,7 +403,7 @@ SOURCE
def test_compile_error def test_compile_error
render("a\nb\n- fee)\nc") render("a\nb\n- fee)\nc")
rescue Exception => e rescue Exception => e
assert_match(/^compile error\n\(haml\):3: syntax error/i, e.message) assert_match(/^compile error\n\(test_compile_error\):3: syntax error/i, e.message)
else else
assert(false, assert(false,
'"a\nb\n- fee)\nc" doesn\'t produce an exception!') '"a\nb\n- fee)\nc" doesn\'t produce an exception!')
@ -471,28 +478,28 @@ END
def test_yield_should_work_with_def_method def test_yield_should_work_with_def_method
s = "foo" s = "foo"
Haml::Engine.new("= yield\n= upcase").def_method(s, :render) engine("= yield\n= upcase").def_method(s, :render)
assert_equal("12\nFOO\n", s.render { 12 }) assert_equal("12\nFOO\n", s.render { 12 })
end end
def test_def_method_with_module def test_def_method_with_module
Haml::Engine.new("= yield\n= upcase").def_method(String, :render_haml) engine("= yield\n= upcase").def_method(String, :render_haml)
assert_equal("12\nFOO\n", "foo".render_haml { 12 }) assert_equal("12\nFOO\n", "foo".render_haml { 12 })
end end
def test_def_method_locals def test_def_method_locals
obj = Object.new obj = Object.new
Haml::Engine.new("%p= foo\n.bar{:baz => baz}= boom").def_method(obj, :render, :foo, :baz, :boom) engine("%p= foo\n.bar{:baz => baz}= boom").def_method(obj, :render, :foo, :baz, :boom)
assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", obj.render(:foo => 1, :baz => 2, :boom => 3)) assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", obj.render(:foo => 1, :baz => 2, :boom => 3))
end end
def test_render_proc_locals def test_render_proc_locals
proc = Haml::Engine.new("%p= foo\n.bar{:baz => baz}= boom").render_proc(Object.new, :foo, :baz, :boom) proc = engine("%p= foo\n.bar{:baz => baz}= boom").render_proc(Object.new, :foo, :baz, :boom)
assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", proc[:foo => 1, :baz => 2, :boom => 3]) assert_equal("<p>1</p>\n<div baz='2' class='bar'>3</div>\n", proc[:foo => 1, :baz => 2, :boom => 3])
end end
def test_render_proc_with_binding def test_render_proc_with_binding
assert_equal("FOO\n", Haml::Engine.new("= upcase").render_proc("foo".instance_eval{binding}).call) assert_equal("FOO\n", engine("= upcase").render_proc("foo".instance_eval{binding}).call)
end end
def test_ugly_true def test_ugly_true
@ -519,7 +526,7 @@ END
end end
def test_arbitrary_output_option def test_arbitrary_output_option
assert_raise(Haml::Error, "Invalid output format :html1") { Haml::Engine.new("%br", :format => :html1) } assert_raise(Haml::Error, "Invalid output format :html1") { engine("%br", :format => :html1) }
end end
# HTML 4.0 # HTML 4.0
@ -548,7 +555,7 @@ END
# because anything before the doctype triggers quirks mode in IE # because anything before the doctype triggers quirks mode in IE
def test_xml_prolog_and_doctype_dont_result_in_a_leading_whitespace_in_html def test_xml_prolog_and_doctype_dont_result_in_a_leading_whitespace_in_html
assert_no_match /^\s+/, render("!!! xml\n!!!", :format => :html4) assert_no_match(/^\s+/, render("!!! xml\n!!!", :format => :html4))
end end
# HTML5 # HTML5

View file

@ -52,7 +52,8 @@ class Html2HamlTest < Test::Unit::TestCase
end end
def assert_equal_attributes(expected, result) def assert_equal_attributes(expected, result)
assert_equal *[expected, result].map { |s| s.gsub!(/\{ (.+) \}/, ''); $1.split(', ').sort } expected_attr, result_attr = [expected, result].map { |s| s.gsub!(/\{ (.+) \}/, ''); $1.split(', ').sort }
assert_equal expected_attr, result_attr
assert_equal expected, result assert_equal expected, result
end end
end end

View file

@ -151,5 +151,6 @@ class Sass::Engine
end end
class ActionController::Base class ActionController::Base
undef :sass_old_process
def sass_old_process(*args); end def sass_old_process(*args); end
end end

View file

@ -1,3 +1,4 @@
lib_dir = File.dirname(__FILE__) + '/../lib'
# allows testing with edge Rails by creating a test/rails symlink # allows testing with edge Rails by creating a test/rails symlink
linked_rails = File.dirname(__FILE__) + '/rails' linked_rails = File.dirname(__FILE__) + '/rails'
@ -12,5 +13,6 @@ require 'action_controller'
require 'action_view' require 'action_view'
require 'test/unit' require 'test/unit'
require File.dirname(__FILE__) + '/../lib/haml' $:.unshift lib_dir unless $:.include?(lib_dir)
require File.dirname(__FILE__) + '/../lib/sass' require 'haml'
require 'sass'