Repository navigation
Testing equality of function with two return values #1683
|
I have the following function which returns two values: func getUnsortedPairs(input string) (left []int, right []int) {
left = []int{}
right = []int{}
pairs := strings.Split(input, "\n")
for p := range pairs {
if len(pairs[p]) > 0 {
parts := strings.Fields(pairs[p])
leftPart, _ := strconv.Atoi(parts[0])
rightPart, _ := strconv.Atoi(parts[1])
left = append(left, leftPart)
right = append(right, rightPart)
}
}
return left, right
}I want to check that the two returned values match two expected values, ideally in a single assertion (I can split them across two assertions but then they're not tightly coupled, and they should be tested as a single 'what does this function return' check). This doesn't work, as it makes Equals think that the two return types are separate parameters: leftExpected := []int{}
rightExpected := []int{}
assert.Equal(t, leftExpected, rightExpected, getUnsortedPairs(""))This also doesn't work, with a syntax error: leftExpected := []int{}
rightExpected := []int{}
assert.Equal(t, (leftExpected, rightExpected), getUnsortedPairs(""))Is there a way to do what I want with a single assertion, or do I have to write my own assertion function or call assert.Equals for each return value? |
Answered by
brackendawson
Dec 2, 2024
Replies: 1 comment 1 reply
|
The idiomatic way to implement this in Go is this: leftExpected := []int{}
rightExpected := []int{}
left, right := getUnsortedPairs("")
assert.Equal(t, leftExpected, left)
assert.Equal(t, rightExpected, right)This is tightly coupled. |
1 reply
Answer selected by
pwaring
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The idiomatic way to implement this in Go is this:
This is tightly coupled.