forked from palantir/stacktrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacktrace_test.go
105 lines (90 loc) · 3.23 KB
/
stacktrace_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 Palantir Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stacktrace_test
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/interuss/stacktrace"
)
func TestMessage(t *testing.T) {
err := startDoing()
err = PublicObj{}.DoPublic(err)
err = PublicObj{}.doPrivate(err)
err = privateObj{}.DoPublic(err)
err = privateObj{}.doPrivate(err)
err = (&ptrObj{}).doPtr(err)
err = doClosure(err)
expected := strings.Join([]string{
"so closed",
" --- at github.com/palantir/stacktrace/functions_for_test.go:51 (doClosure.func1) ---",
"Caused by: pointedly",
" --- at github.com/palantir/stacktrace/functions_for_test.go:46 (ptrObj.doPtr) ---",
" --- at github.com/palantir/stacktrace/functions_for_test.go:42 (privateObj.doPrivate) ---",
" --- at github.com/palantir/stacktrace/functions_for_test.go:38 (privateObj.DoPublic) ---",
" --- at github.com/palantir/stacktrace/functions_for_test.go:34 (PublicObj.doPrivate) ---",
" --- at github.com/palantir/stacktrace/functions_for_test.go:30 (PublicObj.DoPublic) ---",
"Caused by: failed to start doing",
" --- at github.com/palantir/stacktrace/functions_for_test.go:26 (startDoing) ---",
}, "\n")
stacktrace.DefaultFormat = stacktrace.FormatFull
assert.Equal(t, expected, err.Error())
assert.Equal(t, expected, fmt.Sprint(err))
}
func TestGetCode(t *testing.T) {
for _, test := range []struct {
originalError error
originalCode stacktrace.ErrorCode
}{
{
originalError: errors.New("err"),
originalCode: stacktrace.NoCode,
},
{
originalError: stacktrace.NewError("err"),
originalCode: stacktrace.NoCode,
},
{
originalError: stacktrace.NewErrorWithCode(EcodeInvalidVillain, "err"),
originalCode: EcodeInvalidVillain,
},
{
originalError: stacktrace.NewMessageWithCode(EcodeNoSuchPseudo, "err"),
originalCode: EcodeNoSuchPseudo,
},
} {
err := test.originalError
assert.Equal(t, test.originalCode, stacktrace.GetCode(err))
err = stacktrace.Propagate(err, "")
assert.Equal(t, test.originalCode, stacktrace.GetCode(err))
err = stacktrace.PropagateWithCode(err, EcodeNotFastEnough, "")
assert.Equal(t, EcodeNotFastEnough, stacktrace.GetCode(err))
err = stacktrace.PropagateWithCode(err, EcodeTimeIsIllusion, "")
assert.Equal(t, EcodeTimeIsIllusion, stacktrace.GetCode(err))
}
}
func TestPropagateNil(t *testing.T) {
var err error
err = stacktrace.Propagate(err, "")
assert.Nil(t, err)
err = stacktrace.PropagateWithCode(err, EcodeNotImplemented, "")
assert.Nil(t, err)
assert.Equal(t, stacktrace.NoCode, stacktrace.GetCode(err))
}
func TestUnwrap(t *testing.T) {
err := errors.New("cause")
assert.Equal(t, err, unwrap(stacktrace.Propagate(err, "propagated")))
}