-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes_test.go
More file actions
133 lines (106 loc) · 3.41 KB
/
attributes_test.go
File metadata and controls
133 lines (106 loc) · 3.41 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Copyright (C) 2021 - 2025 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/logger
//
package logger
import (
"log/slog"
"path/filepath"
"testing"
"time"
)
func TestAttrLogTimeFormat(t *testing.T) {
format := "2006-01-02 15:04:05"
attr := attrLogTimeFormat(format)
timestamp := time.Now()
a := slog.Attr{Key: slog.TimeKey, Value: slog.AnyValue(timestamp)}
result := attr(nil, a)
expected := timestamp.Format(format)
if result.Value.String() != expected {
t.Errorf("expected %s, got %s", expected, result.Value.String())
}
}
func TestAttrNoTimestamp(t *testing.T) {
attr := attrNoTimestamp
timestamp := time.Now()
a := slog.Attr{Key: slog.TimeKey, Value: slog.AnyValue(timestamp)}
result := attr(nil, a)
if result.Key != "" {
t.Errorf("expected empty key, got %s", result.Key)
}
result = attr([]string{"group"}, a)
if result.Key != slog.TimeKey {
t.Errorf("expected %s, got %s", slog.TimeKey, result.Key)
}
}
func TestAttrLogLevel7(t *testing.T) {
attr := attrLogLevel7(false)
level := slog.LevelInfo
a := slog.Attr{Key: slog.LevelKey, Value: slog.AnyValue(level)}
result := attr(nil, a)
expected := levelLongName[level]
if result.Value.String() != expected {
t.Errorf("expected %s, got %s", expected, result.Value.String())
}
}
func TestAttrLogLevel7Shorten(t *testing.T) {
attr := attrLogLevel7Shorten(false)
level := slog.LevelInfo
a := slog.Attr{Key: slog.LevelKey, Value: slog.AnyValue(level)}
result := attr(nil, a)
expected := levelShortName[level]
if result.Value.String() != expected {
t.Errorf("expected %s, got %s", expected, result.Value.String())
}
}
func TestAttrSourceFileName(t *testing.T) {
attr := attrSourceFileName
source := &slog.Source{File: "/path/to/file.go"}
a := slog.Attr{Key: slog.SourceKey, Value: slog.AnyValue(source)}
result := attr(nil, a)
expected := filepath.Base(source.File)
if result.Value.Any().(*slog.Source).File != expected {
t.Errorf("expected %s, got %s", expected, result.Value.Any().(*slog.Source).File)
}
}
func TestAttrSourceShorten(t *testing.T) {
attr := attrSourceShorten
source := &slog.Source{File: "/path/to/go/src/github.com/fogfish/logger/attributes.go", Function: "github.com/fogfish/logger.TestFunc"}
a := slog.Attr{Key: slog.SourceKey, Value: slog.AnyValue(source)}
result := attr(nil, a)
expectedFile := "gthb.fgfs.lggr/attributes.go"
expectedFunc := "gthb.fgfs/logger.TestFunc"
if result.Value.Any().(*slog.Source).File != expectedFile {
t.Errorf("expected %s, got %s", expectedFile, result.Value.Any().(*slog.Source).File)
}
if result.Value.Any().(*slog.Source).Function != expectedFunc {
t.Errorf("expected %s, got %s", expectedFunc, result.Value.Any().(*slog.Source).Function)
}
}
func TestShorten(t *testing.T) {
path := "github.com/fogfish/logger/attributes.go"
expected := "gthb.fgfs.lggr/attributes.go"
result := shorten(path)
if result != expected {
t.Errorf("expected %s, got %s", expected, result)
}
}
func TestShortenSegment(t *testing.T) {
segment := "logger"
expected := "lggr"
result := shortenSegment(segment)
if result != expected {
t.Errorf("expected %s, got %s", expected, result)
}
}
func TestRemoveVowels(t *testing.T) {
segment := "logger"
expected := "lggr"
result := removeVowels(segment)
if result != expected {
t.Errorf("expected %s, got %s", expected, result)
}
}