2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2020-11-04 14:42:20 -05:00
|
|
|
package errors
|
|
|
|
|
|
|
|
|
|
import (
|
2021-07-29 18:52:56 -06:00
|
|
|
"context"
|
2020-11-04 14:42:20 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Test_getOpts provides unit tests for GetOpts and all the options
|
|
|
|
|
func Test_getOpts(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
t.Run("WithMsg", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
// test default
|
|
|
|
|
opts := GetOpts()
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withErrMsg = ""
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
|
|
|
|
|
// try setting it
|
|
|
|
|
opts = GetOpts(WithMsg("test msg"))
|
|
|
|
|
testOpts.withErrMsg = "test msg"
|
|
|
|
|
assert.Equal(opts, testOpts)
|
2022-06-03 10:38:32 -04:00
|
|
|
|
|
|
|
|
opts = GetOpts(WithMsg("%s msg", "test"))
|
|
|
|
|
testOpts.withErrMsg = "%s msg"
|
|
|
|
|
testOpts.withErrMsgArgs = []any{"test"}
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
|
|
|
|
|
assert.Equal("#1 test msg: unknown: error #0", E(context.Background(), WithMsg("#%d %s msg", 1, "test")).Error())
|
2020-11-04 14:42:20 -05:00
|
|
|
})
|
|
|
|
|
t.Run("WithWrap", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
// test default
|
|
|
|
|
opts := GetOpts()
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withErrWrapped = nil
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
|
2021-07-29 18:52:56 -06:00
|
|
|
testErr := E(context.TODO(), WithCode(InvalidParameter), WithMsg("test error"))
|
2021-03-05 06:34:06 -08:00
|
|
|
|
2020-11-04 14:42:20 -05:00
|
|
|
// try setting it
|
2021-03-05 06:34:06 -08:00
|
|
|
opts = GetOpts(WithWrap(testErr))
|
|
|
|
|
testOpts.withErrWrapped = testErr
|
2020-11-19 12:46:15 -08:00
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
|
|
|
|
t.Run("WithOp", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
// test default
|
|
|
|
|
opts := GetOpts()
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withOp = ""
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
|
|
|
|
|
// try setting it
|
|
|
|
|
opts = GetOpts(WithOp("alice.bob"))
|
|
|
|
|
testOpts.withOp = "alice.bob"
|
2020-11-04 14:42:20 -05:00
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
2020-12-09 10:11:38 -08:00
|
|
|
t.Run("WithCode", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
// test default
|
|
|
|
|
opts := GetOpts()
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withOp = ""
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
|
|
|
|
|
// try setting it
|
|
|
|
|
opts = GetOpts(WithCode(NotUnique))
|
|
|
|
|
testOpts.withCode = NotUnique
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
2021-07-29 18:52:56 -06:00
|
|
|
t.Run("WithoutEvent", func(t *testing.T) {
|
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
// test default
|
|
|
|
|
opts := GetOpts()
|
|
|
|
|
testOpts := getDefaultOptions()
|
|
|
|
|
testOpts.withoutEvent = false
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
|
|
|
|
|
// try setting it
|
|
|
|
|
opts = GetOpts(WithoutEvent())
|
|
|
|
|
testOpts.withoutEvent = true
|
|
|
|
|
assert.Equal(opts, testOpts)
|
|
|
|
|
})
|
2020-11-04 14:42:20 -05:00
|
|
|
}
|