case expressions

This commit is contained in:
Mitchell Hanberg 2020-11-04 19:57:03 -05:00
parent db110d4241
commit 5c5edfa67f
6 changed files with 69 additions and 4 deletions

View file

@ -1,7 +1,7 @@
defmodule TempleDemoWeb.PageController do
use TempleDemoWeb, :controller
def index(conn, _params) do
render(conn, "index.html")
def index(conn, params) do
render(conn, "index.html", text: params["text"])
end
end

View file

@ -3,8 +3,15 @@ section class: "phx-hero" do
gettext("Welcome to %{name}!", name: "Phoenix")
end
p do
"Peace-of-mind from prototype to production"
case @text do
"staging" ->
p do
"Peace-of-mind from prototype to staging"
end
_ ->
p do
"Peace-of-mind from prototype to production"
end
end
end

View file

@ -10,6 +10,22 @@ defmodule TempleDemoWeb.TempleFeatureTest do
|> assert_text("Welcome to Phoenix!")
end
feature "case statements work", %{session: session} do
session =
session
|> visit("/?text=staging")
session |> assert_text("Welcome to Phoenix!")
session |> assert_text("Peace-of-mind from prototype to staging")
session =
session
|> visit("/?text=foobar")
session |> assert_text("Welcome to Phoenix!")
session |> assert_text("Peace-of-mind from prototype to production")
end
feature "can create a new post", %{session: session} do
session
|> visit(Routes.post_path(E, :index))

View file

@ -68,6 +68,7 @@ defmodule Temple.Parser do
Temple.Parser.NonvoidElementsAliases,
Temple.Parser.VoidElementsAliases,
Temple.Parser.AnonymousFunctions,
Temple.Parser.RightArrow,
Temple.Parser.DoExpressions,
Temple.Parser.Match,
Temple.Parser.Default

View file

@ -0,0 +1,20 @@
defmodule Temple.Parser.RightArrow do
@behaviour Temple.Parser
alias Temple.Parser
alias Temple.Buffer
@impl Parser
def applicable?({:->, _, _}), do: true
def applicable?(_), do: false
@impl Parser
def run({_, _, [[pattern], args]}, buffer) do
import Temple.Parser.Private
Buffer.put(buffer, "<% " <> Macro.to_string(pattern) <> " -> %>\n")
traverse(buffer, args)
:ok
end
end

View file

@ -151,6 +151,27 @@ defmodule TempleTest do
assert result == ~s{<%= unless(true == false) do %><div class="hi"></div><% end %>}
end
test "renders a case expression as eex" do
result =
temple do
case @foo.bar do
:baz ->
weight_form(form: @form)
end
end
expected =
~S"""
<%= case(@foo.bar) do %>
<% :baz -> %>
<%= weight_form(form: @form) %>
<% end %>
"""
|> String.trim()
assert result == expected
end
test "renders multiline anonymous function with 1 arg before the function" do
result =
temple do