github.com/viant/toolbox@v0.34.5/macro_test.go (about) 1 package toolbox_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "errors" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/viant/toolbox" 11 ) 12 13 func TestMacroExpansion(t *testing.T) { 14 valueRegistry := toolbox.NewValueProviderRegistry() 15 valueRegistry.Register("abc", TestValueProvider{"Called with %v %v!", nil}) 16 valueRegistry.Register("xyz", TestValueProvider{"XXXX", nil}) 17 valueRegistry.Register("klm", TestValueProvider{"Called with %v %v!", errors.New("Test error")}) 18 evaluator := toolbox.MacroEvaluator{ValueProviderRegistry: valueRegistry, Prefix: "<ds:", Postfix: ">"} 19 { 20 //simple macro test 21 22 actual, err := evaluator.Expand(nil, "<ds:abc[]>") 23 if err != nil { 24 t.Errorf("failed expand macro %v", err.Error()) 25 } 26 assert.Equal(t, "Called with %v %v!", actual) 27 } 28 29 { 30 //simple macro test 31 32 actual, err := evaluator.Expand(nil, "< <ds:abc[]>> <ds:xyz[]>") 33 if err != nil { 34 t.Errorf("failed expand macro %v", err.Error()) 35 } 36 assert.Equal(t, "< Called with %v %v!> XXXX", actual) 37 } 38 39 { 40 //simple macro test 41 actual, err := evaluator.Expand(nil, "<ds:abc>") 42 if err != nil { 43 t.Errorf("failed expand macro %v", err.Error()) 44 } 45 assert.Equal(t, "Called with %v %v!", actual) 46 } 47 48 { 49 //simple macro with arguments 50 51 actual, err := evaluator.Expand(nil, "<ds:abc [1, true]>") 52 if err != nil { 53 t.Errorf("failed expand macro %v", err.Error()) 54 } 55 assert.Equal(t, "Called with 1 true!", actual) 56 } 57 { 58 //simple macro with arguments 59 60 actual, err := evaluator.Expand(nil, "<ds:abc [1, true]> <ds:abc [2, false]>") 61 if err != nil { 62 t.Errorf("failed expand macro %v", err.Error()) 63 } 64 assert.Equal(t, "Called with 1 true! Called with 2 false!", actual) 65 } 66 67 { 68 //embeded macro with arguments 69 70 actual, err := evaluator.Expand(nil, "<ds:abc [1, \"<ds:abc [10,11]>\"]>") 71 if err != nil { 72 t.Errorf("failed expand macro %v", err.Error()) 73 } 74 assert.Equal(t, "Called with 1 Called with 10 11!!", actual) 75 } 76 77 { 78 //value provider with error 79 _, err := evaluator.Expand(nil, "<ds:abc [1, \"<ds:klm>\"]>") 80 assert.NotNil(t, err, "macro argument value provider returns error") 81 } 82 83 { 84 //value provider with error 85 _, err := evaluator.Expand(nil, "<ds:klm>") 86 assert.NotNil(t, err, "value provider returns error") 87 } 88 89 { 90 //simple macro with arguments 91 92 _, err := evaluator.Expand(nil, "<ds:agg>") 93 assert.NotNil(t, err) 94 95 } 96 97 { 98 //value provider with error 99 100 _, err := evaluator.Expand(nil, "<ds:pos [\"events\"]>") 101 assert.NotNil(t, err) 102 103 } 104 } 105 106 type TestValueProvider struct { 107 expandeWith string 108 err error 109 } 110 111 func (t TestValueProvider) Init() error { 112 return nil 113 } 114 115 func (t TestValueProvider) Get(context toolbox.Context, arguments ...interface{}) (interface{}, error) { 116 if len(arguments) > 0 { 117 return fmt.Sprintf(t.expandeWith, arguments...), t.err 118 } 119 return t.expandeWith, t.err 120 } 121 122 func (t TestValueProvider) Destroy() error { 123 return nil 124 } 125 126 func TestExpandParameters(t *testing.T) { 127 valueRegistry := toolbox.NewValueProviderRegistry() 128 valueRegistry.Register("abc", TestValueProvider{"Called with %v %v!", nil}) 129 valueRegistry.Register("klm", TestValueProvider{"Called with %v %v!", errors.New("Test error")}) 130 evaluator := toolbox.MacroEvaluator{ValueProviderRegistry: valueRegistry, Prefix: "<ds:", Postfix: ">"} 131 132 { 133 aMap := map[string]string{ 134 "k1": "!<ds:klm>!", 135 } 136 err := toolbox.ExpandParameters(&evaluator, aMap) 137 assert.NotNil(t, err) 138 } 139 { 140 aMap := map[string]string{ 141 "k1": "!<ds:abc>!", 142 } 143 err := toolbox.ExpandParameters(&evaluator, aMap) 144 assert.Nil(t, err) 145 assert.Equal(t, "!Called with %v %v!!", aMap["k1"]) 146 } 147 } 148 149 func TestExpandValue(t *testing.T) { 150 valueRegistry := toolbox.NewValueProviderRegistry() 151 valueRegistry.Register("abc", TestValueProvider{"Called with %v %v!", nil}) 152 valueRegistry.Register("klm", TestValueProvider{"Called with %v %v!", errors.New("Test error")}) 153 evaluator := toolbox.MacroEvaluator{ValueProviderRegistry: valueRegistry, Prefix: "<ds:", Postfix: ">"} 154 { 155 expanded, err := toolbox.ExpandValue(&evaluator, "!<ds:abc>!") 156 assert.Nil(t, err) 157 assert.Equal(t, "!Called with %v %v!!", expanded) 158 } 159 { 160 expanded, err := toolbox.ExpandValue(&evaluator, "!!") 161 assert.Nil(t, err) 162 assert.Equal(t, "!!", expanded) 163 164 } 165 { 166 _, err := toolbox.ExpandValue(&evaluator, "<ds:klm>") 167 assert.NotNil(t, err) 168 } 169 170 }