refactor a bit

This commit is contained in:
Evan Simkowitz 2023-12-12 17:39:19 -08:00
parent 96f636c2da
commit 5d39815dea
No known key found for this signature in database

View File

@ -51,47 +51,34 @@ func TestDiff(t *testing.T) {
testDiff(t, Str3, Str1)
}
const unexpectedError = "unexpected error"
const expectedError = "expected error"
const wrongRetVal = "wrong return value"
func testAddInt(t *testing.T, a int, b int, shouldError bool, expected int) {
retVal, err := AddInt(a, b)
func testArithmetic(t *testing.T, fn func() (int, error), shouldError bool, expected int) { // nolint: unparam
retVal, err := fn()
if err != nil {
if !shouldError {
t.Errorf(unexpectedError)
t.Errorf("unexpected error")
}
return
}
if shouldError {
t.Errorf(expectedError)
t.Errorf("expected error")
return
}
if retVal != expected {
t.Errorf(wrongRetVal)
t.Errorf("wrong return value")
}
}
func testAddInt(t *testing.T, a int, b int, shouldError bool, expected int) {
testArithmetic(t, func() (int, error) { return AddInt(a, b) }, shouldError, expected)
}
func TestAddInt(t *testing.T) {
testAddInt(t, 1, 2, false, 3)
testAddInt(t, 1, math.MaxInt, true, 0)
}
func testAddIntSlice(t *testing.T, shouldError bool, expected int, vals ...int) {
retVal, err := AddIntSlice(vals...)
if err != nil {
if !shouldError {
t.Errorf(unexpectedError)
}
return
}
if shouldError {
t.Errorf(expectedError)
return
}
if retVal != expected {
t.Errorf(wrongRetVal)
}
testArithmetic(t, func() (int, error) { return AddIntSlice(vals...) }, shouldError, expected)
}
func TestAddIntSlice(t *testing.T) {