Skip to content

Commit

Permalink
allow to test with color
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Mar 29, 2024
1 parent c4b4292 commit 0bf75b4
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
AddSource: true,
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:100 test key=val`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:101 test key=val`,
},
{
Opts: &tint.Options{
TimeFormat: time.Kitchen,
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -113,6 +115,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: drop(slog.TimeKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -122,6 +125,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: drop(slog.LevelKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -131,6 +135,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: drop(slog.MessageKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -140,6 +145,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: drop(slog.TimeKey, slog.LevelKey, slog.MessageKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -149,6 +155,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: drop("key"),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -158,6 +165,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: drop("key"),
NoColor: true,
},
F: func(l *slog.Logger) {
l.WithGroup("group").Info("test", "key", "val", "key2", "val2")
Expand All @@ -172,6 +180,7 @@ func TestHandler(t *testing.T) {
}
return a
},
NoColor: true,
},
F: func(l *slog.Logger) {
l.WithGroup("group").Info("test", "key", "val", "key2", "val2")
Expand All @@ -181,6 +190,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: replace(slog.IntValue(42), slog.TimeKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -190,6 +200,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: replace(slog.StringValue("INFO"), slog.LevelKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -199,6 +210,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: replace(slog.IntValue(42), slog.MessageKey),
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand All @@ -208,6 +220,7 @@ func TestHandler(t *testing.T) {
{
Opts: &tint.Options{
ReplaceAttr: replace(slog.IntValue(42), "key"),
NoColor: true,
},
F: func(l *slog.Logger) {
l.With("key", "val").Info("test", "key2", "val2")
Expand All @@ -219,6 +232,7 @@ func TestHandler(t *testing.T) {
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
return slog.Attr{}
},
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
Expand Down Expand Up @@ -252,7 +266,8 @@ func TestHandler(t *testing.T) {
},
{
Opts: &tint.Options{
Level: slog.LevelDebug - 1,
Level: slog.LevelDebug - 1,
NoColor: true,
},
F: func(l *slog.Logger) {
l.Log(context.TODO(), slog.LevelDebug-1, "test")
Expand All @@ -279,6 +294,7 @@ func TestHandler(t *testing.T) {
}
return a
},
NoColor: true,
},
F: func(l *slog.Logger) {
l.Error("test")
Expand All @@ -296,6 +312,7 @@ func TestHandler(t *testing.T) {
Opts: &tint.Options{
ReplaceAttr: drop(slog.TimeKey, slog.LevelKey, slog.MessageKey, slog.SourceKey),
AddSource: true,
NoColor: true,
},
F: func(l *slog.Logger) {
l.WithGroup("group").Info("test", "key", "val")
Expand All @@ -310,6 +327,7 @@ func TestHandler(t *testing.T) {
}
return a
},
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test")
Expand All @@ -322,11 +340,12 @@ func TestHandler(t *testing.T) {
ReplaceAttr: func(g []string, a slog.Attr) slog.Attr {
return a
},
NoColor: true,
},
F: func(l *slog.Logger) {
l.Info("test")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:327 test`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:346 test`,
},
{ // https://github.com/lmittmann/tint/issues/44
F: func(l *slog.Logger) {
Expand All @@ -350,9 +369,8 @@ func TestHandler(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var buf bytes.Buffer
if test.Opts == nil {
test.Opts = &tint.Options{}
test.Opts = &tint.Options{NoColor: true}
}
test.Opts.NoColor = true
l := slog.New(tint.NewHandler(&buf, test.Opts))
test.F(l)

Expand Down

0 comments on commit 0bf75b4

Please sign in to comment.