I’m trying to do a relatively simple pattern match
test "pattern matching assert" do
user = Repo.get(User, 1)
assert %{id: ^user.id} = user
end
This is obviously a simplified case. I would actually like to pattern match multiple keys (but ignore many more) of a JSON response. This could be fixed by creating a user_id variable:
test "pattern matching assert" do
user = Repo.get(User, 1)
user_id = user.id
assert %{id: ^user_id} = user
end
But if there’s 6 different keys I would like to test for I wouldn’t want to create temporary variables for each of them just to assert their value when something like:
assert %{id: ^(user.id)} = user
or
assert %{id: pin(user.id)} = user
Where pin/1 is some special macro. Would capture the intent very clearly without extra noise and un-needed variables. This doesn’t seem like an outlandish question but I’ve been unable to find anything useful via searching.
Edit: fixed a typo that may have been obscuring some of my intent. The code used to read: assert %{id: ^(user.id) = user} instead of assert %{id: ^(user.id)} = user