2012-06-25 17:13:16 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
module Haml
|
2013-05-23 08:41:27 -04:00
|
|
|
class ParserTest < MiniTest::Unit::TestCase
|
2012-06-25 17:13:16 -04:00
|
|
|
|
|
|
|
test "should raise error for 'else' at wrong indent level" do
|
|
|
|
begin
|
|
|
|
parse("- if true\n #first\n text\n - else\n #second")
|
2012-06-27 14:16:03 -04:00
|
|
|
flunk("Should have raised a Haml::SyntaxError")
|
|
|
|
rescue SyntaxError => e
|
2012-06-25 17:13:16 -04:00
|
|
|
assert_equal Error.message(:bad_script_indent, 'else', 0, 1), e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should raise error for 'elsif' at wrong indent level" do
|
|
|
|
begin
|
|
|
|
parse("- if true\n #first\n text\n - elsif false\n #second")
|
2012-06-27 14:16:03 -04:00
|
|
|
flunk("Should have raised a Haml::SyntaxError")
|
|
|
|
rescue SyntaxError => e
|
2012-06-25 17:13:16 -04:00
|
|
|
assert_equal Error.message(:bad_script_indent, 'elsif', 0, 1), e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-26 18:07:53 -04:00
|
|
|
test "should raise error for 'else' at wrong indent level after unless" do
|
|
|
|
begin
|
|
|
|
parse("- unless true\n #first\n text\n - else\n #second")
|
2012-06-27 14:16:03 -04:00
|
|
|
flunk("Should have raised a Haml::SyntaxError")
|
|
|
|
rescue SyntaxError => e
|
2012-06-26 18:07:53 -04:00
|
|
|
assert_equal Error.message(:bad_script_indent, 'else', 0, 1), e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 14:16:03 -04:00
|
|
|
test "should raise syntax error for else with no if" do
|
|
|
|
begin
|
|
|
|
parse("- else\n 'foo'")
|
|
|
|
flunk("Should have raised a Haml::SyntaxError")
|
|
|
|
rescue SyntaxError => e
|
|
|
|
assert_equal Error.message(:missing_if, 'else'), e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should raise syntax error for nested else with no" do
|
|
|
|
begin
|
|
|
|
parse("#foo\n - else\n 'foo'")
|
|
|
|
flunk("Should have raised a Haml::SyntaxError")
|
|
|
|
rescue SyntaxError => e
|
|
|
|
assert_equal Error.message(:missing_if, 'else'), e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-13 18:58:32 -04:00
|
|
|
test "else after if containing case is accepted" do
|
|
|
|
# see issue 572
|
|
|
|
begin
|
|
|
|
parse "- if true\n - case @foo\n - when 1\n bar\n- else\n bar"
|
|
|
|
assert true
|
|
|
|
rescue SyntaxError
|
|
|
|
flunk 'else clause after if containing case should be accepted'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "else after if containing unless is accepted" do
|
|
|
|
begin
|
|
|
|
parse "- if true\n - unless @foo\n bar\n- else\n bar"
|
|
|
|
assert true
|
|
|
|
rescue SyntaxError
|
|
|
|
flunk 'else clause after if containing unless should be accepted'
|
|
|
|
end
|
|
|
|
end
|
2012-08-10 14:43:23 -04:00
|
|
|
|
|
|
|
test "loud script with else is accepted" do
|
|
|
|
begin
|
|
|
|
parse "= if true\n - 'A'\n-else\n - 'B'"
|
|
|
|
assert true
|
|
|
|
rescue SyntaxError
|
|
|
|
flunk 'loud script (=) should allow else'
|
|
|
|
end
|
|
|
|
end
|
2012-07-13 18:58:32 -04:00
|
|
|
|
2012-08-14 19:13:28 -04:00
|
|
|
test "else after nested loud script is accepted" do
|
|
|
|
begin
|
|
|
|
parse "-if true\n =if true\n - 'A'\n-else\n B"
|
|
|
|
assert true
|
|
|
|
rescue SyntaxError
|
|
|
|
flunk 'else after nested loud script should be accepted'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-16 20:25:08 -04:00
|
|
|
test "case with indented whens should allow else" do
|
|
|
|
begin
|
|
|
|
parse "- foo = 1\n-case foo\n -when 1\n A\n -else\n B"
|
|
|
|
assert true
|
|
|
|
rescue SyntaxError
|
|
|
|
flunk 'case with indented whens should allow else'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-17 15:26:08 -04:00
|
|
|
test "revealed conditional comments are detected" do
|
|
|
|
text = "some revealed text"
|
|
|
|
cond = "[cond]"
|
|
|
|
|
|
|
|
node = parse("/!#{cond} #{text}").children[0]
|
|
|
|
|
|
|
|
assert_equal text, node.value[:text]
|
|
|
|
assert_equal cond, node.value[:conditional]
|
|
|
|
assert node.value[:revealed]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "hidden conditional comments are detected" do
|
|
|
|
text = "some revealed text"
|
|
|
|
cond = "[cond]"
|
|
|
|
|
|
|
|
node = parse("/#{cond} #{text}").children[0]
|
|
|
|
|
|
|
|
assert_equal text, node.value[:text]
|
|
|
|
assert_equal cond, node.value[:conditional]
|
|
|
|
refute node.value[:revealed]
|
|
|
|
end
|
|
|
|
|
2012-06-25 17:13:16 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def parse(haml, options = nil)
|
|
|
|
options ||= Options.new
|
|
|
|
parser = Parser.new(haml, options)
|
|
|
|
parser.parse
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|