chore: move when clause into pattern

This commit is contained in:
mrloyal 2024-09-01 21:41:00 +02:00
parent 7ff96fabe1
commit b2f8ddfff3
2 changed files with 25 additions and 8 deletions

View File

@ -43,11 +43,11 @@ defmodule Swoosh.Adapters.Mailinator do
alias Swoosh.Email alias Swoosh.Email
alias Swoosh.Mailer alias Swoosh.Mailer
def deliver(%Email{html_body: html, text_body: text} = email, _) when is_nil(html) do def deliver(%Email{html_body: nil, text_body: _} = email, _) do
email |> delegate email |> delegate
end end
def deliver(%Email{html_body: html, text_body: text} = email, _) when is_nil(text) do def deliver(%Email{html_body: _, text_body: nil} = email, _) do
email |> delegate email |> delegate
end end

View File

@ -1,3 +1,5 @@
# TODO rewrite with exunit
Mix.install( Mix.install(
[ [
{:swoosh, "~> 1.7"}, {:swoosh, "~> 1.7"},
@ -35,6 +37,12 @@ defmodule Main do
|> html_body(nil) |> html_body(nil)
end end
def test_both_absent() do
prep()
|> Map.delete(:html_body)
|> Map.delete(:text_body)
end
def prep do def prep do
mess = "mess" mess = "mess"
new() new()
@ -47,15 +55,24 @@ defmodule Main do
def send(email) do def send(email) do
email email
|> IO.inspect()
|> deliver([ |> deliver([
adapter: Swoosh.Adapters.Mailinator adapter: Swoosh.Adapters.Mailinator
]) ])
end end
end end
Main.test_both() |> Main.send() |> IO.inspect() # ok cases
Main.test_text_only() |> Main.send() |> IO.inspect() Main.test_both() |> Main.send()
Main.test_html_only() |> Main.send() |> IO.inspect() Main.test_text_only() |> Main.send()
Main.test_text_nil() |> Main.send() |> IO.inspect() Main.test_html_only() |> Main.send()
Main.test_html_nil() |> Main.send() |> IO.inspect() Main.test_text_nil() |> Main.send()
Main.test_html_nil() |> Main.send()
# failing cases
try do
Main.test_both_absent() |> Main.send()
rescue
FunctionClauseError -> "expected"
else
_ -> "shoud have failed"
end