Skip to content

assert.IsIncreasing et al: support sort.Interface - #1124

Open
brackendawson wants to merge 4 commits into
stretchr:masterfrom
brackendawson:sort-interface
Open

assert.IsIncreasing et al: support sort.Interface#1124
brackendawson wants to merge 4 commits into
stretchr:masterfrom
brackendawson:sort-interface

Conversation

@brackendawson

Copy link
Copy Markdown
Collaborator

Summary

Support collections which implement sort.Interface as objects to IsIncreasing, IsNonDecreasing, IsDecreasing & IsNonIncreasing.

Changes

  • Fixed unrelated bug in IsIncreasing, IsNonDecreasing, IsDecreasing & IsNonIncreasing, if a non-collection is passed as object then the assertion returns false without failing the test. 😱
  • Make IsIncreasing, IsNonDecreasing, IsDecreasing & IsNonIncreasing into helper functions, they already should be and their generated siblings already are.
  • If object passed to IsIncreasing, IsNonDecreasing, IsDecreasing or IsNonIncreasing implements sort.Interface, check its ordering using that interface. This is done first as a performance concern because the algorithm does not use reflection.

Motivation

If you have a collection which implements sort.Interface, you can now assert the four ordering primitives against it. For IsIncreasing this could have been expressed as assert.True(t, sort.IsSorted(object)), but for IsNonDecreasing, IsDecreasing or IsNonIncreasing you would have to write the algorithm in the test.

Related issues

closes #1122

@brackendawson brackendawson changed the title Sort interface Support sort.Interface in IsIncreasing et al. Oct 29, 2021
If you passed a non-collection to IsIncreasing or any of its compatriots then the assertion would return false without failing the test.
The auto-generated functions are already correct
The test was failing in github actions.
This change moves a possible allocation in NewTimer() out of the window between the asyncCall starting and the timer elapsing. As the allocation would be before the timer is started this does not risk making the timer effectively 0ms.
@dolmen

dolmen commented Jul 21, 2023

Copy link
Copy Markdown
Collaborator

This will have to be rebased once #1430 is merged.

@dolmen dolmen changed the title Support sort.Interface in IsIncreasing et al. assert.IsIncreasing et al: support sort.Interface May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support order assertions on sort.Interface types

3 participants