| 68 | } |
| 69 | |
| 70 | func TestWriteTree(t *testing.T) { |
| 71 | type testFrame struct { |
| 72 | Function string |
| 73 | File string |
| 74 | Line int |
| 75 | } |
| 76 | |
| 77 | // Helpers to make tests more readable. |
| 78 | type frames = []testFrame |
| 79 | tree := func(err error, trace frames, children ...traceTree) traceTree { |
| 80 | runtimeFrames := make([]runtime.Frame, len(trace)) |
| 81 | for i, f := range trace { |
| 82 | runtimeFrames[i] = runtime.Frame{ |
| 83 | Function: f.Function, |
| 84 | File: f.File, |
| 85 | Line: f.Line, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return traceTree{ |
| 90 | Err: err, |
| 91 | Trace: runtimeFrames, |
| 92 | Children: children, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | tests := []struct { |
| 97 | name string |
| 98 | give traceTree |
| 99 | want []string // lines minus trailing newline |
| 100 | }{ |
| 101 | { |
| 102 | name: "top level single error", |
| 103 | give: tree( |
| 104 | errors.New("test error"), |
| 105 | frames{ |
| 106 | {"foo", "foo.go", 42}, |
| 107 | {"bar", "bar.go", 24}, |
| 108 | }, |
| 109 | ), |
| 110 | want: []string{ |
| 111 | "test error", |
| 112 | "", |
| 113 | "foo", |
| 114 | " foo.go:42", |
| 115 | "bar", |
| 116 | " bar.go:24", |
| 117 | }, |
| 118 | }, |
| 119 | { |
| 120 | name: "multi error without trace", |
| 121 | give: tree( |
| 122 | errors.Join( |
| 123 | errors.New("err a"), |
| 124 | errors.New("err b"), |
| 125 | ), |
| 126 | frames{}, |
| 127 | tree(errors.New("err a"), frames{ |