-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
notice_test.go
148 lines (116 loc) · 4.77 KB
/
notice_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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package sprout
import (
"bytes"
"context"
"fmt"
"log/slog"
"reflect"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type noticeLoggerHandler struct {
messages bytes.Buffer
}
func (h *noticeLoggerHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
func (h *noticeLoggerHandler) Handle(_ context.Context, r slog.Record) error {
msg := fmt.Sprintf("[%s] %s\n", r.Level.String(), r.Message)
h.messages.Write([]byte(msg))
return nil
}
func (h *noticeLoggerHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return h
}
func (h *noticeLoggerHandler) WithGroup(name string) slog.Handler {
return h
}
func TestWithNotice(t *testing.T) {
handler := New()
originalFunc := "originalFunc"
notice := NewInfoNotice(originalFunc, "amazing")
// Apply the WithNotices option with one notice.
require.NoError(t, WithNotices(notice)(handler))
// Check that the aliases were added.
assert.Contains(t, handler.Notices(), *notice)
assert.Len(t, handler.notices, 1, "there should be exactly 1 notice")
// Apply the WithNotices option with multiple notices.
notice2 := NewDeprecatedNotice(originalFunc, "oh no")
require.NoError(t, WithNotices(notice, notice2)(handler))
// Check that the aliases were added.
assert.Contains(t, handler.Notices(), *notice)
assert.Contains(t, handler.Notices(), *notice2)
assert.Len(t, handler.notices, 3, "there should be exactly 3 notices")
// Apply the WithNotices option with an empty message
notice3 := NewDebugNotice(originalFunc, "")
require.NoError(t, WithNotices(notice3)(handler))
assert.Contains(t, handler.Notices(), *notice)
assert.Contains(t, handler.Notices(), *notice2)
assert.Contains(t, handler.Notices(), *notice3)
assert.Len(t, handler.notices, 4, "there should be exactly 3 notices")
// Try to apply a notice with an empty function name.
notice4 := &FunctionNotice{}
require.NoError(t, WithNotices(notice4)(handler))
// Check that the aliases were not added.
assert.NotContains(t, handler.Notices(), *notice4)
assert.Len(t, handler.notices, 4, "there should still be exactly 3 notices")
}
func TestAssignNotices(t *testing.T) {
handler := New()
originalFunc := "originalFunc"
notice := NewInfoNotice(originalFunc, "amazing")
// Mock a function for originalFunc and add it to funcsRegistry.
mockFunc := func() string { return "cheese" }
handler.cachedFuncsMap[originalFunc] = mockFunc
// Assign the notices directly.
handler.notices = []FunctionNotice{*notice}
AssignNotices(handler)
// Check that the aliases were added.
assert.Contains(t, handler.Notices(), *notice)
assert.Len(t, handler.notices, 1, "there should be exactly 1 notice")
require.Contains(t, handler.RawFunctions(), originalFunc)
assert.NotEqual(t, reflect.ValueOf(mockFunc).Pointer(), reflect.ValueOf(handler.RawFunctions()[originalFunc]).Pointer(), "the function should have been wrapped")
}
func TestCreateWrappedFunction(t *testing.T) {
loggerHandler := ¬iceLoggerHandler{}
handler := New(WithLogger(slog.New(loggerHandler)))
originalFunc := "originalFunc"
mockFunc := func() string { return "cheese" }
// Create a wrapped function.
wrappedFunc := noticeWrapper(handler, *NewInfoNotice(originalFunc, "amazing"), originalFunc, mockFunc)
wrappedFunc2 := noticeWrapper(handler, *NewDeprecatedNotice(originalFunc, "oh no"), originalFunc, mockFunc)
wrappedFunc3 := noticeWrapper(handler, *NewNotice(NoticeKindDebug, []string{originalFunc}, "Nice this function returns $out"), originalFunc, mockFunc)
// Call the wrapped function.
out, err := wrappedFunc()
require.NoError(t, err)
assert.Equal(t, "cheese", out)
assert.Contains(t, loggerHandler.messages.String(), "[INFO] amazing")
out, err = wrappedFunc2()
require.NoError(t, err)
assert.Equal(t, "cheese", out)
assert.Contains(t, loggerHandler.messages.String(), "[WARN] Template function `originalFunc` is deprecated: oh no")
out, err = wrappedFunc3()
require.NoError(t, err)
assert.Equal(t, "cheese", out)
assert.Contains(t, loggerHandler.messages.String(), "[DEBUG] Nice this function returns cheese")
}
func TestNoticeInTemplate(t *testing.T) {
loggerHandler := ¬iceLoggerHandler{}
handler := New(WithLogger(slog.New(loggerHandler)))
originalFunc := "originalFunc"
mockFunc := func() string { return "cheese" }
handler.cachedFuncsMap[originalFunc] = mockFunc
// Add a notice to the handler.
AddNotice(&handler.notices, NewInfoNotice(originalFunc, "amazing"))
// Create a template with the function.
tmpl, err := template.New("test").Funcs(handler.Build()).Parse("{{- originalFunc -}}")
require.NoError(t, err)
// Execute the template.
var buf bytes.Buffer
err = tmpl.Execute(&buf, nil)
require.NoError(t, err)
assert.Equal(t, "cheese", buf.String())
assert.Equal(t, "[INFO] amazing\n", loggerHandler.messages.String())
}