forked from dedis/cothority
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
45 lines (35 loc) · 1.19 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cothority
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
)
var errExample = xerrors.New("example")
func makeError() error {
return xerrors.Errorf("oops: %w", errExample)
}
// Test that the basic function create an error when the parameter
// is not nil, and returns nil otherwise.
func TestError_ErrorOrNil(t *testing.T) {
err := ErrorOrNil(makeError(), "test")
require.Equal(t, "test: oops: example", err.Error())
require.Nil(t, ErrorOrNil(nil, ""))
}
// Test that the skip option is correctly used to prevent a call
// to be included in the stack trace.
func TestError_ErrorOrNilSkip(t *testing.T) {
err := ErrorOrNilSkip(makeError(), "test", 2)
println(t.Name())
require.NotContains(t, fmt.Sprintf("%+v", err), t.Name())
require.Contains(t, fmt.Sprintf("%+v", err), ".makeError")
}
// Test that the wrapper is invisible but allows the error
// comparison to work.
func TestError_WrapError(t *testing.T) {
err := WrapError(makeError())
require.Equal(t, "oops: example", err.Error())
require.Contains(t, fmt.Sprintf("%+v", err), ".makeError")
require.True(t, xerrors.Is(err, errExample))
require.False(t, xerrors.Is(err, xerrors.New("abc")))
}