-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sprout_test.go
80 lines (60 loc) · 1.69 KB
/
sprout_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
package sprout
import (
"log/slog"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew_DefaultValues(t *testing.T) {
handler := New()
assert.NotNil(t, handler)
assert.NotNil(t, handler.Logger)
}
func TestNew_CustomValues(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
handler := New(
WithLogger(logger),
)
assert.NotNil(t, handler)
assert.Equal(t, logger, handler.Logger())
}
func TestWithLogger(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
option := WithLogger(logger)
handler := New()
require.NoError(t, option(handler)) // Apply the option
assert.Equal(t, logger, handler.Logger())
}
func TestWithParser(t *testing.T) {
fnHandler := &DefaultHandler{
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}
option := WithHandler(fnHandler)
handler := New()
require.NoError(t, option(handler)) // Apply the option
assert.Equal(t, fnHandler, handler)
}
func TestWithNilHandler(t *testing.T) {
fnHandler := &DefaultHandler{
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}
option := WithHandler(nil)
beforeApply := fnHandler
require.NoError(t, option(beforeApply)) // Apply the option
assert.Equal(t, beforeApply, fnHandler)
}
func TestWithSafeFuncs(t *testing.T) {
handler := New(WithSafeFuncs(true))
assert.True(t, handler.wantSafeFuncs)
handler.cachedFuncsMap["test"] = func() {}
funcCount := len(handler.RawFunctions())
handler.Build()
assert.Len(t, handler.cachedFuncsMap, funcCount*2)
var keys []string
for k := range handler.RawFunctions() {
keys = append(keys, k)
}
assert.Contains(t, keys, "test")
assert.Contains(t, keys, "safeTest")
}