-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ce9a8c
commit 319ed42
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package stacktrace | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestErrToSlogAttr(t *testing.T) { | ||
type args struct { | ||
err error | ||
opts []TracesOpt | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want slog.Attr | ||
}{ | ||
{ | ||
name: "Test simple", | ||
args: args{ | ||
err: New("error message", "location.raml"), | ||
opts: []TracesOpt{}, | ||
}, | ||
want: slog.Group( | ||
"tracebacks", "traces", []slog.Attr{ | ||
slog.Group( | ||
"0", "stack", []slog.Attr{ | ||
slog.Group( | ||
"0", | ||
slog.String("type", "parsing"), | ||
slog.String("severity", "error"), | ||
slog.String("position", "location.raml:1"), | ||
slog.String("message", "error message"), | ||
), | ||
}, | ||
), | ||
}, | ||
), | ||
}, | ||
{ | ||
name: "Test is not a stacktrace", | ||
args: args{ | ||
err: fmt.Errorf("error message"), | ||
opts: []TracesOpt{}, | ||
}, | ||
want: slog.String("error", "error message"), | ||
}, | ||
{ | ||
name: "Test nil err", | ||
args: args{ | ||
err: nil, | ||
opts: []TracesOpt{}, | ||
}, | ||
want: slog.Attr{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := ErrToSlogAttr(tt.args.err, tt.args.opts...); !reflect.DeepEqual(got, tt.want) { | ||
slog.Error("got", got) | ||
slog.Error("want", tt.want) | ||
t.Errorf("ErrToSlogAttr() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |