MCPcopy Index your code
hub / github.com/cockroachdb/errors / TestSimplifyStacks

Function TestSimplifyStacks

errbase/stack_format_test.go:28–100  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestSimplifyStacks(t *testing.T) {
29 leaf := func() error {
30 return pkgErr.New("hello world")
31 }
32 wrapper := func() error {
33 err := leaf()
34 return pkgErr.WithStack(err)
35 }
36 errWrapper := wrapper()
37 t.Logf("error: %+v", errWrapper)
38
39 t.Run("low level API", func(t *testing.T) {
40 tt := testutils.T{t}
41 // Extract the stack trace from the leaf.
42 errLeaf := errbase.UnwrapOnce(errWrapper)
43 leafP, ok := errLeaf.(errbase.StackTraceProvider)
44 if !ok {
45 t.Fatal("leaf error does not provide stack trace")
46 }
47 leafT := leafP.StackTrace()
48 spv := fmtClean(leafT)
49 t.Logf("-- leaf trace --%+v", spv)
50 if !strings.Contains(spv, "TestSimplifyStacks") {
51 t.Fatalf("expected test function in trace, got:%v", spv)
52 }
53 leafLines := strings.Split(spv, "\n")
54
55 // Extract the stack trace from the wrapper.
56 wrapperP, ok := errWrapper.(errbase.StackTraceProvider)
57 if !ok {
58 t.Fatal("wrapper error does not provide stack trace")
59 }
60 wrapperT := wrapperP.StackTrace()
61 spv = fmtClean(wrapperT)
62 t.Logf("-- wrapper trace --%+v", spv)
63 wrapperLines := strings.Split(spv, "\n")
64
65 // Sanity check before we verify the result.
66 tt.Check(len(wrapperLines) > 0)
67 tt.CheckDeepEqual(wrapperLines[3:], leafLines[5:])
68
69 // Elide the suffix and verify that we arrive to the same result.
70 simplified, hasElided := errbase.ElideSharedStackTraceSuffix(leafT, wrapperT)
71 spv = fmtClean(simplified)
72 t.Logf("-- simplified (%v) --%+v", hasElided, spv)
73 simplifiedLines := strings.Split(spv, "\n")
74 tt.CheckDeepEqual(simplifiedLines, wrapperLines[0:3])
75 })
76
77 t.Run("high level API", func(t *testing.T) {
78 tt := testutils.T{t}
79
80 spv := fmtClean(errbase.Formattable(errWrapper))
81 tt.CheckStringEqual(spv, `hello world
82(1)
83 -- stack trace:
84 | github.com/cockroachdb/errors/errbase_test.TestSimplifyStacks.func2
85 | <tab><path>:<lineno>

Callers

nothing calls this directly

Calls 9

CheckMethod · 0.95
CheckDeepEqualMethod · 0.95
CheckStringEqualMethod · 0.95
UnwrapOnceFunction · 0.92
FormattableFunction · 0.92
RunMethod · 0.80
fmtCleanFunction · 0.70
StackTraceMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…