From ad712a6d7e4cd1abb54ba09dc16697df0c70e172 Mon Sep 17 00:00:00 2001 From: hcatlin Date: Sun, 6 Aug 2006 03:18:54 +0000 Subject: [PATCH] Proudly presenting excellent test coverage for HAML. C0 is at 99.0%... all except for one experimental feature. So, now any changes have something to be tested against which should be a fairly strong background. Now, the only problem left is that whitespace active areas get fucked. That's not a good thing. Once we get a philosophical stance on such subjects, then we shall figure crap out about what that all means. git-svn-id: svn://hamptoncatlin.com/haml/trunk@13 7063305b-7217-0410-af8c-cdc13e5119b9 --- Rakefile | 2 + init.rb | 6 +-- lib/{haml.rb => haml/engine.rb} | 51 +++++++++++++----------- lib/{haml_helpers.rb => haml/helpers.rb} | 0 test/haml_test.rb | 46 +++++++++++++++++++-- 5 files changed, 75 insertions(+), 30 deletions(-) rename lib/{haml.rb => haml/engine.rb} (94%) rename lib/{haml_helpers.rb => haml/helpers.rb} (100%) diff --git a/Rakefile b/Rakefile index 7ac8911f..c16cfbbb 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,8 @@ require 'rake' require 'rake/testtask' require 'rake/rdoctask' +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + desc 'Default: run unit tests.' task :default => :test diff --git a/init.rb b/init.rb index f8fc22d0..9e43d00e 100644 --- a/init.rb +++ b/init.rb @@ -1,4 +1,4 @@ -require 'haml' -require 'haml_helpers' +require 'haml/engine' +require 'haml/helpers' -ActionView::Base.register_template_handler("haml", HAML::TemplateEngine) \ No newline at end of file +ActionView::Base.register_template_handler("haml", HAML::Engine) \ No newline at end of file diff --git a/lib/haml.rb b/lib/haml/engine.rb similarity index 94% rename from lib/haml.rb rename to lib/haml/engine.rb index 328ead83..c8ad966f 100644 --- a/lib/haml.rb +++ b/lib/haml/engine.rb @@ -1,6 +1,10 @@ + +require File.dirname(__FILE__) + '/helpers' + module HAML - class TemplateEngine + class Engine + attr_accessor :base include HAMLHelpers @@ -13,7 +17,7 @@ module HAML @tab_index << @tab_index.last + " " end end - + def render(template = "", locals = {}) @result = "" @to_close_queue = [] @@ -22,11 +26,9 @@ module HAML @base.assigns.each do |key,value| @base.instance_eval("@#{key} = value") end - + @happy_land.set_locals(locals) - - #breakpoint - + #main loop handling line reading #and interpretation template.each_line do |line| @@ -48,25 +50,25 @@ module HAML when '=' add template_eval(line[1, line.length]) else - add line + add line.strip end end end - + @to_close_queue.length.times { close_tag } @result end def add(line) return nil if line.nil? - line.each_line { |me| add_single(me) } + line.to_s.each_line { |me| add_single(me) } end - + def add_single(line = "") @result << @tab_index[@to_close_queue.size] @result << line.chomp + "\n" end - + def open_tag(name, attributes = {}) add "<#{name.to_s}#{build_attributes(attributes)}>" @to_close_queue.push(name) @@ -75,7 +77,7 @@ module HAML def one_line_tag(name, value, attributes = {}) add "<#{name.to_s}#{build_attributes(attributes)}>#{value}" end - + def print_tag(name, value, attributes = {}) unless value.empty? if one_liner?(value) @@ -100,19 +102,19 @@ module HAML return "" if attributes.empty? " " + (attributes.collect { |attr_name, val| attr_name.to_s + "='" + val.to_s + "'" }).join(" ") end - + def close_tag add "" end - + def render_div(line) render_tag("%div" + line) end - + def render_comment(line) - add "" + add "" end - + def render_tag(line) broken_up = line.scan(/[%]([-_a-z1-9]+)([-_a-z\.\#]*)(\{.*\})?([=\/\~]?)?(.*)?/) broken_up.each do |tag_name, attributes, attributes_hash, action, value| @@ -130,11 +132,10 @@ module HAML value = template_eval(value) one_line_tag(tag_name, value.to_s, attributes) if value != false else - print_tag(tag_name, value, attributes) + print_tag(tag_name, value.to_s.strip, attributes) end end end - def parse_attributes(list) attributes = {} @@ -148,24 +149,26 @@ module HAML end attributes end - + def count_levels(line) [line.index(/[^ ]/)/2, line.strip] end - + def one_liner?(value) ((value.length < 50) && value.scan(/\n/).empty?) end - + def template_eval(code) #@base.instance_eval(code) #render :inline => "<%=#{code}%>" @happy_land.instance_eval(code) end - + end - + class HappyLand #:nodoc + include HAMLHelpers + def initialize(base, hash_of_assigns, hash_of_locals = {}) base.instance_variables.each do |key| value = base.instance_eval(key) diff --git a/lib/haml_helpers.rb b/lib/haml/helpers.rb similarity index 100% rename from lib/haml_helpers.rb rename to lib/haml/helpers.rb diff --git a/test/haml_test.rb b/test/haml_test.rb index 1e3cd672..1dd3c00e 100644 --- a/test/haml_test.rb +++ b/test/haml_test.rb @@ -1,8 +1,48 @@ require 'test/unit' +require File.dirname(__FILE__) + '/../lib/haml/engine' +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") +require 'rubygems' +require 'action_view' class HamlTest < Test::Unit::TestCase - # Replace this with your real tests. - def test_this_plugin - flunk + def setup + ActionView::Base.register_template_handler("haml", HAML::Engine) + @base = ActionView::Base.new(File.dirname(__FILE__) + "/../test/templates/") + @engine = HAML::Engine.new(@base) end + + def load_result(name) + @result = "" + File.new(File.dirname(__FILE__) + "/results/" + name + ".xhtml").each_line { |l| @result += l} + @result + end + + def assert_renders_correctly(name) + assert_equal(load_result(name), @base.render(name)) + end + + # Make sure our little environment builds + def test_build_stub + assert_not_nil(@engine) + assert_equal(HAML::Engine, @engine.class) + end + + def test_empty_render + assert_equal("", @engine.render("")) + end + + def test_renderings + assert_renders_correctly("very_basic") + assert_renders_correctly("standard") + assert_renders_correctly("helpers") + end + + def test_instance_variables + @base.instance_eval("@content_for_layout = 'Hampton'") + @base.instance_eval("@assigns['last_name'] = 'Catlin'") + #make sure to reload! + @engine = HAML::Engine.new(@base) + assert_equal("
Hampton Catlin
\n", @engine.render(".author= @content_for_layout + ' ' + @last_name")) + end + end