Skip to content

test: add unit tests for TotalVotingPowerSafe#5570

Merged
technicallyty merged 3 commits intocometbft:mainfrom
bit2swaz:test/voting-power-safe-coverage
Jan 20, 2026
Merged

test: add unit tests for TotalVotingPowerSafe#5570
technicallyty merged 3 commits intocometbft:mainfrom
bit2swaz:test/voting-power-safe-coverage

Conversation

@bit2swaz
Copy link
Copy Markdown
Contributor

@bit2swaz bit2swaz commented Jan 17, 2026

Summary

This PR adds comprehensive unit tests for the TotalVotingPowerSafe method in types/validator_set.go, addressing the request in issue #5522.

Changes

  • Added TestValidatorSet_TotalVotingPowerSafe to types/validator_set_test.go.
  • Implemented a table-driven test structure using testify/require for strict assertions.
  • Added 11 test cases covering:
    • Happy Path: Standard validator sets with safe sums.
    • Zero State: Handling of nil and empty validator slices (expects 0, no error).
    • Boundaries: Sums exactly at MaxTotalVotingPower.
    • Overflow: Scenarios exceeding MaxTotalVotingPower (checking that it returns the expected error).

Related Issues

Closes #5522

PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (N/A: test only change)
  • Updated relevant documentation (docs/ or spec/) and code comments

Note

Adds comprehensive unit tests for TotalVotingPowerSafe in types/validator_set_test.go to validate summation behavior and overflow handling.

  • Introduces TestValidatorSet_TotalVotingPowerSafe with 11 cases: normal sums, nil/empty sets, single validator, boundaries at MaxTotalVotingPower, and multiple overflow scenarios (ensuring error with "exceeds maximum")
  • Verifies correct totals when within limits and zero result with error on overflow, without invoking NewValidatorSet to avoid panic

Written by Cursor Bugbot for commit a1931b6. This will update automatically on new commits. Configure here.

- Adds TestValidatorSet_TotalVotingPowerSafe table-driven test
- Covers happy paths, boundary conditions, and overflow scenarios
- Verifies nil/empty set handling

Closes cometbft#5522

Signed-off-by: bit2swaz <bit2swaz@gmail.com>
Copy link
Copy Markdown
Collaborator

@mattac21 mattac21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one small comment but looks good, thanks!

The 'tc := tc' pattern is no longer needed in modern Go versions (1.22+).
This addresses reviewer feedback to clean up the code.
@bit2swaz
Copy link
Copy Markdown
Contributor Author

@mattac21 my bad. i forgot go 1.22 fixed the loop variable semantics.

ive merged main and removed the redundant tc := tc capture. ready for another look :)

@bit2swaz bit2swaz requested a review from mattac21 January 20, 2026 16:18
@mattac21 mattac21 added this pull request to the merge queue Jan 20, 2026
@mattac21 mattac21 removed this pull request from the merge queue due to a manual request Jan 20, 2026
Copy link
Copy Markdown
Contributor

@technicallyty technicallyty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@technicallyty technicallyty added this pull request to the merge queue Jan 20, 2026
Merged via the queue into cometbft:main with commit c2ed98e Jan 20, 2026
35 checks passed
@technicallyty
Copy link
Copy Markdown
Contributor

@mergify backport v0.38.x

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jan 20, 2026

backport v0.38.x

✅ Backports have been created

Details

aljo242 added a commit that referenced this pull request Jan 21, 2026
## Summary
This PR adds comprehensive unit tests for the `TotalVotingPowerSafe`
method in `types/validator_set.go`, addressing the request in issue
#5522.

## Changes
- Added `TestValidatorSet_TotalVotingPowerSafe` to
`types/validator_set_test.go`.
- Implemented a table-driven test structure using `testify/require` for
strict assertions.
- Added 11 test cases covering:
  - **Happy Path:** Standard validator sets with safe sums.
- **Zero State:** Handling of nil and empty validator slices (expects 0,
no error).
  - **Boundaries:** Sums exactly at `MaxTotalVotingPower`.
- **Overflow:** Scenarios exceeding `MaxTotalVotingPower` (checking that
it returns the expected error).

## Related Issues
Closes #5522

#### PR checklist

- [x] Tests written/updated
- [ ] Changelog entry added in `.changelog` (N/A: `test` only change)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments


---

> [!NOTE]
> Adds comprehensive unit tests for `TotalVotingPowerSafe` in
`types/validator_set_test.go` to validate summation behavior and
overflow handling.
> 
> - Introduces `TestValidatorSet_TotalVotingPowerSafe` with 11 cases:
normal sums, nil/empty sets, single validator, boundaries at
`MaxTotalVotingPower`, and multiple overflow scenarios (ensuring error
with "exceeds maximum")
> - Verifies correct totals when within limits and zero result with
error on overflow, without invoking `NewValidatorSet` to avoid panic
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
a1931b6. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<hr>This is an automatic backport of pull request #5570 done by
[Mergify](https://mergify.com).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Adds comprehensive coverage for `TotalVotingPowerSafe` behavior and
overflow handling.
> 
> - Introduces `TestValidatorSet_TotalVotingPowerSafe` in
`types/validator_set_test.go`
> - Covers normal sums, nil/empty sets, single validator, boundaries at
`MaxTotalVotingPower`, and multiple overflow cases (asserting error
contains "exceeds maximum")
> - Bypasses `NewValidatorSet` to avoid panic during overflow scenarios;
uses `require` for strict assertions
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
bd517d2. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
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.

add unit tests for TotalVotingPowerSafe method in v0.38.x and main

3 participants