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

Hash destructuring shows missing coverage? #1092

Open
mockdeep opened this issue May 26, 2024 · 3 comments
Open

Hash destructuring shows missing coverage? #1092

mockdeep opened this issue May 26, 2024 · 3 comments

Comments

@mockdeep
Copy link

mockdeep commented May 26, 2024

  • using RSpec run via rake
  • simplecov (0.22.0)
  • ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux]

I'm not able to understand why this line using rightward assignment is reported as missing coverage:

destructuring

It looks like it's treating the line as if it's a conditional without an else case, but as far as I'm aware there is no branching possible here. Ruby will throw an error if any of the properties are missing. You can see the behavior on this project. You should be able to clone it and run bundle && rake to produce coverage results.

@viralpraxis
Copy link

The missing branch is the one that raises NoMatchingPatternKeyError. You can treat that rightward assignment as a syntax sugar for something like this:

if %i[card checklist checklist_items].all? { state.key? _1 } # covered
  card, checklist, checklist_items = ...
else # uncovered
  raise NoMatchingPatternKeyError, ...
end

@mockdeep
Copy link
Author

@viralpraxis thanks for the explanation. That strikes me as kind of odd, though. The way my code is structured, it is impossible for me to cover that case. I would have to stub some private behavior in my classes to return invalid data. There are lots of other places in Ruby that will cause an error if the shape of things is incorrect, but those don't report coverage failures. Things like passing keyword arguments to methods, or hash.fetch(:foo), which throws an error if the key is not present.

@viralpraxis
Copy link

@mockdeep please note that simplecov is just a wrapper around ruby's low-level coverage API (https://rubyapi.org/o/coverage). Also, Hash#fetch's branching logic is hidden in MRI's C implementation

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

No branches or pull requests

2 participants