From b2f8ddfff316f78b55077e30414e34c37a3e22ea Mon Sep 17 00:00:00 2001 From: mrloyal Date: Sun, 1 Sep 2024 21:41:00 +0200 Subject: [PATCH] chore: move when clause into pattern --- lib/swoosh_mailinator.ex | 4 ++-- test/swoosh_mlntr.exs | 29 +++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/swoosh_mailinator.ex b/lib/swoosh_mailinator.ex index 14a5991..b34a672 100644 --- a/lib/swoosh_mailinator.ex +++ b/lib/swoosh_mailinator.ex @@ -43,11 +43,11 @@ defmodule Swoosh.Adapters.Mailinator do alias Swoosh.Email 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 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 end diff --git a/test/swoosh_mlntr.exs b/test/swoosh_mlntr.exs index f9f4353..a9e29f1 100644 --- a/test/swoosh_mlntr.exs +++ b/test/swoosh_mlntr.exs @@ -1,3 +1,5 @@ +# TODO rewrite with exunit + Mix.install( [ {:swoosh, "~> 1.7"}, @@ -35,6 +37,12 @@ defmodule Main do |> html_body(nil) end + def test_both_absent() do + prep() + |> Map.delete(:html_body) + |> Map.delete(:text_body) + end + def prep do mess = "mess" new() @@ -47,15 +55,24 @@ defmodule Main do def send(email) do email - |> IO.inspect() |> deliver([ adapter: Swoosh.Adapters.Mailinator ]) end end -Main.test_both() |> Main.send() |> IO.inspect() -Main.test_text_only() |> Main.send() |> IO.inspect() -Main.test_html_only() |> Main.send() |> IO.inspect() -Main.test_text_nil() |> Main.send() |> IO.inspect() -Main.test_html_nil() |> Main.send() |> IO.inspect() +# ok cases +Main.test_both() |> Main.send() +Main.test_text_only() |> Main.send() +Main.test_html_only() |> Main.send() +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