Skip to content

Commit e5171a5

Browse files
authored
Add basic ERB support to Lexer and refactor Lexer (#20)
* Add ERB support to lexer and refactor lexer * Also support empty attribute values * Fix Text Content test by relying on `lexer_parse_text_content` * Add TODOs for unhandled cases * Revert change to examples/test.html.erb file * Add support for HTML comments * Remove debug printf * Update `minitest-difftastic` to `~> 0.2` * Update bundler * Improve whitespace handling * Add some more ERB tests * Rename `WHITESPACE` to `TOKEN_WHITESPACE`
1 parent e92faf3 commit e5171a5

File tree

9 files changed

+742
-282
lines changed

9 files changed

+742
-282
lines changed

ext/erbx/test/lexer/erb_test.rb

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module Lexer
6+
class ERBTest < Minitest::Spec
7+
test "erb silent" do
8+
result = ERBX.lex(%(<% 'hello world' %>))
9+
10+
expected = %w[
11+
TOKEN_ERB_START
12+
TOKEN_ERB_CONTENT
13+
TOKEN_ERB_END
14+
TOKEN_EOF
15+
]
16+
17+
assert_equal expected, result.array.items.map(&:type)
18+
end
19+
20+
test "erb loud" do
21+
result = ERBX.lex(%(<%= "hello world" %>))
22+
23+
expected = %w[
24+
TOKEN_ERB_START
25+
TOKEN_ERB_CONTENT
26+
TOKEN_ERB_END
27+
TOKEN_EOF
28+
]
29+
30+
assert_equal expected, result.array.items.map(&:type)
31+
end
32+
33+
test "erb <%-" do
34+
result = ERBX.lex(%(<%- "Test" %>))
35+
36+
expected = %w[
37+
TOKEN_ERB_START
38+
TOKEN_ERB_CONTENT
39+
TOKEN_ERB_END
40+
TOKEN_EOF
41+
]
42+
43+
assert_equal expected, result.array.items.map(&:type)
44+
end
45+
46+
test "erb <%- -%>" do
47+
result = ERBX.lex(%(<%- "Test" -%>))
48+
49+
expected = %w[
50+
TOKEN_ERB_START
51+
TOKEN_ERB_CONTENT
52+
TOKEN_ERB_END
53+
TOKEN_EOF
54+
]
55+
56+
assert_equal expected, result.array.items.map(&:type)
57+
end
58+
59+
test "erb <%# %>" do
60+
result = ERBX.lex(%(<%# "Test" %>))
61+
62+
expected = %w[
63+
TOKEN_ERB_START
64+
TOKEN_ERB_CONTENT
65+
TOKEN_ERB_END
66+
TOKEN_EOF
67+
]
68+
69+
assert_equal expected, result.array.items.map(&:type)
70+
end
71+
72+
xtest "erb <%% %%>" do
73+
result = ERBX.lex(%(<%% "Test" %%>))
74+
75+
expected = %w[
76+
TOKEN_ERB_START
77+
TOKEN_ERB_CONTENT
78+
TOKEN_ERB_END
79+
TOKEN_EOF
80+
]
81+
82+
assert_equal expected, result.array.items.map(&:type)
83+
end
84+
85+
xtest "erb output inside HTML attribute value" do
86+
result = ERBX.lex(%(<article id="<%= dom_id(article) %>"></article>))
87+
88+
expected = %w[
89+
TOKEN_HTML_TAG_START
90+
TOKEN_HTML_TAG_NAME
91+
TOKEN_WHITESPACE
92+
TOKEN_HTML_ATTRIBUTE_NAME
93+
TOKEN_HTML_EQUALS
94+
TOKEN_HTML_QUOTE
95+
TOKEN_ERB_START
96+
TOKEN_ERB_CONTENT
97+
TOKEN_ERB_END
98+
TOKEN_HTML_QUOTE
99+
TOKEN_HTML_TAG_END
100+
TOKEN_HTML_CLOSE_TAG_START
101+
TOKEN_HTML_TAG_NAME
102+
TOKEN_ERB_END
103+
TOKEN_EOF
104+
]
105+
106+
assert_equal expected, result.array.items.map(&:type)
107+
end
108+
109+
xtest "erb output inside HTML attribute value with value before" do
110+
result = ERBX.lex(%(<div class="bg-black <%= "text-white" %>"></div>))
111+
112+
expected = %w[
113+
TOKEN_HTML_TAG_START
114+
TOKEN_HTML_TAG_NAME
115+
TOKEN_WHITESPACE
116+
TOKEN_HTML_ATTRIBUTE_NAME
117+
TOKEN_HTML_EQUALS
118+
TOKEN_HTML_QUOTE
119+
TOKEN_HTML_ATTRIBUTE_VALUE
120+
TOKEN_ERB_START
121+
TOKEN_ERB_CONTENT
122+
TOKEN_ERB_END
123+
TOKEN_HTML_QUOTE
124+
TOKEN_HTML_TAG_END
125+
TOKEN_HTML_CLOSE_TAG_START
126+
TOKEN_HTML_TAG_NAME
127+
TOKEN_ERB_END
128+
TOKEN_EOF
129+
]
130+
131+
assert_equal expected, result.array.items.map(&:type)
132+
end
133+
134+
xtest "erb output inside HTML attribute value with value before and after" do
135+
result = ERBX.lex(%(<div class="bg-black <%= "text-white" %>"></div>))
136+
137+
expected = %w[
138+
TOKEN_HTML_TAG_START
139+
TOKEN_HTML_TAG_NAME
140+
TOKEN_WHITESPACE
141+
TOKEN_HTML_ATTRIBUTE_NAME
142+
TOKEN_HTML_EQUALS
143+
TOKEN_HTML_QUOTE
144+
TOKEN_HTML_ATTRIBUTE_VALUE
145+
TOKEN_ERB_START
146+
TOKEN_ERB_CONTENT
147+
TOKEN_ERB_END
148+
TOKEN_HTML_ATTRIBUTE_VALUE
149+
TOKEN_HTML_QUOTE
150+
TOKEN_HTML_TAG_END
151+
TOKEN_HTML_CLOSE_TAG_START
152+
TOKEN_HTML_TAG_NAME
153+
TOKEN_ERB_END
154+
TOKEN_EOF
155+
]
156+
157+
assert_equal expected, result.array.items.map(&:type)
158+
end
159+
160+
xtest "erb output inside HTML attribute value with value and after" do
161+
result = ERBX.lex(%(<div class="bg-black <%= "text-white" %>"></div>))
162+
163+
expected = %w[
164+
TOKEN_HTML_TAG_START
165+
TOKEN_HTML_TAG_NAME
166+
TOKEN_WHITESPACE
167+
TOKEN_HTML_ATTRIBUTE_NAME
168+
TOKEN_HTML_EQUALS
169+
TOKEN_HTML_QUOTE
170+
TOKEN_ERB_START
171+
TOKEN_ERB_CONTENT
172+
TOKEN_ERB_END
173+
TOKEN_HTML_ATTRIBUTE_VALUE
174+
TOKEN_HTML_QUOTE
175+
TOKEN_HTML_TAG_END
176+
TOKEN_HTML_CLOSE_TAG_START
177+
TOKEN_HTML_TAG_NAME
178+
TOKEN_ERB_END
179+
TOKEN_EOF
180+
]
181+
182+
assert_equal expected, result.array.items.map(&:type)
183+
end
184+
end
185+
end

0 commit comments

Comments
 (0)