-
Notifications
You must be signed in to change notification settings - Fork 388
Switch expression 0% coverage #936
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
Comments
Thanks for reporting this, seems related to new c# pattern matching. |
It's definitely related to the new switch pattern. I have that in a few places in my code too running the same coverlet.msbuild version |
@MarcoRossignoli Since my code suffers from this issue I decided to look at it. I started with additional method in I'm not getting that on Sharplab nor when I write code in LINQPad or in another assembly. As you can see from IL, expression switch shouldn't be different from regular switch. Do you have any clue why this is happening? What am I missing? Branche code generated before/after switch:
|
Can you try in release to see if it disappears?that branch could be needed for debuggers, for instance to put on it a breakpoint |
@MarcoRossignoli You are right, I'm not getting those conditions in Release build. Also I think I might have some compiler options on, so that would explain why I didn't see it there before. Anyway, I grabbed instrumented copy of method with switch expression and I do see instrumentation hits there just fine.
Problem is not in instrumentation itself (luckily, because it is still good ol' switch - nothing broken here). At the moment, I'm not sure where to look next - will be glad for any input, or happily leave issue to someone experienced. |
You should try to add a tmp test like https://github.com/coverlet-coverage/coverlet/blob/master/test/coverlet.core.tests/Coverage/CoverageTests.SelectionStatements.cs#L14 and use Usually the two main point to check is branch getter https://github.com/coverlet-coverage/coverlet/blob/master/src/coverlet.core/Instrumentation/Instrumenter.cs#L540 |
@MarcoRossignoli I spent some time trying to understand what's going on and I have to give up (for now). What I tried:As I wrote before, I can see I redirected my attention back to instrumenter to check branch points and line ranges. Branch points, at least regarding preceding Since this domain is completely new for me, I'm honestly not even sure if my observation is correct - so for anyone else looking into this issue, don't take my findings for granted. |
@wdolek every idea/thought is welcome and will help everyone will move on this, thanks a lot for your effort!I'm bit busy but I'll take a look asap and I'll start from your analysis, so many many thanks! |
We hit this problem also on our side. It can be easily reproducible:
|
Not that it changes anything, just be aware that those short switches are actually compiled to bunch of simple @MarcoRossignoli I'm looking forward to see what's wrong here 🤞 |
other repro danielpalme/ReportGenerator#362 (comment) |
Hello, Thanks |
I don't have ETA but I have some ideas on how to solve that it's better than nothing 😄 I'll try to fix asap!
Unfortunately there is no workaround is related to the way C# compiler emits sequence for new pattern, we have to fix in code. |
Thank you , hope you all the best 👍 |
@MarcoRossignoli Can I help here somehow? I just took a peek at the IL and see an additional branch point and a sequence point that spans over multiple lines. I guess we could try to find a match for the switch pattern and ignore the branch point. Still this doesn't seem to be enough. |
My idea was bit different, we could count in different way here ~ https://github.com/coverlet-coverage/coverlet/blob/master/src/coverlet.core/Coverage.cs#L384 We could create some structure to keep the actual accounted line for every sequence, try to explain better, suppose to have 3 overlapped sequences record similar to nested closure or switch: 1 sequence start line 10 end line 18 -> 10 hit We could build some metadata for every range sequence where we report the "line to not account" because is accounted by a nested sequence and my statement is that the most nested sequence range is the right one. So in the case above we could end with something like line = hits count
Are you able to try this alg? The changes should be done here https://github.com/coverlet-coverage/coverlet/blob/master/src/coverlet.core/Coverage.cs#L384-L444 This month I'm very busy so I think I won't have enough time until next month, I was experimenting here master...MarcoRossignoli:issue_936_switch take my test if you want(ignore the patch code doesn't work as-is is only a test I was in debugging). |
Yes OK I got it. I will try this and get back to you when I'm facing some issues. |
I rapidly prototyped this and at a first glimpse it seemed to work out. Then I tested a bit with What was bugging me all the time when looking at the IL was this first branch point that seems to be some internal null check or so. And I'm pretty sure that this is the problem. Because of that our instrumentation instruction for the 2nd sequence point is never hit. This can be seen in the screenshot of the IL that was posted before. Just to make it a bit more clear here again. I'm still trying to figure out a solution for that but every idea/thought on this is very welcome. |
Can you share your test branch so I can clone and debug? Btw the switch coverage seems good, right? I mean the case are correctly reported? Does it work with all other tests? We can try to release a first version with the "correct branch coverage" because it's not so important that last sequence (for the moment). And after try to skip compiler injected branches. Can you try to run in release? |
Sure but this is really early stage (https://github.com/daveMueller/coverlet/tree/936_SwitchExpression).
Yes cases are correctly reported.
Up to now I didn't test too much. I don't know if I broke something and if the other unit tests are still green.
See next answer.
I checked the IL of the release and this compiler injected branchpoint is gone. Now the result of the implementation is also looking good. 😏 Is it a requirement that coverlet also reports correctly for debug builds? If not I could ignore the problem I'm facing with the debug builds. |
Usually I ask to do coverage in debug because pdb is more precise/rich but with it there are also more false/true positive/negative. So my opinion here for now is that it's hard have a very precise % on coverage and usually a dev should check % but also take a look at report of branches/lines to understand the needs of more tests. In case of switch I prefer a correct branch cases coverage that a false negative on a bracket. Also because there are some place where is very very hard recognize correctly the pattern and so a false negative of 1 line shouldn't be a problem on % number. I'll go for this fix for now and after wait for feedbacks. Run a complete test and let me know! Thanks a lot for the effort! |
I got a question regarding coverage of a simple if statement. Has the line coverage for the I ask this because with my current implementation the test |
If it expected in test i think it was hit, you should try to understand why, I don't remember overlapped sequences but my memory could fail. Thanks a lot for the effort Dave. |
@daveMueller some more info on this, we can skip generated sequences/branches looking for this pattern https://github.com/dotnet/roslyn/blob/master/docs/compilers/CSharp/Expression%20Breakpoints.md |
@daveMueller how is going with this?Let me know if you're busy so I can go on based on your idea and Jakub hint to skip first branche. |
@MarcoRossignoli sorry was a bit busy lately on work. I've prepared PR today but then I've seen your PR and I also think this is the best and quickest solution. I add my thoughts to your PR. |
@MarcoRossignoli I see this issue is fixed, when do you think we can get the update? Thank you |
Asap, I would try to fix one last issue in record, but if won't work we plan to release before end of year |
Thank you.. 👍 |
I use coverlet.msbuild v2.9.0.

I use "dotnet test" task in Azure DevOps:
Somehow the execution of the switch expression is skipped, but that is impossible. The switch expression is executed for sure in my code flow.
However ReSharper shows the correct coverage.

The text was updated successfully, but these errors were encountered: