87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package ut
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type optional struct {
|
|
err error
|
|
exec bool
|
|
}
|
|
|
|
func (o *optional) Invoke() error {
|
|
o.exec = true
|
|
return o.err
|
|
}
|
|
|
|
func TestDeopterInvoked(t *testing.T) {
|
|
var o = &optional{}
|
|
var d = NewDeopter(o.Invoke)
|
|
|
|
var err = d.Invoke()
|
|
|
|
assert.Nil(t, err)
|
|
assert.True(t, o.exec)
|
|
}
|
|
|
|
func TestDeopterOptOut(t *testing.T) {
|
|
var o = &optional{}
|
|
var d = NewDeopter(o.Invoke)
|
|
|
|
d.Deopt()
|
|
var err = d.Invoke()
|
|
|
|
assert.Nil(t, err)
|
|
assert.False(t, o.exec)
|
|
}
|
|
|
|
func TestDeopterErrorOnInvocation(t *testing.T) {
|
|
var o = &optional{err: errors.New("err")}
|
|
var d = NewDeopter(o.Invoke)
|
|
|
|
var err = d.Invoke()
|
|
|
|
assert.Equal(t, o.err, err)
|
|
}
|
|
|
|
func TestDeopterMultipleInvocations(t *testing.T) {
|
|
var o1, o2 = &optional{}, &optional{}
|
|
var d = NewDeopter(o1.Invoke, o2.Invoke)
|
|
|
|
var err = d.Invoke()
|
|
|
|
assert.Nil(t, err)
|
|
assert.True(t, o1.exec)
|
|
assert.True(t, o2.exec)
|
|
}
|
|
|
|
func TestDeopterMultipleErrors(t *testing.T) {
|
|
var o1, o2 = &optional{err: errors.New("err1")}, &optional{err: errors.New("err2")}
|
|
var d = NewDeopter(o1.Invoke, o2.Invoke)
|
|
|
|
var err = d.Invoke()
|
|
|
|
assert.NotNil(t, err)
|
|
require.IsType(t, &Errors{}, err)
|
|
var errs = err.(*Errors).Errs
|
|
require.Equal(t, 2, len(errs))
|
|
assert.True(t, o1.exec)
|
|
assert.True(t, o2.exec)
|
|
}
|
|
|
|
func TestDeopterCanAdd(t *testing.T) {
|
|
var o1, o2 = &optional{}, &optional{}
|
|
var d = NewDeopter(o1.Invoke)
|
|
|
|
d.Add(o2.Invoke)
|
|
|
|
var err = d.Invoke()
|
|
assert.Nil(t, err)
|
|
assert.True(t, o1.exec)
|
|
assert.True(t, o2.exec)
|
|
}
|