552614aee8
The ClampInt() function does not always return a value between its "min" and "max" arguments because it reverses their meaning, and the relevant tests in the corresponding tools/math_test.go file do not report the problem because they do not execute when the "go test ./tools" command is run. The latter bug is a result of the test functions lacking the necessary Test* prefix to be recognized by the Go "test" command. After we fix the ClampInt() function, we rename all the test functions in tools/math_test.go to follow the Test*() naming scheme, which ensures they now run properly. Lastly, we change the one caller of the ClampInt() function, the migrateInfoCommand() function, which previously worked correctly because it also reversed the order of the arguments it passed to the function. That reversal was introduced in commit bd2e1b27368745f4014a448c371fd72c658ef9d6 in PR #2313, as part of the development of the "migrate info" command, presumably because the author discovered the call was not working as expected.
28 lines
546 B
Go
28 lines
546 B
Go
package tools
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMinIntPicksTheSmallerInt(t *testing.T) {
|
|
assert.Equal(t, -1, MinInt(-1, 1))
|
|
}
|
|
|
|
func TestMaxIntPicksTheBiggertInt(t *testing.T) {
|
|
assert.Equal(t, 1, MaxInt(-1, 1))
|
|
}
|
|
|
|
func TestClampDiscardsIntsLowerThanMin(t *testing.T) {
|
|
assert.Equal(t, 0, ClampInt(-1, 0, 1))
|
|
}
|
|
|
|
func TestClampDiscardsIntsGreaterThanMax(t *testing.T) {
|
|
assert.Equal(t, 1, ClampInt(2, 0, 1))
|
|
}
|
|
|
|
func TestClampAcceptsIntsWithinBounds(t *testing.T) {
|
|
assert.Equal(t, 1, ClampInt(1, 0, 2))
|
|
}
|