Custom Assertions
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue MetaMathQueuesCustom Assertions
Create a CustomAssertion.assert
macro which provides test feedback in the format:
"""
Assertion with `==` failed.
left: 1
right: 2
"""
Your solution should work with the ==
, >
, <
, <=
, >=
, and ===
operators.
Print a "Success"
message if the assertion passes.
Example Solution
If we want to have full control over the result, one solution would be to use multiple function heads (or any means of control flow) for each operator. However, if we don’t have custom messages for each operator, we can cleverly use the apply/3
function to keep our solution minimal.
defmodule CustomAssertion do
@success "Success!"
def check(operator, left, right) do
if apply(Kernel, operator, [left, right]) do
@success
else
"""
Assertion with #{operator} failed.
left: #{left}
right: #{right}
"""
end
end
defmacro assert({operator, _meta, [left, right]}) do
quote do
CustomAssertion.check(unquote(operator), unquote(left), unquote(right))
end
end
end
defmodule CustomAssertion do
defmacro assert({operator, _meta, [left, right]}) do
end
end
Your solution should be able to display failure messages for the following assertions.
require CustomAssertion
CustomAssertion.assert(1 == 2) |> IO.puts()
CustomAssertion.assert(1 === 2) |> IO.puts()
CustomAssertion.assert(1 > 2) |> IO.puts()
CustomAssertion.assert(1 >= 2) |> IO.puts()
CustomAssertion.assert(2 < 1) |> IO.puts()
CustomAssertion.assert(2 <= 1) |> IO.puts()
Your solution should display success messages for the following assertions.
require CustomAssertion
CustomAssertion.assert(1 == 1) |> IO.puts()
CustomAssertion.assert(1 === 1) |> IO.puts()
CustomAssertion.assert(2 > 1) |> IO.puts()
CustomAssertion.assert(1 >= 1) |> IO.puts()
CustomAssertion.assert(1 < 2) |> IO.puts()
CustomAssertion.assert(1 <= 1) |> IO.puts()
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Custom Assertions exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.