# frozen_string_literal: true
require_relative "../test_helper"
require_relative "../../lib/herb/engine"
module Engine
class EngineErubiCompatTest < Minitest::Spec
include SnapshotUtils
test "handles no tags" do
template = "a\n"
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles basic erb expressions" do
template = <<~ERB
<% i = 0
list.each_with_index do |item, i| %>
<%= i+1 %>
<%== item %>
<% end %>
<%== i+1 %>
ERB
assert_compiled_snapshot(template)
end
test "escapes backslashes and apostrophes in text" do
template = "
\n ' ' \\\\ \\\\\n\n
"
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "strips whitespace with -%> tag" do
template = <<~ERB
<% a = 1 -%>
text
ERB
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles erb comments" do
template = <<~ERB
<%# This is a comment %>
Content
ERB
assert_compiled_snapshot(template)
end
test "compiles an erb comment to the same lines as erubi" do
assert_compiled_snapshot("<%# a comment %>\n
Content
\n", enforce_erubi_equality: true)
end
test "compiles an erb comment followed by text to the same lines as erubi" do
assert_compiled_snapshot("<%# comment %>\nhi\n", enforce_erubi_equality: true)
end
test "handles escape option" do
template = "<%= content %>"
assert_compiled_snapshot(template, { escape: true })
assert_compiled_snapshot(template, { escape: false })
end
test "handles double equals for inverse escaping" do
template = "<%== content %>"
assert_compiled_snapshot(template, { escape: true })
assert_compiled_snapshot(template, { escape: false })
end
test "handles custom bufvar" do
template = "
Test
"
assert_compiled_snapshot(template, { bufvar: "@output" }, enforce_erubi_equality: true)
end
test "handles freeze option" do
template = "
Static content
"
assert_compiled_snapshot(template, { freeze: true }, enforce_erubi_equality: true)
end
test "handles freeze_template_literals option" do
template = "
Content
"
assert_compiled_snapshot(template, enforce_erubi_equality: true)
assert_compiled_snapshot(template, { freeze_template_literals: false }, enforce_erubi_equality: true)
end
test "handles custom preamble and postamble" do
template = "
Test
"
assert_compiled_snapshot(template, { preamble: "@buf = []", postamble: "@buf.join" })
end
test "handles ensure option" do
template = "
Test
"
assert_compiled_snapshot(template, { ensure: true }, enforce_erubi_equality: true)
end
test "handles custom escapefunc" do
template = "<%== content %>"
assert_compiled_snapshot(template, { escape: false, escapefunc: "CGI.escapeHTML" })
end
test "handles chain_appends option" do
template = <<~ERB
<%= a %>
<%= b %>
ERB
assert_compiled_snapshot(template, { chain_appends: true })
assert_compiled_snapshot(template, { chain_appends: false })
end
test "handles multiple erb constructs in complex template" do
template = <<~ERB
<%= @title %>
<% if @user %>
Welcome <%= @user.name %>!
<% @user.posts.each do |post| %>
<%== post.title %>
<%= post.content %>
<% end %>
<% else %>
Please log in
<% end %>
<%# This comment should not appear %>
ERB
assert_compiled_snapshot(template)
end
test "handles void elements correctly" do
template = <<~ERB
ERB
assert_compiled_snapshot(template)
end
test "handles CDATA sections" do
template = <<~ERB
ERB
assert_compiled_snapshot(template)
end
test "handles XML declarations" do
template = ''
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles XML processing instructions" do
template = ''
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles XML processing instructions with ERB" do
template = '"?>'
assert_compiled_snapshot(template)
end
test "handles empty escaped erb tag" do
assert_evaluated_snapshot("<%%>", {}, enforce_erubi_equality: true)
end
test "handles empty escaped erb tag surrounded by text" do
assert_evaluated_snapshot("a <%%> b", {}, enforce_erubi_equality: true)
end
test "leaves whitespace before a <%- tag alone" do
template = <<~ERB
text
<%- a = 1 %>
after
ERB
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles case with conditions in separate erb tags" do
template = <<~ERB
<% case animal %>
<% when "cat" %>
You chose a cat!
<% when "dog" %>
You chose a dog!
<% else %>
Unknown animal
<% end %>
ERB
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles case with a trimming tag before the first condition" do
template = <<~ERB
<% case animal -%>
<% when "cat" %>
You chose a cat!
<% else %>
Unknown animal
<% end %>
ERB
assert_compiled_snapshot(template, enforce_erubi_equality: true)
end
test "handles case with its first condition in the same erb tag when not strict" do
template = <<~ERB
<% case animal
when "cat" %>
You chose a cat!
<% when "dog" %>
You chose a dog!
<% else %>
Unknown animal
<% end %>
ERB
assert_compiled_snapshot(template, parser_options: { strict: false }, enforce_erubi_equality: true)
end
test "rejects case with its first condition in the same erb tag when strict" do
template = <<~ERB
<% case animal
when "cat" %>
You chose a cat!
<% end %>
ERB
error = assert_raises(Herb::Engine::ParseError) do
Herb::Engine.new(template)
end
assert_equal ["ERBCaseWithConditionsError"], error.diagnostics.map(&:code)
end
test "keeps the whitespace around a standalone code tag with trim: false" do
template = <<~ERB
<% a = 1 %>
text
ERB
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, trim: false, enforce_erubi_equality: true)
end
test "keeps the indentation in front of a code tag with trim: false" do
template = "before\n <% a = 1 %>\nafter\n"
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, trim: false, enforce_erubi_equality: true)
end
test "keeps the whitespace around a comment with trim: false" do
template = "before\n <%# comment %>\nafter\n"
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, trim: false, enforce_erubi_equality: true)
end
test "leaves whitespace before a <%- tag alone with trim: false" do
template = "before\n <%- a = 1 %>\nafter\n"
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, trim: false, enforce_erubi_equality: true)
end
test "keeps the newline after a code tag ending in -%> with trim: false" do
template = "<% a = 1 -%>\ntext\n"
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, trim: false, enforce_erubi_equality: true)
end
test "drops the newline after an expression ending in -%> with trim: false" do
template = "<%= a -%>\ntext\n"
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, { a: 1 }, trim: false, enforce_erubi_equality: true)
end
test "keeps the whitespace around control flow with trim: false" do
template = <<~ERB
<% if a == 1 %>
yes
<% else %>
no
<% end %>
ERB
assert_compiled_snapshot(template, trim: false)
assert_evaluated_snapshot(template, { a: 1 }, trim: false, enforce_erubi_equality: true)
end
test "emits escaped ERB tags as literal text" do
template = "<%% literal %>\n<%%= name %>\n<%%- a -%>\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits an escaped ERB block as literal text" do
template = "<%% foo do %>\nx\n<%% end %>\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits escaped ERB tags as literal text inside parsed HTML" do
template = "
<%%= name %>
\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits an escaped ERB conditional as literal text" do
template = "<%% if admin? %>\n
hi
\n<%% else %>\n
bye
\n<%% end %>\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits an escaped ERB iteration as literal text" do
template = "<%% for item in @items %>\n
<%%= item %>
\n<%% end %>\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits an escaped ERB case as literal text" do
template = "<%% case status %>\n<%% when :active %>\n on\n<%% else %>\n off\n<%% end %>\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits an escaped ERB case sharing a tag with its condition as literal text" do
template = "<%% case status when :active %>\n on\n<%% end %>\n"
assert_compiled_snapshot(template, parser_options: { strict: false })
assert_evaluated_snapshot(template, {}, { parser_options: { strict: false } }, enforce_erubi_equality: true)
end
test "keeps the line count when a continuation closes its block in the same tag" do
template = <<~ERB
<% begin %>
body
<% rescue; end %>
<%= after %>
ERB
assert_compiled_snapshot(template)
end
test "emits escaped ERB tags with custom literal delimiters" do
template = "<%% literal %>\n<%%= name %>\n<%%- a -%>\n"
assert_compiled_snapshot(template, literal_prefix: "{%", literal_postfix: "%}")
assert_evaluated_snapshot(template, literal_prefix: "{%", literal_postfix: "%}", enforce_erubi_equality: true)
end
test "emits an escaped ERB block with custom literal delimiters" do
template = "<%% for item in @items %>\n
<%%= item %>
\n<%% end %>\n"
assert_compiled_snapshot(template, literal_prefix: "{%", literal_postfix: "%}")
assert_evaluated_snapshot(template, literal_prefix: "{%", literal_postfix: "%}", enforce_erubi_equality: true)
end
test "emits an escaped ERB comment as literal text" do
template = "<%%# a comment %>\n"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "renders an ERB delimiter held in a single quoted Ruby string" do
template = %(<% x = '<%' %><%= x %>)
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true, enforce_actionview_erubi_equality: false)
end
test "renders an ERB delimiter held in a double quoted Ruby string" do
template = %(<% x = "<%" %><%= x %>)
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true, enforce_actionview_erubi_equality: false)
end
test "renders an ERB delimiter held in a Ruby string in an output tag" do
template = %(<%= '<%' %>)
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true, enforce_actionview_erubi_equality: false)
end
test "keeps the indentation before a case whose first when shares its line" do
template = "\n <% case r %><% when :a %>A<% end %>"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, { r: :a }, enforce_erubi_equality: true)
end
test "keeps the indentation before a case with several whens on one line" do
template = " <% case r %><% when :a %>A<% when :b %>B<% end %>"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, { r: :b }, enforce_erubi_equality: true)
end
test "keeps the indentation before a false if followed by a statement tag" do
template = "\n <% if false %><% x = 1 %>B<% end %>C"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "emits the indentation before a loop followed by a statement tag once" do
template = "\n <% [1, 2].each do |i| %><% x = i %>B<% end %>"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "keeps the indentation before adjacent statement tags" do
template = "\n <% x = 1 %><% y = 2 %>Z"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, enforce_erubi_equality: true)
end
test "trims indented lines that only hold a statement tag" do
template = "
\n <% if a %>\n A\n <% end %>\n
"
assert_compiled_snapshot(template)
assert_evaluated_snapshot(template, { a: true }, enforce_erubi_equality: true)
end
end
end