Skip to content

Conversation

@bukzor
Copy link
Contributor

@bukzor bukzor commented Oct 22, 2025

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:

  • 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: #162

@bukzor bukzor marked this pull request as ready for review October 22, 2025 18:15
@bukzor bukzor force-pushed the fix-160-exhaustive-switches branch from 93ecda9 to a780997 Compare October 22, 2025 20:57
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
@bukzor bukzor force-pushed the fix-160-exhaustive-switches branch from a780997 to 6b932e3 Compare November 14, 2025 17:34
@codecov-commenter
Copy link

codecov-commenter commented Nov 14, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 52.38095% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.85%. Comparing base (ec5a59a) to head (6ddf893).

Files with missing lines Patch % Lines
internal/utils/utils.go 12.50% 7 Missing ⚠️
internal/utils/jsonutil.go 83.33% 2 Missing ⚠️
internal/utils/config.go 0.00% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- 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.
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