# frozen_string_literal: true
require_relative "../../test_helper"
require_relative "../../../lib/herb/engine/slots/dynamics_compiler"
module Engine
module Slots
class ParityTest < Minitest::Spec
class View
def initialize(**assigns)
assigns.each { |name, value| instance_variable_set(:"@#{name}", value) }
end
def form_with(**) = "
"
def tag = TagBuilder.new
class Field
def label = "Name"
end
class TagBuilder
def li(**) = "#{yield}"
end
end
TEMPLATES = {
"expressions in order" => [%(<%= @a %><%= @b %>
<%= @c %>), { a: "a", b: "b", c: "c" }],
"an attribute and a child" => [%(<%= @n %>), { c: "item", n: "Marco" }],
"two attributes on one element" => [%(), { c: "c", i: "i" }],
"a block, and what follows it" => [%(<%= form_with(model: 1) do |f| %><%= f.label %><% end %><%= @after %>), { after: "A" }],
"a conditional that branches" => [%(<% if @on %><%= @x %><% else %><%= @y %><% end %><%= @z %>), { on: true, x: "x", y: "y", z: "z" }],
"a conditional that collapses" => [%(<% if @on %><%= @x %>
<% else %><%= @y %>
<% end %>), { on: false, x: "x", y: "y" }],
"a conditional matching nothing" => [%(<% if @on %><%= @x %><% end %>), { on: false, x: "x" }],
"a keyed collection" => [%(<% @items.each do |r| %><%= r %><% end %>), { items: [1, 2] }],
"an empty collection" => [%(<% @items.each do |r| %><%= r %><% end %>), { items: [] }],
"a collection inside a branch" => [%(<% if @on %><% @items.each do |r| %><%= r %><% end %><% end %>), { on: true, items: ["a"] }],
"a helper block building an element" => [%(<%= tag.li(id: 1) do %><%= @n %><% end %><%= @after %>), { n: "n", after: "A" }],
"an iteration whose value is output" => [%(<%= @items.each do |r| %><%= r %><% end %>), { items: [1, 2] }],
"a conditional inside a block" => [%(<%= form_with(model: 1) do |f| %><% if @on %><%= @x %><% end %><% end %>), { on: true, x: "x" }],
"a boolean attribute inside a block" => [%(<%# herb:state (sending: false) %><%= form_with(model: 1) do |f| %><% end %>), {}],
"an interpolated attribute inside a block" => [%(<%= form_with(model: 1) do |f| %>x<% end %>), { r: 7 }],
"nothing dynamic at all" => [%(static
), {}],
}.freeze
def compile(source)
Herb::Engine::Slots::DynamicsCompiler.new(source, filename: "app/views/test.html.erb")
end
def evaluate(compiler, assigns)
View.new(**assigns).instance_eval(compiler.src).fetch(:slots)
end
def shapes(values, found = {})
values.each do |index, value|
case value
when Hash
if value.key?(:branch)
found[index] = :conditional
shapes(value[:slots] || {}, found)
elsif value.key?(:items)
found[index] = :collection
value[:items].each_value { |item| shapes(item, found) }
else
found[index] = :value
end
else
found[index] = :value
end
end
found
end
def recorded(compiler)
compiler.slot_visitor.slots.to_h { |slot|
shape = case slot.type
when :conditional then :conditional
when :collection then :collection
else :value
end
[slot.index, shape]
}
end
TEMPLATES.each do |label, (source, assigns)|
test "every value #{label} produces names a slot the visitor recorded" do
compiler = compile(source)
carried = shapes(evaluate(compiler, assigns))
known = recorded(compiler)
assert_empty carried.keys - known.keys
end
test "every value #{label} produces is carried as the shape its slot was recorded as" do
compiler = compile(source)
carried = shapes(evaluate(compiler, assigns))
known = recorded(compiler)
carried.each do |index, shape|
next unless known.key?(index)
assert_equal known[index], shape, "slot #{index} was recorded as #{known[index]} and its value arrived as #{shape}"
end
end
end
test "an interpolated attribute inside a block records its parts and renders text" do
source = %(<%= form_with(model: 1) do |f| %>x<% end %>)
compiler = compile(source)
values = evaluate(compiler, { r: 7 })
interpolated = compiler.slot_visitor.slots.find { |slot| slot.type == :attribute_interpolation }
assert_equal ["7"], values.fetch(interpolated.index)
assert_equal %(), values.values.grep(String).join
end
test "a boolean attribute inside a block records presence and renders text" do
source = %(<%# herb:state (sending: true) %><%= form_with(model: 1) do |f| %><% end %>)
compiler = compile(source)
values = evaluate(compiler, {})
presence = compiler.slot_visitor.slots.find { |slot| slot.type == :boolean_attribute }
assert_equal true, values.fetch(presence.index)
assert_equal "", values.values.grep(String).join
end
test "a block's interior is covered alongside the block itself" do
source = %(<%= form_with(model: 1) do |f| %><%= f.label %><% end %><%= @after %>)
compiler = compile(source)
assert_equal([[0, :block], [1, :child], [2, :child]], compiler.slot_visitor.slots.map { |slot| [slot.index, slot.type] })
assert_equal({ 0 => "", 1 => "Name", 2 => "A" }, evaluate(compiler, after: "A"))
end
test "an iteration is a collection whether or not its value is output" do
items = { items: { "0" => { 1 => "1" }, "1" => { 1 => "2" } }, order: ["0", "1"] }
%(<% @items.each do |r| %><%= r %><% end %>).then do |source|
assert_equal({ 0 => items }, evaluate(compile(source), items: [1, 2]))
end
%(<%= @items.each do |r| %><%= r %><% end %>).then do |source|
assert_equal({ 0 => items }, evaluate(compile(source), items: [1, 2]))
end
end
test "a collapsed conditional reports one value rather than a branch" do
source = %(<% if @on %><%= @x %>
<% else %><%= @y %>
<% end %>)
compiler = compile(source)
assert_equal [:child], compiler.slot_visitor.slots.map(&:type)
assert_equal({ 0 => "y" }, evaluate(compiler, on: false, x: "x", y: "y"))
end
test "the visitor it numbered against is the one it compiled with" do
compiler = compile(%(<%= @a %>
))
assert_equal "app/views/test.html.erb", compiler.slot_visitor.schema[:file]
assert_match(/\A[0-9a-f]{8}\z/, compiler.slot_visitor.version)
end
end
end
end