[test-improver] Improve tests for strutil.RandomHex#3636
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test-improver] Improve tests for strutil.RandomHex#3636github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
Add missing coverage for error paths and edge cases: - Add wantErr bool field to table-driven test to cover negative n values - Add zero-byte edge case (n=0 produces empty string with no error) - Add negative n test cases (-1, -100) exercising the error branch - Add TestRandomHex_ErrorMessageContainsSize to verify error includes size - Add TestRandomHex_IsValidHex to verify output is valid hex encoding Previously only the happy path (positive n) was tested, leaving the n<0 error branch and the n==0 edge case uncovered. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
File Analyzed
internal/strutil/random_hex_test.gointernal/strutilImprovements Made
1. Better Testing Patterns
wantErr boolfield to cover both success and error paths in a single unified tablerequire.Error/assert.Emptyfor error cases andrequire.NoError/assert.Lenfor success cases2. Increased Coverage
n=0edge case — zero bytes produces empty string with no errorn=-1andn=-100test cases — exercises theif n < 0error branch that was previously untestedTestRandomHex_ErrorMessageContainsSize— verifies the error message includes the invalid size valueTestRandomHex_IsValidHex— verifies the output is a valid hex-encoded string and thathex.DecodeStringround-trips back tonbytesPreviously uncovered code paths:
This branch was completely untested. The
n==0edge case (producing an empty string) was also not exercised.3. Cleaner & More Stable Tests
ninput variants (including errors), reducing the number of separately named test functionsTestRandomHex_Uniquenesstest preserved unchangedWhy These Changes?
RandomHexis a foundational utility used for generating API keys and session IDs. Its error branch for negative inputs was entirely untested — any regression there (e.g., panic instead of error, wrong error message) would go undetected. The zero-byte case is a valid boundary that deserves explicit documentation as a test. The hex-encoding validity test provides a round-trip guarantee, ensuring future changes to the encoding format are caught early.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests