From dcbf0c95edfdc8d748b78a60328c668c096b5555 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Mislav=20Marohni=C4=87?= <mislav.marohnic@gmail.com>
Date: Thu, 28 Aug 2008 18:36:46 +0200
Subject: [PATCH] start reducing the number of Ruby warnings when run with RUBYOPT="-w"

---
 bin/haml                    |    3 ++-
 lib/haml/exec.rb            |    1 -
 lib/haml/helpers.rb         |    2 +-
 lib/haml/precompiler.rb     |    6 ++++--
 lib/sass/engine.rb          |    2 +-
 test/haml/engine_test.rb    |    6 +++---
 test/haml/html2haml_test.rb |    3 ++-
 7 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/bin/haml b/bin/haml
index 3480fb9..7777c9f 100755
--- a/bin/haml
+++ b/bin/haml
@@ -1,7 +1,8 @@
 #!/usr/bin/env ruby
 # The command line Haml parser.
 
-require File.dirname(__FILE__) + '/../lib/haml'
+$:.unshift File.dirname(__FILE__) + '/../lib'
+require 'haml'
 require 'haml/exec'
 
 opts = Haml::Exec::Haml.new(ARGV)
diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb
index f6e86a6..5692d3f 100644
--- a/lib/haml/exec.rb
+++ b/lib/haml/exec.rb
@@ -1,4 +1,3 @@
-require File.dirname(__FILE__) + '/../haml'
 require 'optparse'
 require 'fileutils'
 
diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb
index 190f3f6..40ca952 100644
--- a/lib/haml/helpers.rb
+++ b/lib/haml/helpers.rb
@@ -397,7 +397,7 @@ module Haml
     def capture_haml_with_buffer(local_buffer, *args, &block)
       position = local_buffer.length
 
-      block.call *args
+      block.call(*args)
 
       captured = local_buffer.slice!(position..-1)
 
diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb
index 18bf187..8902284 100644
--- a/lib/haml/precompiler.rb
+++ b/lib/haml/precompiler.rb
@@ -136,6 +136,8 @@ END
     end
 
     def precompile
+      @haml_comment = @dont_indent_next_line = @dont_tab_up_next_text = false
+      @indentation = nil
       @line = next_line
       resolve_newlines
       newline
@@ -733,7 +735,7 @@ END
 
     def handle_multiline(line)
       if is_multiline?(line.text)
-        line.text.slice! -1
+        line.text.slice!(-1)
         while new_line = raw_next_line.first
           newline and next if new_line.strip.empty?
           break unless is_multiline?(new_line.strip)
@@ -772,7 +774,7 @@ END
     end
 
     def balance(*args)
-      res = Haml::Shared.balance *args
+      res = Haml::Shared.balance(*args)
       return res if res
       raise SyntaxError.new("Unbalanced brackets.")
     end
diff --git a/lib/sass/engine.rb b/lib/sass/engine.rb
index 06dd8f3..c3195ee 100644
--- a/lib/sass/engine.rb
+++ b/lib/sass/engine.rb
@@ -445,7 +445,7 @@ END
     end
 
     def balance(*args)
-      res = Haml::Shared.balance *args
+      res = Haml::Shared.balance(*args)
       return res if res
       raise SyntaxError.new("Unbalanced brackets.", @line)
     end
diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb
index 5413b6b..a9f18d3 100644
--- a/test/haml/engine_test.rb
+++ b/test/haml/engine_test.rb
@@ -293,14 +293,14 @@ SOURCE
       render("\n\n = abc", :filename => 'test', :line => 2)
     rescue Exception => e
       assert_kind_of Haml::SyntaxError, e
-      assert_match /test:4/, e.backtrace.first
+      assert_match(/test:4/, e.backtrace.first)
     end
 
     begin
       render("\n\n= 123\n\n= nil[]", :filename => 'test', :line => 2)
     rescue Exception => e
       assert_kind_of NoMethodError, e
-      assert_match /test:6/, e.backtrace.first
+      assert_match(/test:6/, e.backtrace.first)
     end
   end
 
@@ -615,7 +615,7 @@ END
 
   # because anything before the doctype triggers quirks mode in IE
   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
 
   # HTML5
diff --git a/test/haml/html2haml_test.rb b/test/haml/html2haml_test.rb
index 3746fde..0132e12 100644
--- a/test/haml/html2haml_test.rb
+++ b/test/haml/html2haml_test.rb
@@ -52,7 +52,8 @@ class Html2HamlTest < Test::Unit::TestCase
     end
 
     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
     end
 end
-- 
1.6.0


From 36295fb8638e45090a56dbfeba7ef8d979b913b1 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Mislav=20Marohni=C4=87?= <mislav.marohnic@gmail.com>
Date: Thu, 28 Aug 2008 19:07:40 +0200
Subject: [PATCH] more silencing of warnings

---
 lib/haml/engine.rb          |    2 +-
 lib/haml/filters.rb         |    1 +
 lib/sass/constant/number.rb |    2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb
index 5141b37..185dfbf 100644
--- a/lib/haml/engine.rb
+++ b/lib/haml/engine.rb
@@ -73,6 +73,7 @@ module Haml
         :escape_html => false
       }
       @options.merge! options
+      @index = 0
 
       unless [:xhtml, :html4, :html5].include?(@options[:format])
         raise Haml::Error, "Invalid format #{@options[:format].inspect}"
@@ -83,7 +84,6 @@ module Haml
       @to_close_stack = []
       @output_tabs = 0
       @template_tabs = 0
-      @index = 0
       @flat = false
       @newlines = 0
       @precompiled = ''
diff --git a/lib/haml/filters.rb b/lib/haml/filters.rb
index 131c13b..ea83632 100644
--- a/lib/haml/filters.rb
+++ b/lib/haml/filters.rb
@@ -32,6 +32,7 @@ module Haml
       def self.included(base) # :nodoc:
         Filters.defined[base.name.split("::").last.downcase] = base
         base.extend(base)
+        base.instance_variable_set "@lazy_requires", nil
       end
 
       # Takes a string, the source text that should be passed to the filter,
diff --git a/lib/sass/constant/number.rb b/lib/sass/constant/number.rb
index d2c3ce4..de90a94 100644
--- a/lib/sass/constant/number.rb
+++ b/lib/sass/constant/number.rb
@@ -8,7 +8,7 @@ module Sass::Constant
     def parse(value)
       first, second, unit = value.scan(Literal::NUMBER)[0]
       @value = first.empty? ? second.to_i : "#{first}#{second}".to_f
-      @unit = unit unless unit.empty?
+      @unit = unit.empty? ? nil : unit
     end
 
     def plus(other)
-- 
1.6.0


From dae2f472de3d670524fed977f687c45024095ddb Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Mislav=20Marohni=C4=87?= <mislav.marohnic@gmail.com>
Date: Thu, 28 Aug 2008 21:03:43 +0200
Subject: [PATCH] get rid of more Ruby warnings, most of which were related to "@haml_buffer" not being initialized

---
 bin/haml                 |    2 +-
 lib/haml/engine.rb       |    4 ++-
 lib/haml/helpers.rb      |    3 +-
 lib/haml/precompiler.rb  |    3 +-
 test/haml/engine_test.rb |   67 +++++++++++++++++++++++++--------------------
 test/sass/plugin_test.rb |    1 +
 test/test_helper.rb      |   22 +++++++++++++-
 7 files changed, 66 insertions(+), 36 deletions(-)

diff --git a/bin/haml b/bin/haml
index 7777c9f..1a24e4e 100755
--- a/bin/haml
+++ b/bin/haml
@@ -1,7 +1,7 @@
 #!/usr/bin/env ruby
 # The command line Haml parser.
 
-$:.unshift File.dirname(__FILE__) + '/../lib'
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
 require 'haml'
 require 'haml/exec'
 
diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb
index 185dfbf..3b1794e 100644
--- a/lib/haml/engine.rb
+++ b/lib/haml/engine.rb
@@ -140,7 +140,9 @@ END
     # but if you're relying on local variables defined in the context of scope,
     # they won't work.
     def render(scope = Object.new, locals = {}, &block)
-      buffer = Haml::Buffer.new(scope.instance_variable_get('@haml_buffer'), options_for_buffer)
+      scope_buffer = scope.instance_variables.include?('@haml_buffer') ?
+        scope.instance_variable_get('@haml_buffer') : nil
+      buffer = Haml::Buffer.new(scope_buffer, options_for_buffer)
 
       if scope.is_a?(Binding) || scope.is_a?(Proc)
         scope_object = eval("self", scope)
diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb
index 40ca952..1f434e8 100644
--- a/lib/haml/helpers.rb
+++ b/lib/haml/helpers.rb
@@ -37,7 +37,8 @@ module Haml
     #   context.haml_tag :p, "Stuff"
     #
     def init_haml_helpers
-      @haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer))
+      upper_buffer = instance_variables.include?('@haml_buffer') ? @haml_buffer : nil
+      @haml_buffer = Haml::Buffer.new(upper_buffer, Haml::Engine.new('').send(:options_for_buffer))
       nil
     end
 
diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb
index 8902284..a1a5d44 100644
--- a/lib/haml/precompiler.rb
+++ b/lib/haml/precompiler.rb
@@ -88,7 +88,8 @@ module Haml
     def precompiled_with_ambles(local_names)
       preamble = <<END.gsub("\n", ";")
 extend Haml::Helpers
-_hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect})
+upper_buffer = instance_variables.include?('@haml_buffer') ? @haml_buffer : nil
+_hamlout = @haml_buffer = Haml::Buffer.new(upper_buffer, #{options_for_buffer.inspect})
 _erbout = _hamlout.buffer
 END
       postamble = <<END.gsub("\n", ";")
diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb
index a9f18d3..e4be03b 100644
--- a/test/haml/engine_test.rb
+++ b/test/haml/engine_test.rb
@@ -2,7 +2,7 @@
 require File.dirname(__FILE__) + '/../test_helper'
 
 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;
   # 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.
@@ -34,13 +34,7 @@ class EngineTest < Test::Unit::TestCase
     "%p{:a => 'b',\n:c => 'd',\n:e => raise('foo')}" => ["foo", 3],
     " %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,
-You don't need to use "- end" in Haml. Use indentation instead:
-- if foo?
-  %strong Foo!
-- else
-  Not foo.
-END
+    "- end" => "You don't need to use \"- end\" in Haml. Use indentation instead:\n- if foo?\n  %strong Foo!\n- else\n  Not foo.",
     " \n\t\n %p foo" => ["Indenting at the beginning of the document is illegal.", 3],
     "\n\n %p foo" => ["Indenting at the beginning of the document is illegal.", 3],
     "%p\n  foo\n foo" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 3],
@@ -71,9 +65,19 @@ END
   def render(text, options = {}, &block)
     scope  = options.delete(:scope)  || Object.new
     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
+  
   def test_flexible_tabulation
     assert_equal("<p>\n  foo\n</p>\n<q>\n  bar\n  <a>\n    baz\n  </a>\n</q>\n",
                  render("%p\n foo\n%q\n bar\n %a\n  baz"))
@@ -115,7 +119,7 @@ END
   end
 
   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)
@@ -387,12 +391,17 @@ SOURCE
   def test_exceptions
     EXCEPTION_MAP.each do |key, value|
       begin
-        render(key)
+        capture_stream(:stderr) {
+          render(key, :filename => "(exception test for #{key.inspect})")
+        }
       rescue Exception => err
         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(value[1] || key.split("\n").length, err.backtrace[0].gsub('(haml):', '').to_i, "Line: #{key}")
+        assert_equal(expected_message, err.message, "Line: #{key}")
+        assert_equal(line_no, line_reported, "Line: #{key}")
       else
         assert(false, "Exception not raised for\n#{key}")
       end
@@ -402,7 +411,7 @@ SOURCE
   def test_exception_line
     render("a\nb\n!!!\n  c\nd")
   rescue Haml::SyntaxError => e
-    assert_equal("(haml):4", e.backtrace[0])
+    assert_equal("(test_exception_line):4", e.backtrace[0])
   else
     assert(false, '"a\nb\n!!!\n  c\nd" doesn\'t produce an exception')
   end
@@ -410,7 +419,7 @@ SOURCE
   def test_exception
     render("%p\n  hi\n  %a= undefined\n= 12")
   rescue Exception => e
-    assert_match("(haml):3", e.backtrace[0])
+    assert_match("(test_exception):3", e.backtrace[0])
   else
     # Test failed... should have raised an exception
     assert(false)
@@ -419,7 +428,7 @@ SOURCE
   def test_compile_error
     render("a\nb\n- fee)\nc")
   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
     assert(false,
            '"a\nb\n- fee)\nc" doesn\'t produce an exception!')
@@ -446,12 +455,11 @@ SOURCE
       alias_method :gem_original_require, :gem_original_require_with_bluecloth
     end
     
-    e = assert_raise(Haml::Error) {
-      Haml::Engine.new(":markdown\n  Foo").to_html
-    }
+    e = assert_raise(Haml::Error) { render ":markdown\n  Foo" }
     assert_equal "Can't run Markdown filter; required file 'bluecloth' not found", e.message
 
     Kernel.module_eval do
+      undef :gem_original_require
       alias_method :gem_original_require, :gem_original_require_without_bluecloth
     end
   end
@@ -466,18 +474,17 @@ SOURCE
       alias_method :gem_original_require, :gem_original_require_with_redcloth
     end
 
-    e = assert_raise(Haml::Error) {
-      Haml::Engine.new(":redcloth\n  _foo_").to_html
-    }
+    e = assert_raise(Haml::Error) { render ":redcloth\n  _foo_" }
     assert_equal "Can't run RedCloth filter; required file 'redcloth' not found", e.message
 
     Kernel.module_eval do
+      undef :gem_original_require
       alias_method :gem_original_require, :gem_original_require_without_redcloth
     end
   end
   
   def test_redcloth_gets_run
-    assert_equal "<p><em>foo</em></p>\n", Haml::Engine.new(":redcloth\n  _foo_").to_html
+    assert_equal "<p><em>foo</em></p>\n", render(":redcloth\n  _foo_")
   end
 
   def test_empty_filter
@@ -538,28 +545,28 @@ END
 
   def test_yield_should_work_with_def_method
     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 })
   end
 
   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 })
   end
 
   def test_def_method_locals
     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))
   end
 
   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])
   end
 
   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
 
   def test_ugly_true
@@ -586,7 +593,7 @@ END
   end
 
   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
 
   # HTML 4.0
diff --git a/test/sass/plugin_test.rb b/test/sass/plugin_test.rb
index ef02566..2ae1551 100644
--- a/test/sass/plugin_test.rb
+++ b/test/sass/plugin_test.rb
@@ -198,5 +198,6 @@ class Sass::Engine
 end
 
 class ActionController::Base
+  undef :sass_old_process
   def sass_old_process(*args); end
 end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 94af86f..ae4a27a 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,3 +1,4 @@
+lib_dir = File.dirname(__FILE__) + '/../lib'
 # allows testing with edge Rails by creating a test/rails symlink
 linked_rails = File.dirname(__FILE__) + '/rails'
 
@@ -12,5 +13,22 @@ require 'action_controller'
 require 'action_view'
 
 require 'test/unit'
-require File.dirname(__FILE__) + '/../lib/haml'
-require File.dirname(__FILE__) + '/../lib/sass'
+$:.unshift lib_dir unless $:.include?(lib_dir)
+require 'haml'
+require 'sass'
+
+unless respond_to?(:capture_stream)
+  require 'stringio'
+
+  def capture_stream(stream)
+    begin
+      stream = stream.to_s
+      eval "$#{stream} = StringIO.new"
+      yield
+      result = eval("$#{stream}").string
+    ensure 
+      eval("$#{stream} = #{stream.upcase}")
+    end
+    result
+  end
+end
\ No newline at end of file
-- 
1.6.0

