@@ -377,9 +377,33 @@ func TestSubstituteWithCustomFunc(t *testing.T) {
377377 assert .Check (t , is .ErrorContains (err , "required variable" ))
378378}
379379
380+ func TestSubstituteWithReplacementFunc (t * testing.T ) {
381+ options := []Option {
382+ WithReplacementFunction (func (s string , m Mapping , c * Config ) (string , error ) {
383+ if s == "${NOTHERE}" {
384+ return "" , fmt .Errorf ("bad choice dude: %q" , s )
385+ }
386+ r , err := DefaultReplacementFunc (s , m , c )
387+ if err == nil && r != "" {
388+ return r , nil
389+ }
390+ return "foobar" , nil
391+ }),
392+ }
393+ result , err := SubstituteWithOptions ("ok ${FOO}" , defaultMapping , options ... )
394+ assert .NilError (t , err )
395+ assert .Check (t , is .Equal ("ok first" , result ))
396+
397+ result , err = SubstituteWithOptions ("ok ${BAR}" , defaultMapping , options ... )
398+ assert .NilError (t , err )
399+ assert .Check (t , is .Equal ("ok foobar" , result ))
400+
401+ _ , err = SubstituteWithOptions ("ok ${NOTHERE}" , defaultMapping , options ... )
402+ assert .Check (t , is .ErrorContains (err , "bad choice dude" ))
403+ }
404+
380405// TestPrecedence tests is the precedence on '-' and '?' is of the first match
381406func TestPrecedence (t * testing.T ) {
382-
383407 testCases := []struct {
384408 template string
385409 expected string
0 commit comments