-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexpr_string_test.go
More file actions
102 lines (94 loc) · 1.51 KB
/
expr_string_test.go
File metadata and controls
102 lines (94 loc) · 1.51 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
package helper
import (
"os"
"strconv"
"testing"
"github.com/observiq/stanza/entry"
"github.com/stretchr/testify/require"
)
func TestExprString(t *testing.T) {
os.Setenv("TEST_EXPR_STRING_ENV", "foo")
defer os.Unsetenv("TEST_EXPR_STRING_ENV")
exampleEntry := func() *entry.Entry {
e := entry.New()
e.Record = map[string]interface{}{
"test": "value",
}
e.Resource = map[string]string{
"id": "value",
}
return e
}
cases := []struct {
config ExprStringConfig
expected string
}{
{
"test",
"test",
},
{
"EXPR( 'test' )",
"test",
},
{
"prefix-EXPR( 'test' )",
"prefix-test",
},
{
"prefix-EXPR('test')-suffix",
"prefix-test-suffix",
},
{
"prefix-EXPR('test')-suffix-EXPR('test2' + 'test3')",
"prefix-test-suffix-test2test3",
},
{
"EXPR('test' )EXPR('asdf')",
"testasdf",
},
{
"EXPR",
"EXPR",
},
{
"EXPR(",
"EXPR(",
},
{
")EXPR(",
")EXPR(",
},
{
"my EXPR( $.test )",
"my value",
},
{
"my EXPR($.test )",
"my value",
},
{
"my EXPR($.test)",
"my value",
},
{
"my EXPR(env('TEST_EXPR_STRING_ENV'))",
"my foo",
},
{
"EXPR( $resource.id )",
"value",
},
}
for i, tc := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
exprString, err := tc.config.Build()
require.NoError(t, err)
env := GetExprEnv(exampleEntry())
defer PutExprEnv(env)
result, err := exprString.Render(env)
require.NoError(t, err)
require.Equal(t, tc.expected, result)
})
}
}