-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson_format_test.go
More file actions
114 lines (99 loc) · 2.73 KB
/
json_format_test.go
File metadata and controls
114 lines (99 loc) · 2.73 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
package mtlog_test
import (
"strings"
"testing"
"github.com/willibrandon/mtlog"
"github.com/willibrandon/mtlog/internal/parser"
"github.com/willibrandon/mtlog/sinks"
)
func TestJSONFormatSpecifier(t *testing.T) {
sink := sinks.NewMemorySink()
logger := mtlog.New(mtlog.WithSink(sink))
tests := []struct {
name string
template string
args []any
expected string
}{
{
name: "Simple map to JSON",
template: "Config: {Settings:j}",
args: []any{map[string]any{"debug": true, "port": 8080}},
expected: `{"debug":true,"port":8080}`,
},
{
name: "Struct to JSON",
template: "User: {User:j}",
args: []any{struct {
ID int `json:"id"`
Name string `json:"name"`
}{ID: 123, Name: "Alice"}},
expected: `{"id":123,"name":"Alice"}`,
},
{
name: "Nil value to JSON null",
template: "Value: {Val:j}",
args: []any{nil},
expected: "Value: null", // Include prefix to match full output
},
{
name: "Array to JSON",
template: "Tags: {Tags:j}",
args: []any{[]string{"admin", "user", "beta"}},
expected: `["admin","user","beta"]`,
},
{
name: "Complex nested structure",
template: "Data: {Data:j}",
args: []any{map[string]any{
"user": map[string]any{
"id": 123,
"name": "Bob",
},
"active": true,
}},
expected: `{"active":true,"user":{"id":123,"name":"Bob"}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sink.Clear()
logger.Information(tt.template, tt.args...)
events := sink.Events()
if len(events) != 1 {
t.Fatalf("Expected 1 event, got %d", len(events))
}
// Parse and render the message template
event := events[0]
mt, err := parser.Parse(event.MessageTemplate)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
rendered := mt.Render(event.Properties)
// Check if the JSON is in the output
if !strings.Contains(rendered, tt.expected) {
t.Errorf("Expected JSON %q in output, got: %q", tt.expected, rendered)
}
})
}
}
func TestJSONFormatWithQuotedStrings(t *testing.T) {
sink := sinks.NewMemorySink()
logger := mtlog.New(mtlog.WithSink(sink))
// Test combining :q and :j formats
logger.Information("String normal: {Name}, quoted: {Name:q}, JSON: {Name:j}", "Alice", "Alice", "Alice")
events := sink.Events()
if len(events) != 1 {
t.Fatalf("Expected 1 event, got %d", len(events))
}
event := events[0]
mt, err := parser.Parse(event.MessageTemplate)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
rendered := mt.Render(event.Properties)
expected := `String normal: Alice, quoted: "Alice", JSON: "Alice"`
if rendered != expected {
t.Errorf("Expected: %q\nGot: %q", expected, rendered)
}
}