mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
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
This commit is contained in:
parent
ff323de099
commit
ad712a6d7e
5 changed files with 75 additions and 30 deletions
2
Rakefile
2
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
|
||||
|
|
6
init.rb
6
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)
|
||||
ActionView::Base.register_template_handler("haml", HAML::Engine)
|
|
@ -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}</#{name.to_s}>"
|
||||
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 "</#{name = @to_close_queue.pop}>"
|
||||
end
|
||||
|
||||
|
||||
def render_div(line)
|
||||
render_tag("%div" + line)
|
||||
end
|
||||
|
||||
|
||||
def render_comment(line)
|
||||
add "<!-- #{line} -->"
|
||||
add "<!-- #{line[1..line.length].strip} -->"
|
||||
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)
|
|
@ -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("<div class='author'>Hampton Catlin</div>\n", @engine.render(".author= @content_for_layout + ' ' + @last_name"))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue