mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
cdd9464f9a
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package funcs
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestReplace(t *testing.T) {
|
|
tests := []struct {
|
|
String cty.Value
|
|
Substr cty.Value
|
|
Replace cty.Value
|
|
Want cty.Value
|
|
Err bool
|
|
}{
|
|
{ // Regular search and replace
|
|
cty.StringVal("hello"),
|
|
cty.StringVal("hel"),
|
|
cty.StringVal("bel"),
|
|
cty.StringVal("bello"),
|
|
false,
|
|
},
|
|
{ // Search string doesn't match
|
|
cty.StringVal("hello"),
|
|
cty.StringVal("nope"),
|
|
cty.StringVal("bel"),
|
|
cty.StringVal("hello"),
|
|
false,
|
|
},
|
|
{ // Regular expression
|
|
cty.StringVal("hello"),
|
|
cty.StringVal("/l/"),
|
|
cty.StringVal("L"),
|
|
cty.StringVal("heLLo"),
|
|
false,
|
|
},
|
|
{
|
|
cty.StringVal("helo"),
|
|
cty.StringVal("/(l)/"),
|
|
cty.StringVal("$1$1"),
|
|
cty.StringVal("hello"),
|
|
false,
|
|
},
|
|
{ // Bad regexp
|
|
cty.StringVal("hello"),
|
|
cty.StringVal("/(l/"),
|
|
cty.StringVal("$1$1"),
|
|
cty.UnknownVal(cty.String),
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(fmt.Sprintf("replace(%#v, %#v, %#v)", test.String, test.Substr, test.Replace), func(t *testing.T) {
|
|
got, err := Replace(test.String, test.Substr, test.Replace)
|
|
|
|
if test.Err {
|
|
if err == nil {
|
|
t.Fatal("succeeded; want error")
|
|
}
|
|
return
|
|
} else if err != nil {
|
|
t.Fatalf("unexpected error: %s", err)
|
|
}
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
}
|
|
})
|
|
}
|
|
}
|