forked from viant/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro_test.go
170 lines (140 loc) · 4.22 KB
/
macro_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package toolbox_test
import (
"fmt"
"testing"
"errors"
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
)
func TestMacroExpansion(t *testing.T) {
valueRegistry := toolbox.NewValueProviderRegistry()
valueRegistry.Register("abc", TestValueProvider{"Called with %v %v!", nil})
valueRegistry.Register("xyz", TestValueProvider{"XXXX", nil})
valueRegistry.Register("klm", TestValueProvider{"Called with %v %v!", errors.New("Test error")})
evaluator := toolbox.MacroEvaluator{ValueProviderRegistry: valueRegistry, Prefix: "<ds:", Postfix: ">"}
{
//simple macro test
actual, err := evaluator.Expand(nil, "<ds:abc[]>")
if err != nil {
t.Errorf("failed expand macro %v", err.Error())
}
assert.Equal(t, "Called with %v %v!", actual)
}
{
//simple macro test
actual, err := evaluator.Expand(nil, "< <ds:abc[]>> <ds:xyz[]>")
if err != nil {
t.Errorf("failed expand macro %v", err.Error())
}
assert.Equal(t, "< Called with %v %v!> XXXX", actual)
}
{
//simple macro test
actual, err := evaluator.Expand(nil, "<ds:abc>")
if err != nil {
t.Errorf("failed expand macro %v", err.Error())
}
assert.Equal(t, "Called with %v %v!", actual)
}
{
//simple macro with arguments
actual, err := evaluator.Expand(nil, "<ds:abc [1, true]>")
if err != nil {
t.Errorf("failed expand macro %v", err.Error())
}
assert.Equal(t, "Called with 1 true!", actual)
}
{
//simple macro with arguments
actual, err := evaluator.Expand(nil, "<ds:abc [1, true]> <ds:abc [2, false]>")
if err != nil {
t.Errorf("failed expand macro %v", err.Error())
}
assert.Equal(t, "Called with 1 true! Called with 2 false!", actual)
}
{
//embeded macro with arguments
actual, err := evaluator.Expand(nil, "<ds:abc [1, \"<ds:abc [10,11]>\"]>")
if err != nil {
t.Errorf("failed expand macro %v", err.Error())
}
assert.Equal(t, "Called with 1 Called with 10 11!!", actual)
}
{
//value provider with error
_, err := evaluator.Expand(nil, "<ds:abc [1, \"<ds:klm>\"]>")
assert.NotNil(t, err, "macro argument value provider returns error")
}
{
//value provider with error
_, err := evaluator.Expand(nil, "<ds:klm>")
assert.NotNil(t, err, "value provider returns error")
}
{
//simple macro with arguments
_, err := evaluator.Expand(nil, "<ds:agg>")
assert.NotNil(t, err)
}
{
//value provider with error
_, err := evaluator.Expand(nil, "<ds:pos [\"events\"]>")
assert.NotNil(t, err)
}
}
type TestValueProvider struct {
expandeWith string
err error
}
func (t TestValueProvider) Init() error {
return nil
}
func (t TestValueProvider) Get(context toolbox.Context, arguments ...interface{}) (interface{}, error) {
if len(arguments) > 0 {
return fmt.Sprintf(t.expandeWith, arguments...), t.err
}
return t.expandeWith, t.err
}
func (t TestValueProvider) Destroy() error {
return nil
}
func TestExpandParameters(t *testing.T) {
valueRegistry := toolbox.NewValueProviderRegistry()
valueRegistry.Register("abc", TestValueProvider{"Called with %v %v!", nil})
valueRegistry.Register("klm", TestValueProvider{"Called with %v %v!", errors.New("Test error")})
evaluator := toolbox.MacroEvaluator{ValueProviderRegistry: valueRegistry, Prefix: "<ds:", Postfix: ">"}
{
aMap := map[string]string{
"k1": "!<ds:klm>!",
}
err := toolbox.ExpandParameters(&evaluator, aMap)
assert.NotNil(t, err)
}
{
aMap := map[string]string{
"k1": "!<ds:abc>!",
}
err := toolbox.ExpandParameters(&evaluator, aMap)
assert.Nil(t, err)
assert.Equal(t, "!Called with %v %v!!", aMap["k1"])
}
}
func TestExpandValue(t *testing.T) {
valueRegistry := toolbox.NewValueProviderRegistry()
valueRegistry.Register("abc", TestValueProvider{"Called with %v %v!", nil})
valueRegistry.Register("klm", TestValueProvider{"Called with %v %v!", errors.New("Test error")})
evaluator := toolbox.MacroEvaluator{ValueProviderRegistry: valueRegistry, Prefix: "<ds:", Postfix: ">"}
{
expanded, err := toolbox.ExpandValue(&evaluator, "!<ds:abc>!")
assert.Nil(t, err)
assert.Equal(t, "!Called with %v %v!!", expanded)
}
{
expanded, err := toolbox.ExpandValue(&evaluator, "!!")
assert.Nil(t, err)
assert.Equal(t, "!!", expanded)
}
{
_, err := toolbox.ExpandValue(&evaluator, "<ds:klm>")
assert.NotNil(t, err)
}
}