|
| 1 | +defmodule ElixirScript.Test.Assertions do |
| 2 | + @moduledoc """ |
| 3 | + Defines assertions for use in ElixirScript test. |
| 4 | + These are a subset of [ExUnit.Assertions](https://hexdocs.pm/ex_unit/ExUnit.Assertions.html) |
| 5 | + """ |
| 6 | + |
| 7 | + @doc false |
| 8 | + def raise_elixir_script_assert(error, file, line) do |
| 9 | + reraise(ElixirScript.Test.AssertionError, [ |
| 10 | + left: error.left, |
| 11 | + right: error.right, |
| 12 | + message: error.message, |
| 13 | + expr: error.expr, |
| 14 | + file: file, |
| 15 | + line: line |
| 16 | + ], []) |
| 17 | + end |
| 18 | + |
| 19 | + @doc """ |
| 20 | + Asserts its argument is a truthy value |
| 21 | + """ |
| 22 | + defmacro assert(assertion) do |
| 23 | + %{file: file, line: line} = __CALLER__ |
| 24 | + |
| 25 | + quote [file: file, line: line] do |
| 26 | + try do |
| 27 | + ExUnit.Assertions.assert(unquote(assertion)) |
| 28 | + rescue |
| 29 | + error in [ExUnit.AssertionError] -> |
| 30 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 31 | + error, |
| 32 | + unquote(file), |
| 33 | + unquote(line) |
| 34 | + ) |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + @doc """ |
| 40 | + Asserts `value` is `true`, displaying the given `message` otherwise. |
| 41 | + """ |
| 42 | + defmacro assert(value, message) do |
| 43 | + %{file: file, line: line} = __CALLER__ |
| 44 | + |
| 45 | + quote [file: file, line: line] do |
| 46 | + try do |
| 47 | + ExUnit.Assertions.assert(unquote(value), unquote(message)) |
| 48 | + rescue |
| 49 | + error in [ExUnit.AssertionError] -> |
| 50 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 51 | + error, |
| 52 | + unquote(file), |
| 53 | + unquote(line) |
| 54 | + ) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + @doc """ |
| 60 | + Asserts the `exception` is raised during `function` execution. |
| 61 | + Returns the rescued exception, fails otherwise. |
| 62 | + """ |
| 63 | + defmacro assert_raise(exception, function) do |
| 64 | + %{file: file, line: line} = __CALLER__ |
| 65 | + |
| 66 | + quote [file: file, line: line] do |
| 67 | + try do |
| 68 | + ExUnit.Assertions.assert(unquote(exception), unquote(function)) |
| 69 | + rescue |
| 70 | + error in [ExUnit.AssertionError] -> |
| 71 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 72 | + error, |
| 73 | + unquote(file), |
| 74 | + unquote(line) |
| 75 | + ) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + @doc """ |
| 81 | + Asserts the `exception` is raised during `function` execution with |
| 82 | + the expected `message`, which can be a `Regex` or an exact `String`. |
| 83 | + Returns the rescued exception, fails otherwise. |
| 84 | + """ |
| 85 | + defmacro assert_raise(exception, message, function) do |
| 86 | + %{file: file, line: line} = __CALLER__ |
| 87 | + |
| 88 | + quote [file: file, line: line] do |
| 89 | + try do |
| 90 | + ExUnit.Assertions.assert( |
| 91 | + unquote(exception), |
| 92 | + unquote(message), |
| 93 | + unquote(function) |
| 94 | + ) |
| 95 | + rescue |
| 96 | + error in [ExUnit.AssertionError] -> |
| 97 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 98 | + error, |
| 99 | + unquote(file), |
| 100 | + unquote(line) |
| 101 | + ) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + @doc """ |
| 107 | + Asserts that `value1` and `value2` differ by no more than `delta` |
| 108 | + """ |
| 109 | + defmacro assert_in_delta(value1, value2, delta, message \\ nil) do |
| 110 | + %{file: file, line: line} = __CALLER__ |
| 111 | + |
| 112 | + quote [file: file, line: line] do |
| 113 | + try do |
| 114 | + ExUnit.Assertions.assert_in_delta( |
| 115 | + unquote(value1), |
| 116 | + unquote(value2), |
| 117 | + unquote(delta), |
| 118 | + unquote(message) |
| 119 | + ) |
| 120 | + rescue |
| 121 | + error in [ExUnit.AssertionError] -> |
| 122 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 123 | + error, |
| 124 | + unquote(file), |
| 125 | + unquote(line) |
| 126 | + ) |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + @doc """ |
| 132 | + A negative assertion, expects the expression to be `false` or `nil`. |
| 133 | + """ |
| 134 | + defmacro refute(assertion) do |
| 135 | + %{file: file, line: line} = __CALLER__ |
| 136 | + |
| 137 | + quote [file: file, line: line] do |
| 138 | + try do |
| 139 | + ExUnit.Assertions.assert(unquote(assertion)) |
| 140 | + rescue |
| 141 | + error in [ExUnit.AssertionError] -> |
| 142 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 143 | + error, |
| 144 | + unquote(file), |
| 145 | + unquote(line) |
| 146 | + ) |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + @doc """ |
| 152 | + Asserts `value` is `nil` or `false` (that is, `value` is not truthy). |
| 153 | + """ |
| 154 | + defmacro refute(value, message) do |
| 155 | + %{file: file, line: line} = __CALLER__ |
| 156 | + |
| 157 | + quote [file: file, line: line] do |
| 158 | + try do |
| 159 | + ExUnit.Assertions.assert(unquote(value), unquote(message)) |
| 160 | + rescue |
| 161 | + error in [ExUnit.AssertionError] -> |
| 162 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 163 | + error, |
| 164 | + unquote(file), |
| 165 | + unquote(line) |
| 166 | + ) |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + @doc """ |
| 172 | + Asserts that `value1` and `value2` are not within `delta` |
| 173 | + """ |
| 174 | + defmacro refute_in_delta(value1, value2, delta, message \\ nil) do |
| 175 | + %{file: file, line: line} = __CALLER__ |
| 176 | + |
| 177 | + quote [file: file, line: line] do |
| 178 | + try do |
| 179 | + ExUnit.Assertions.refute_in_delta( |
| 180 | + unquote(value1), |
| 181 | + unquote(value2), |
| 182 | + unquote(delta), |
| 183 | + unquote(message) |
| 184 | + ) |
| 185 | + rescue |
| 186 | + error in [ExUnit.AssertionError] -> |
| 187 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 188 | + error, |
| 189 | + unquote(file), |
| 190 | + unquote(line) |
| 191 | + ) |
| 192 | + end |
| 193 | + end |
| 194 | + end |
| 195 | + |
| 196 | + @doc """ |
| 197 | + Fails with a message. |
| 198 | + """ |
| 199 | + defmacro flunk(message \\ "Flunked!") do |
| 200 | + %{file: file, line: line} = __CALLER__ |
| 201 | + |
| 202 | + quote [file: file, line: line] do |
| 203 | + try do |
| 204 | + ExUnit.Assertions.flunk(unquote(message)) |
| 205 | + rescue |
| 206 | + error in [ExUnit.AssertionError] -> |
| 207 | + ElixirScript.Test.Assertions.raise_elixir_script_assert( |
| 208 | + error, |
| 209 | + unquote(file), |
| 210 | + unquote(line) |
| 211 | + ) |
| 212 | + end |
| 213 | + end |
| 214 | + end |
| 215 | +end |
0 commit comments