github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/vars/vars_test.go (about)

     1  package vars
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestVars(t *testing.T) {
    10  	m := map[string]func(params string) GenFn{
    11  		"name": func(params string) GenFn { return func() interface{} { return "bingoo" } },
    12  	}
    13  	mv := NewMapGenValue(m)
    14  	s := EvalSubstitute("hello {name}", mv)
    15  	assert.Equal(t, "hello bingoo", s)
    16  	assert.Equal(t, map[string]interface{}{"name": "bingoo"}, mv.Vars)
    17  
    18  	s = EvalSubstitute("hello {{name}}", mv)
    19  	assert.Equal(t, "hello bingoo", s)
    20  	assert.Equal(t, map[string]interface{}{"name": "bingoo"}, mv.Vars)
    21  
    22  	s = EvalSubstitute("hello ${name}", mv)
    23  	assert.Equal(t, "hello bingoo", s)
    24  	assert.Equal(t, map[string]interface{}{"name": "bingoo"}, mv.Vars)
    25  
    26  	mv = NewMapGenValue(map[string]func(params string) GenFn{})
    27  	s = EvalSubstitute("hello ${name}", mv)
    28  	assert.Equal(t, "hello name", s)
    29  	assert.Equal(t, map[string]bool{"name": true}, mv.MissedVars)
    30  }