-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Do not allow guards in assert/1 #13817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,12 @@ defmodule ExUnit.Assertions do | |
|
||
Even though the match works, `assert` still expects a truth | ||
value. In such cases, simply use `==/2` or `match?/2`. | ||
|
||
If you need more complex pattern matching using guards, you | ||
need to use `match?/2`: | ||
|
||
assert match?([%{id: id} | _] when is_integer(id), records) | ||
|
||
""" | ||
defmacro assert({:=, meta, [left, right]} = assertion) do | ||
code = escape_quoted(:assert, meta, assertion) | ||
|
@@ -357,6 +363,23 @@ defmodule ExUnit.Assertions do | |
end | ||
|
||
@doc false | ||
def __match__({:when, _, _} = left, right, _, _, _) do | ||
suggestion = | ||
quote do | ||
assert match?(unquote(left), unquote(right)) | ||
end | ||
|
||
raise ArgumentError, """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the best way to raise here would be. BTW, I noticed that right now without assert we're getting: error: undefined function when/2 (there is no such import) But I think we can have a better error message in this case, given that |
||
invalid pattern in assert/1: | ||
|
||
#{Macro.to_string(left) |> Inspect.Error.pad(2)} | ||
|
||
To assert with guards, use match?/2: | ||
|
||
#{Macro.to_string(suggestion) |> Inspect.Error.pad(2)} | ||
""" | ||
end | ||
|
||
def __match__(left, right, code, check, caller) do | ||
left = __expand_pattern__(left, caller) | ||
vars = collect_vars_from_pattern(left) | ||
|
Uh oh!
There was an error while loading. Please reload this page.