View Source Corsa.Assert (corsa v0.1.2)

Assertions in programming are statements that check conditions. They help catch errors early and improve code reliability and maintainability. In Corsa, assertions can work in two ways: by throwing exceptions or logging errors.

Examples

iex> defmodule Elixir.Corsa.Assert.ExampleAssert do
...>   use Corsa.Assert
...>   def f(x) do
...>     Corsa.Assert.assert x > 0
...>     :ok
...>   end
...> end
iex> Elixir.Corsa.Assert.ExampleAssert.f(10)
:ok
iex> Elixir.Corsa.Assert.ExampleAssert.f(0)
** (Corsa.AssertViolationError) @assert does not hold in expression 'x > 0' with value left '0'

iex> f = fn -> {:ok, 1} end
iex> Corsa.Assert.assert {:ok, x} = f.()
iex> x
1
iex> x = 2
iex> Corsa.Assert.assert {:ok, ^x} = f.()
** (Corsa.AssertViolationError) @assert does not hold in expression '{:ok, ^x} = f.()' with values left '{:ok, ^x}' and right '{:ok, 1}'

Summary

Functions

Link to this macro

assert(expr, violation \\ Corsa.AssertViolationError, error \\ Corsa.AssertError, opts \\ [])

View Source (macro)