Skip to content

Commit

Permalink
Added other variant tests with guard clause
Browse files Browse the repository at this point in the history
Signed-off-by: Willy Wombat <[email protected]>
  • Loading branch information
octowombat committed Oct 13, 2023
1 parent 751f454 commit 1045672
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions test/elixir_analyzer/test_suite/newsletter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,114 @@ defmodule ElixirAnalyzer.ExerciseTest.NewsletterTest do
end
end)

close_log(log_pid)
end
end,
defmodule Newsletter do
def read_emails(path) when is_binary(path) do
path
|> File.read!()
|> String.split()
end

def open_log(path) when is_binary(path) do
File.open(path, [:write])
end

def log_sent_email(pid, email) when is_pid(pid) and is_binary(email) do
IO.puts(pid, email)
end

def close_log(pid) when is_pid(pid) do
File.close(pid)
end

def send_newsletter(emails_path, log_path, send_fun)
when is_binary(emails_path) and is_binary(log_path) and is_function(send_fun, 1) do
{:ok, log_pid} = open_log(log_path)
emails = read_emails(emails_path)

Enum.each(emails, fn email ->
case send_fun.(email) do
:ok -> log_sent_email(log_pid, email)
_ -> nil
end
end)

close_log(log_pid)
end
end,
defmodule Newsletter do
import File
import IO

def read_emails(path) when is_binary(path) do
path
|> read!()
|> String.split()
end

def open_log(path) when is_binary(path) do
open!(path, [:write])
end

def log_sent_email(pid, email) when is_pid(pid) and is_binary(email) do
puts(pid, email)
end

def close_log(pid) when is_pid(pid) do
close(pid)
end

def send_newsletter(emails_path, log_path, send_fun)
when is_binary(emails_path) and is_binary(log_path) and is_function(send_fun, 1) do
log_pid = open_log(log_path)
emails = read_emails(emails_path)

Enum.each(emails, fn email ->
case send_fun.(email) do
:ok -> log_sent_email(log_pid, email)
_ -> nil
end
end)

close_log(log_pid)
end
end,
defmodule Newsletter do
import File
import IO

def read_emails(path) when is_binary(path) do
path
|> read!()
|> String.split()
end

def open_log(path) when is_binary(path) do
open(path, [:write])
end

def log_sent_email(pid, email) when is_pid(pid) and is_binary(email) do
puts(pid, email)
end

def close_log(pid) when is_pid(pid) do
close(pid)
end

def send_newsletter(emails_path, log_path, send_fun)
when is_binary(emails_path) and is_binary(log_path) and is_function(send_fun, 1) do
{:ok, log_pid} = open_log(log_path)
emails = read_emails(emails_path)

Enum.each(emails, fn email ->
case send_fun.(email) do
:ok -> log_sent_email(log_pid, email)
_ -> nil
end
end)

close_log(log_pid)
end
end
Expand Down

0 comments on commit 1045672

Please sign in to comment.