Ensure exhaustive switch statements#164
Open
bukzor wants to merge 2 commits intosibprogrammer:masterfrom
Open
Conversation
93ecda9 to
a780997
Compare
PR sibprogrammer#162 revealed that missing node type handlers in switch statements cause silent data loss - CDATA content was being dropped because CharDataNode wasn't handled. This PR adds defensive programming to prevent this entire class of bug. Changes: - Added exhaustive switch coverage for all xmlquery.NodeType values - Added exhaustive switch coverage for xml.Token and html.TokenType - Added exhaustive switch coverage for json.Token and json.Delim - Added default cases with panic() to all exhaustive switches - Added explicit documented defaults to intentionally non-exhaustive switches This follows the Go stdlib pattern (runtime/panic.go, go/types) of using panic("unreachable") for "should be impossible" states. If future xmlquery versions add new node types, or if we overlook a handler, tests will fail immediately rather than silently corrupting output. All switches now explicitly handle defaults: - 8 exhaustive switches panic on unknown values - 7 non-exhaustive switches have documented intentional behavior Added TestExhaustiveNodeTypeHandling to verify all node types are processed without panicking. All existing tests pass. This is a non-breaking change - no API modifications, only defensive additions to switch statements. Related: sibprogrammer#162
a780997 to
6b932e3
Compare
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #164 +/- ##
==========================================
- Coverage 80.57% 79.85% -0.73%
==========================================
Files 5 5
Lines 690 710 +20
==========================================
+ Hits 556 567 +11
- Misses 92 101 +9
Partials 42 42 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Test all 4 panic cases with invalid NodeType(255) values - Achieves 100% coverage of jsonutil.go panic guards - Verifies defensive programming works as intended The 4 utils.go formatter panics remain untested as they would require refactoring to inject mock tokens (stdlib parsers control token creation). The jsonutil.go panics are fully tested and prove the defensive guards work correctly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR #162 revealed that missing node type handlers in switch statements
cause silent data loss - CDATA content was being dropped because
CharDataNode wasn't handled. This PR adds defensive programming to
prevent this entire class of bug.
Changes:
This follows the Go stdlib pattern (runtime/panic.go, go/types) of using
panic("unreachable") for "should be impossible" states. If future xmlquery
versions add new node types, or if we overlook a handler, tests will fail
immediately rather than silently corrupting output.
All switches now explicitly handle defaults:
Added TestExhaustiveNodeTypeHandling to verify all node types are
processed without panicking. All existing tests pass.
This is a non-breaking change - no API modifications, only defensive
additions to switch statements.
Related: #162