Skip to content
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

Fix non-existing atom exception when accidentally matching on map literal #48

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

s3cur3
Copy link
Contributor

@s3cur3 s3cur3 commented Dec 9, 2024

Hi Mat! Thanks so much for this wonderful library. I'm on workplace number 3 where it's been introduced to the delight of my coworkers. ☺️

This PR fixes a crash that looks like this:

     ** (ArgumentError) errors were found at the given arguments:

       * 1st argument: not an already existing atom

...which was previously occurring when you used the literal map matcher on a map with arbitrary string keys that don't have a corresponding atom defined. For me, this occurred when I forgot to wrap my map matcher in a superset/1. So, for instance, what I meant was:

assert %{"foo" => "bar", "baz" => "bop"} ~> superset(%{"foo" => "bar"})

...but what I accidentally wrote was just:

assert %{"foo" => "bar", "baz" => "bop"} ~> %{"foo" => "bar"}

Because the error was so generic, this was pretty difficult to debug. With this change applied, though, you now get a nice "Unexpected key" error.

…eral

This fixes a crash that looks like this:

```
     ** (ArgumentError) errors were found at the given arguments:

       * 1st argument: not an already existing atom
```

...which was previously occurring when you used the literal map matcher on a map with arbitrary string keys that don't have a corresponding atom defined. For me, this occurred when I forgot to wrap my map matcher in a `superset/1`. So, for instance, what I *meant* was:

```elixir
assert %{"foo" => "bar", "baz" => "bop"} ~> superset(%{"foo" => "bar"})
```

...but what I accidentally wrote was just:

```elixir
assert %{"foo" => "bar", "baz" => "bop"} ~> %{"foo" => "bar"}
```

Because the error was so generic, this was pretty difficult to debug. With this change applied, though, you now get a nice "Unexpected key" error.
Comment on lines -46 to +45
|> MapSet.difference(atom_keys |> Enum.map(&Kernel.to_string/1) |> MapSet.new())
|> MapSet.difference(string_keys |> Enum.map(&String.to_existing_atom/1) |> MapSet.new())
|> MapSet.difference(MapSet.new(atom_keys, &Kernel.to_string/1))
|> MapSet.difference(MapSet.new(string_keys, &safe_existing_atom/1))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff on the first line here is not strictly necessary for the fix, but I figured I'd save an unnecessary list creation where possible.

@mtrudel mtrudel merged commit 9fc62f9 into mtrudel:main Dec 9, 2024
8 checks passed
@mtrudel
Copy link
Owner

mtrudel commented Dec 9, 2024

Love it! Thanks for the contribution!

@mtrudel
Copy link
Owner

mtrudel commented Dec 9, 2024

Published as 0.3.6

@s3cur3
Copy link
Contributor Author

s3cur3 commented Dec 9, 2024

Thank you so much! ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants