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

100% Rcov! Woo-hoo! Yeah!

git-svn-id: svn://hamptoncatlin.com/haml/trunk@325 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-01-31 06:38:23 +00:00
parent 79d7fef620
commit a94b40953a
7 changed files with 110 additions and 26 deletions

4
TODO
View file

@ -3,9 +3,9 @@ Testing:
Documentation:
! Document Sass module
Haml::Engine public method info could use work
Haml::Engine public method documentation could use work
Features:
Sass needs to throw exceptions
Sass should throw generic errors for undefined constants, not syntax errors
There should be a way to represent options in-document
Haml and Sass executables should return meaningful exit codes

View file

@ -125,7 +125,8 @@ module Haml
'textile' => Haml::Filters::Textile,
'markdown' => Haml::Filters::Markdown
})
elsif !NOT_LOADED.include? 'bluecloth'
end
if !NOT_LOADED.include? 'bluecloth'
@options[:filters]['markdown'] = Haml::Filters::Markdown
end
@ -147,7 +148,7 @@ module Haml
# Only do the first round of pre-compiling if we really need to.
# They might be passing in the precompiled string.
do_precompile if @precompiled.nil? && (@precompiled = String.new)
rescue Haml::SyntaxError => e
rescue Haml::Error => e
e.add_backtrace_entry(@index, @options[:filename])
raise e
end
@ -519,9 +520,9 @@ module Haml
@flat_spaces = -1
if filter.is_a? String
if filter == 'redcloth' || filter == 'markdown' || filter == 'textile'
push_text("You must have the RedCloth gem installed to use #{filter}")
raise HamlError.new("You must have the RedCloth gem installed to use #{filter}")
else
push_text("Filter \"#{filter}\" is not defined!")
raise HamlError.new("Filter \"#{filter}\" is not defined!")
end
else
push_text(filter.new(@filter_buffer).render.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))

View file

@ -28,10 +28,16 @@ module Haml
end
end
# SyntaxError is the type of exception thrown when Haml encounters an
# SyntaxError is the type of exception raised when Haml encounters an
# ill-formatted document.
# It's not particularly interesting, except in that it includes Haml::Error.
class SyntaxError < StandardError
include Haml::Error
end
# HamlError is the type of exception raised when Haml encounters an error
# not of a syntactical nature, such as an undefined Filter.
class HamlError < StandardError
include Haml::Error
end
end

View file

@ -8,7 +8,7 @@ require 'sass/engine'
require 'stringio'
volatile_requires = ['rubygems', 'redcloth', 'bluecloth']
NOT_LOADED = []
NOT_LOADED = [] unless defined?(NOT_LOADED)
volatile_requires.each do |file|
begin
require file
@ -50,6 +50,10 @@ module Haml
end
end
unless NOT_LOADED.include? 'bluecloth'
Markdown = BlueCloth unless defined?(Markdown)
end
unless NOT_LOADED.include? 'redcloth'
class ::RedCloth; alias_method :render, :to_html; end
@ -60,15 +64,13 @@ module Haml
end
end
unless defined?(BlueCloth)
unless defined?(Markdown)
# Uses RedCloth to provide only Markdown (not Textile) parsing
class Markdown < RedCloth
def render
self.to_html(:markdown)
end
end
else
Markdown = BlueCloth
end
end
end

View file

@ -45,18 +45,23 @@ module Sass
# Processes the template and returns the result as a string.
def render
split_lines
begin
split_lines
root = Tree::Node.new
index = 0
while @lines[index]
child, index = build_tree(index)
child.line = index if child
root << child if child
end
@line = nil
root = Tree::Node.new
index = 0
while @lines[index]
child, index = build_tree(index)
child.line = index if child
root << child if child
end
@line = nil
root.to_s
root.to_s
rescue SyntaxError => err
err.add_backtrace_entry
raise err
end
end
private
@ -75,7 +80,7 @@ module Sass
if tabs # if line isn't blank
if tabs - old_tabs > 1
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.")
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line)
end
@lines << [line.strip, tabs]
@ -91,7 +96,7 @@ module Sass
spaces = line.index(/[^ ]/)
if spaces
if spaces % 2 == 1 || line[spaces] == ?\t
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.")
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line)
end
spaces / 2
else

View file

@ -16,7 +16,7 @@ module Sass
result = String.new
children.each do |child|
if child.is_a? AttrNode
raise SyntaxError.new('Attributes aren\'t allowed at the root of a document.', @line)
raise SyntaxError.new('Attributes aren\'t allowed at the root of a document.', child.line)
end
result += "#{child.to_s}\n"

View file

@ -122,13 +122,14 @@ class EngineTest < Test::Unit::TestCase
errs = [ "!!!\n a", "a\n b", "a\n:foo\nb", "/ a\n b",
"% a", "%p a\n b", "a\n%p=\nb", "%p=\n a",
"a\n%p~\nb", "a\n~\nb", "%p/\n a", "%p\n \t%a b",
"%a\n b\nc", "%a\n b\nc"
"%a\n b\nc", "%a\n b\nc",
":notafilter\n This isn't\n a filter!",
]
errs.each do |err|
begin
render(err)
rescue Exception => e
assert(e.is_a?(Haml::SyntaxError),
assert(e.is_a?(Haml::Error),
"#{err.dump} doesn't produce Haml::SyntaxError!")
else
assert(false,
@ -147,4 +148,73 @@ class EngineTest < Test::Unit::TestCase
'"a\nb\n- fee do\nc" doesn\'t produce an exception!')
end
end
def test_no_bluecloth
old_markdown = false
if defined?(Haml::Filters::Markdown)
old_markdown = Haml::Filters::Markdown
end
Kernel.module_eval do
alias_method :haml_old_require, :gem_original_require
def gem_original_require(file)
raise LoadError if file == 'bluecloth'
haml_old_require(file)
end
end
if old_markdown
Haml::Filters.instance_eval do
remove_const 'Markdown'
end
end
# This is purposefully redundant, so it doesn't stop
# haml/filters from being required later on.
require 'haml/../haml/filters'
assert_equal("<h1>Foo</h1>\t<p>- a\n- b</p>\n",
Haml::Engine.new(":markdown\n Foo\n ===\n - a\n - b").to_html)
Haml::Filters.instance_eval do
remove_const 'Markdown'
end
Haml::Filters.const_set('Markdown', old_markdown) if old_markdown
Kernel.module_eval do
alias_method :gem_original_require, :haml_old_require
end
NOT_LOADED.delete 'bluecloth'
end
def test_no_redcloth
Kernel.module_eval do
alias_method :haml_old_require2, :gem_original_require
def gem_original_require(file)
raise LoadError if file == 'redcloth'
haml_old_require2(file)
end
end
# This is purposefully redundant, so it doesn't stop
# haml/filters from being required later on.
require 'haml/../haml/../haml/filters'
begin
Haml::Engine.new(":redcloth\n _foo_").to_html
rescue Haml::HamlError
else
assert(false, "No exception raised!")
end
Kernel.module_eval do
alias_method :gem_original_require2, :haml_old_require
end
NOT_LOADED.delete 'redcloth'
end
end