Files
EdgeCommon/pkg/configutils/variable_test.go

98 lines
1.9 KiB
Go
Raw Normal View History

2020-09-26 08:07:24 +08:00
package configutils
import (
2021-06-27 22:00:02 +08:00
"github.com/iwind/TeaGo/types"
2021-12-13 15:31:03 +08:00
"runtime"
2020-09-26 08:07:24 +08:00
"strconv"
"testing"
)
func TestParseVariables(t *testing.T) {
2021-12-13 15:31:03 +08:00
{
v := ParseVariables("hello, ${name}, world", func(s string) string {
return "Lu"
})
t.Log(v)
}
{
v := ParseVariables("hello, world", func(s string) string {
return "Lu"
})
t.Log(v)
}
{
v := ParseVariables("${name}", func(s string) string {
return "Lu"
})
t.Log(v)
}
2020-09-26 08:07:24 +08:00
}
func TestParseNoVariables(t *testing.T) {
for i := 0; i < 2; i++ {
v := ParseVariables("hello, world", func(s string) string {
return "Lu"
})
t.Log(v)
}
}
2021-06-27 22:00:02 +08:00
func TestParseHolders(t *testing.T) {
var holders = ParseHolders("hello, ${name}, world")
for _, h := range holders {
t.Log(types.String(h))
}
t.Log("parse result:", ParseVariablesFromHolders(holders, func(s string) string {
return "[" + s + "]"
}))
}
2020-09-26 08:07:24 +08:00
func BenchmarkParseVariables(b *testing.B) {
_ = ParseVariables("hello, ${name}, ${age}, ${gender}, ${home}, world", func(s string) string {
return "Lu"
})
for i := 0; i < b.N; i++ {
_ = ParseVariables("hello, ${name}, ${age}, ${gender}, ${home}, world", func(s string) string {
return "Lu"
})
}
}
2021-06-27 22:00:02 +08:00
func BenchmarkParseVariablesFromHolders(b *testing.B) {
var holders = ParseHolders("hello, ${name}, ${age}, ${gender}, ${home}, world")
for i := 0; i < b.N; i++ {
_ = ParseVariablesFromHolders(holders, func(s string) string {
return "Lu"
})
}
}
2020-09-26 08:07:24 +08:00
func BenchmarkParseVariablesUnique(b *testing.B) {
for i := 0; i < b.N; i++ {
2021-12-13 15:31:03 +08:00
_ = ParseVariables("hello, ${name} "+strconv.Itoa(i), func(s string) string {
return "Lu"
})
}
}
func BenchmarkParseVariablesUnique_Single(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
_ = ParseVariables("${name}", func(s string) string {
2020-09-26 08:07:24 +08:00
return "Lu"
})
}
}
func BenchmarkParseNoVariables(b *testing.B) {
for i := 0; i < b.N; i++ {
2021-12-13 15:31:03 +08:00
_ = ParseVariables("hello, world", func(s string) string {
2020-09-26 08:07:24 +08:00
return "Lu"
})
}
}