Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support order assertions on sort.Interface types #1122

Open
sjiekak opened this issue Oct 27, 2021 · 3 comments · May be fixed by #1124
Open

Support order assertions on sort.Interface types #1122

sjiekak opened this issue Oct 27, 2021 · 3 comments · May be fixed by #1124

Comments

@sjiekak
Copy link

sjiekak commented Oct 27, 2021

Given the following type implementing the sort interface

type segment struct {
    left int
    right int
}

type segments []segment

func (l *segments) Len() int {
	return len([]segment(*l))
}

func (l *segments) Less(i, j int) bool {
	data := []segment(*l)
	return data[i].left < data[j].left  || (data[i].left == data[j].left && data[i].right < data[j].right)
}

func (l *segments) Swap(i, j int) {
	data := []segment(*l)
	data[i], data[j] = data[j], data[i]
}

I should be able to perform sort assertions

sample := segments([]segment{
{1, 2},
{3, 4},
})
assert.IsIncreasing(t, &sample)
@brackendawson
Copy link
Collaborator

How about:

assert.True(t, sort.IsSorted(&sample))

I think if we had something like assert.IsSorted() it should also work on types that don't implement sort.Interface, it could also check if slices of Strings, Ints and Float64s are sorted, as per the functions in the sort package?

@sjiekak
Copy link
Author

sjiekak commented Oct 27, 2021

Yes I am already using

assert.True(t, sort.IsSorted(&sample))

The goal of my request is precisely to use functions such as IsIncreasing and IsNonDecreasing such as :

  • types which implement sort.Interface may succeed assertion
  • other types (other than string, int, etc...) fail the assertion

@brackendawson
Copy link
Collaborator

brackendawson commented Oct 28, 2021

Ah, apologies, I totally missed that IsIncreasing is already a function. Yes, it should be trivial to make these support collections implementing sort.Interface.

@brackendawson brackendawson linked a pull request Oct 29, 2021 that will close this issue
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 a pull request may close this issue.

2 participants