-
Notifications
You must be signed in to change notification settings - Fork 10
/
attributes_test.go
102 lines (82 loc) · 2.24 KB
/
attributes_test.go
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
package devslog
import (
"log/slog"
"testing"
)
func Test_Attributes(t *testing.T) {
test_AttributesLen(t)
test_AttributesSwap(t)
test_AttributesLess(t)
test_AttributesLessGroupTrue(t)
test_AttributesLessGroupFalse(t)
test_AttributesPadding(t)
}
func test_AttributesLen(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
slog.Attr{Key: "key2", Value: someValue},
slog.Attr{Key: "key3", Value: someValue},
}
expectedLen := 3
actualLen := attrs.Len()
if actualLen != expectedLen {
t.Errorf("Expected length: %d, but got: %d", expectedLen, actualLen)
}
}
func test_AttributesSwap(t *testing.T) {
attr1 := slog.Attr{Key: "key1", Value: slog.StringValue("value")}
attr2 := slog.Attr{Key: "key2", Value: slog.StringValue("value")}
attrs := attributes{
attr1,
attr2,
}
attrs.Swap(0, 1)
if attrs[0].Key != attr2.Key || attrs[1].Key != attr1.Key {
t.Error("attributes were not swapped correctly")
}
}
func test_AttributesLess(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
slog.Attr{Key: "key2", Value: someValue},
}
less := attrs.Less(0, 1)
if !less {
t.Error("Expected the first attribute to be less than the second")
}
}
func test_AttributesLessGroupTrue(t *testing.T) {
attrs := attributes{
slog.String("key1", "someValue"),
slog.Group("key2", slog.String("someString", "someValue")),
}
less := attrs.Less(0, 1)
if !less {
t.Error("Expected the first attribute to be less than the second")
}
}
func test_AttributesLessGroupFalse(t *testing.T) {
attrs := attributes{
slog.Group("key1", slog.String("someString", "someValue")),
slog.String("key2", "someValue"),
}
less := attrs.Less(0, 1)
if less {
t.Error("Expected the first attribute to be less than the second")
}
}
func test_AttributesPadding(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
slog.Attr{Key: "key2", Value: someValue},
}
h := NewHandler(nil, nil)
padding := attrs.padding(fgMagenta, h.cs)
expectedPadding := 13
if padding != expectedPadding {
t.Errorf("Expected padding: %d, but got: %d", expectedPadding, padding)
}
}