github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/fmtstr/formatstring_test.go (about) 1 // Copyright 2023 iLogtail Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fmtstr 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestFormatString(t *testing.T) { 24 topic := "kafka_%{app_name}" 25 fields := map[string]string{ 26 "app_name": "ilogtail", 27 } 28 29 expectTopic := "kafka_ilogtail" 30 31 sf, _ := Compile(topic, func(key string, ops []VariableOp) (FormatEvaler, error) { 32 if v, found := fields[key]; found { 33 return StringElement{v}, nil 34 } 35 return StringElement{key}, nil 36 }) 37 actualTopic, _ := sf.Run(nil) 38 39 assert.Equal(t, expectTopic, actualTopic) 40 } 41 42 func TestCompileKeys(t *testing.T) { 43 topic := "kafka_%{app_name}" 44 expectTopicKeys := []string{"app_name"} 45 topicKeys, _ := CompileKeys(topic) 46 assert.Equal(t, expectTopicKeys, topicKeys) 47 }