Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Closing brace reported as not covered by tests
mwoinoski
#1 Posted : Sunday, June 24, 2018 5:53:59 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/24/2018(UTC)
Posts: 2
Location: United States of America

Thanks: 1 times
When testing the following block of code, NCrunch reports all code as covered by tests except the closing braces of the "if" blocks. But if I put the closing braces at the end of the preceding lines of code, NCrunch reports 100% coverage.

If I replace "yield return" with a plain "return", NCrunch reports 100% coverage as expected, so it looks like NCrunch isn't handling "yield return" correctly.

Is there a workaround for this doesn't require modifying the code?

Code:
if (Id <= 0)
{
    yield return new ValidationResult($"Account ID {Id} is invalid", pName);
}
else if (string.IsNullOrEmpty(Name) || string.IsNullOrWhiteSpace(Name))
{
    yield return new ValidationResult("Name must not be empty.", pName);
}
else if (Name.Length > NameMaxLength)
{
    yield return new ValidationResult($"Name '{Name}' is too long. " +
        $"Names must be no more than ${NameMaxLength} characters.", pName);
}


Thanks.
Remco
#2 Posted : Sunday, June 24, 2018 11:04:28 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,277

Thanks: 695 times
Was thanked: 856 time(s) in 814 post(s)
Hi, thanks for posting!

Looking at this code, it does actually make sense to me that the closing braces wouldn't be marked as covered. This is because they actually are not physically covered. You'll likely notice that if you place a breakpoint on any of these lines, you won't be able to break into the code.

This is because of the behaviour of the 'yield return' statement. Were you to place any lines directly after these statements inside the condition, they wouldn't be executed. The logical behaviour of a normal 'return' statement is to load the return value onto the evaluation stack and goto the end of the method, avoiding any further execution of other code in the method. In practice, 'yield return' probably tries to do the same thing, but it does it via a large compiler generated structure that actually bears little resemblance to your original code. My guess would be that the compiler's generation of this structure includes different execution path that results in the 'NOP' of the closing brace not being touched after the return fires.

There really is little that NCrunch can do here to try and report these lines as covered. Making these lines green or hidden would involve NCrunch introducing special behaviour to detect this structure and deliberately manipulating the execution results to make it appear as though something happened that didn't physically happen. That would result in loss in performance, obscurity, maintenance issues and potential risks of breaking important things if such behaviour were to go wrong. So this isn't something that we can fix in NCrunch.

If you're concerned about the effect this has on your metrics, I'd suggest removing the braces around the 'yield return' lines or using code coverage suppression comments.
1 user thanked Remco for this useful post.
mwoinoski on 6/24/2018(UTC)
mwoinoski
#3 Posted : Sunday, June 24, 2018 11:11:12 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/24/2018(UTC)
Posts: 2
Location: United States of America

Thanks: 1 times
" //ncrunch: no coverage" after the closing brace solves the problem. Thanks for the quick response!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.038 seconds.