Skip to content

[test-improver] Improve tests for strutil.RandomHex#3636

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/strutil-random-hex-d5d1f4153edcd604
Draft

[test-improver] Improve tests for strutil.RandomHex#3636
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/strutil-random-hex-d5d1f4153edcd604

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

File Analyzed

  • Test File: internal/strutil/random_hex_test.go
  • Package: internal/strutil
  • Lines of Code: 51 → 92

Improvements Made

1. Better Testing Patterns

  • ✅ Extended table-driven test with wantErr bool field to cover both success and error paths in a single unified table
  • ✅ Added descriptive test names for all new cases
  • ✅ Tests use require.Error / assert.Empty for error cases and require.NoError / assert.Len for success cases

2. Increased Coverage

  • ✅ Added n=0 edge case — zero bytes produces empty string with no error
  • ✅ Added n=-1 and n=-100 test cases — exercises the if n < 0 error branch that was previously untested
  • ✅ Added TestRandomHex_ErrorMessageContainsSize — verifies the error message includes the invalid size value
  • ✅ Added TestRandomHex_IsValidHex — verifies the output is a valid hex-encoded string and that hex.DecodeString round-trips back to n bytes

Previously uncovered code paths:

if n < 0 {
    return "", fmt.Errorf("failed to generate random bytes: negative size %d", n)
}

This branch was completely untested. The n==0 edge case (producing an empty string) was also not exercised.

3. Cleaner & More Stable Tests

  • ✅ Single table covers all n input variants (including errors), reducing the number of separately named test functions
  • ✅ New standalone tests focus on orthogonal properties (error message content, hex encoding validity)
  • ✅ Existing TestRandomHex_Uniqueness test preserved unchanged

Why These Changes?

RandomHex is 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

Generated by Test Improver · ● 4.9M ·

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants