Debian Codegraph @

test/parser_test.rb

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