# frozen_string_literal: true require_relative "../../test_helper" require_relative "../../snapshot_utils" require_relative "../../../lib/herb/engine" require_relative "../../../lib/herb/engine/slots/visitor" require_relative "../../../lib/herb/engine/slots/dynamics_compiler" module Engine module Slots class StaticsTest < Minitest::Spec include SnapshotUtils def options { visitors: [Herb::Engine::Slots::Visitor.new(mode: :client)], filename: "app/views/test.html.erb" } end def render(template, locals = {}) evaluate_herb_source(Herb::Engine.new(template, **options).src, locals) end def parked(template, locals = {}) render(template, locals)[%r{}m, 1] end test "parks both branches when only one of them rendered" do assert_evaluated_snapshot( "
<% if @admin %>Hi <%= @name %><% else %>guest<% end %>
", { "@admin" => false, "@name" => "Marco" }, options ) end test "parks every arm of a case, including the one that ran" do assert_evaluated_snapshot( "
<% case @s %><% when 1 %>one<% when 2 %>two<% else %>other<% end %>
", { "@s" => 3 }, options ) end test "parks both branches whichever one ran first" do assert_evaluated_snapshot( "
<% if @admin %>Hi <%= @name %><% else %>guest<% end %>
", { "@admin" => true, "@name" => "Marco" }, options ) end test "parks a conditional nested in a branch on its own rather than inside its parent" do assert_evaluated_snapshot( "

<% if @a %><% if @b %>deep<% end %>outer<% end %>

", { "@a" => false, "@b" => false }, options ) end test "keeps the markers of the slots inside a branch, so the client can fill them" do assert_evaluated_snapshot( %(
<% if @a %><%= @name %><% end %>
), { "@a" => false, "@c" => "item", "@name" => "Marco" }, options ) end test "parks nothing for a template that has no conditionals" do assert_evaluated_snapshot("

Hi <%= @name %>

", { "@name" => "Marco" }, options) end test "names the template it belongs to, so it does not depend on where it sits" do output = render("
<% if @a %>x<% end %>
", { "@a" => false }) assert_match(%r{