1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
require_relative 'helper'
class ParserTest < Minitest::Test
def test_parser_extension
parser = Mustache::Parser.new
parser.instance_variable_set :@result, 'zomg'
Mustache::Parser.add_type(:'@', :'$') do |*args|
[:mustache, :at_sign, @result, *args]
end
assert_match Mustache::Parser.valid_types, '@'
assert_match Mustache::Parser.valid_types, '$'
assert_equal [:mustache, :at_sign, 'zomg', 1, 2, 3],
parser.send('scan_tag_@', 1, 2, 3)
assert_equal [:mustache, :at_sign, 'zomg', 1, 2, 3],
parser.send('scan_tag_$', 1, 2, 3)
end
def test_parser
lexer = Mustache::Parser.new
tokens = lexer.compile(<<-EOF)
<h1>{{header}}</h1>
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/items}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
EOF
expected = [:multi,
[:static, "<h1>"],
[:mustache, :etag, [:mustache, :fetch, ["header"]], [1, 11]],
[:static, "</h1>\n"],
[:mustache,
:section,
[:mustache, :fetch, ["items"]],
[2, 7],
[:multi,
[:mustache,
:section,
[:mustache, :fetch, ["first"]],
[3, 7],
[:multi,
[:static, " <li><strong>"],
[:mustache, :etag, [:mustache, :fetch, ["name"]], [4, 19]],
[:static, "</strong></li>\n"]],
%Q' <li><strong>{{name}}</strong></li>\n',
%w[{{ }}]],
[:mustache,
:section,
[:mustache, :fetch, ["link"]],
[6, 6],
[:multi,
[:static, " <li><a href=\""],
[:mustache, :etag, [:mustache, :fetch, ["url"]], [7, 19]],
[:static, "\">"],
[:mustache, :etag, [:mustache, :fetch, ["name"]], [7, 29]],
[:static, "</a></li>\n"]],
%Q' <li><a href="{{url}}">{{name}}</a></li>\n',
%w[{{ }}]]],
%Q'{{#first}}\n <li><strong>{{name}}</strong></li>\n{{/first}}\n{{#link}}\n <li><a href="{{url}}">{{name}}</a></li>\n{{/link}}\n',
%w[{{ }}]],
[:static, "\n"],
[:mustache,
:section,
[:mustache, :fetch, ["empty"]],
[11, 7],
[:multi, [:static, "<p>The list is empty.</p>\n"]],
%Q'<p>The list is empty.</p>\n',
%w[{{ }}]]]
assert_equal expected, tokens
end
def test_raw_content_and_whitespace
lexer = Mustache::Parser.new
tokens = lexer.compile("{{#list}}\t{{/list}}")
expected = [:multi,
[:mustache,
:section,
[:mustache, :fetch, ["list"]],
[1, 6],
[:multi, [:static, "\t"]],
"\t",
%w[{{ }}]]]
assert_equal expected, tokens
end
def test_unclosed_section
lexer = Mustache::Parser.new
exception = assert_raises Mustache::Parser::SyntaxError do
lexer.compile("{{#list}}")
end
expected = <<-EOF
Unclosed section "list"
Line 1
{{#list}}
^
EOF
assert_equal expected, exception.message
end
def test_closing_unopened
lexer = Mustache::Parser.new
exception = assert_raises Mustache::Parser::SyntaxError do
lexer.compile("{{/list}}")
end
expected = <<-EOF
Closing unopened "list"
Line 1
{{/list}}
^
EOF
assert_equal expected, exception.message
end
def test_unclosed_tag
lexer = Mustache::Parser.new
exception = assert_raises Mustache::Parser::SyntaxError do
lexer.compile("{{list")
end
expected = <<-EOF
Unclosed tag
Line 1
{{list
^
EOF
assert_equal expected, exception.message
end
def test_illegal_content
lexer = Mustache::Parser.new
exception = assert_raises Mustache::Parser::SyntaxError do
lexer.compile("{{")
end
expected = <<-EOF
Illegal content in tag
Line 1
{{
^
EOF
assert_equal expected, exception.message
end
end
|