Skip to content

Instantly share code, notes, and snippets.

@aglassman
Created October 19, 2021 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aglassman/aaf3bb118f3e130a4be17bbeb9355df5 to your computer and use it in GitHub Desktop.
Save aglassman/aaf3bb118f3e130a4be17bbeb9355df5 to your computer and use it in GitHub Desktop.
Elvis Test
defmodule ElvisTest do
use ExUnit.Case
def a ~> b, do: elvis(a, b)
def elvis(nil, fun), do: nil
def elvis({nil, default_val}, fun), do: elvis(default_val, fun)
def elvis(val, fun), do: fun.(val)
test "example" do
tag = fn inner, tag -> "<#{tag}>#{inner}</#{tag}>" end
params = %{title: "Movie Title", description: "Great Movie A+"}
result_1 =
params[:description]
~> fn a -> tag.(a, "h1") end
~> fn a -> tag.(a, "div") end
assert "<div><h1>Great Movie A+</h1></div>" == result_1
result_2 =
params[:sub_title]
~> fn a -> tag.(a, "h1") end
~> fn a -> tag.(a, "div") end
refute result_2
result_2 =
{params[:sub_title], "No Subtitle"}
~> fn a -> tag.(a, "h1") end
~> fn a -> tag.(a, "div") end
assert "<div><h1>No Subtitle</div>" == result_1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment