Skip to content

Commit

Permalink
Updated testInterpolationFunc params.
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-stitt committed Dec 1, 2017
1 parent 6a7286e commit 7502528
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
18 changes: 10 additions & 8 deletions interpolation/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (

// CoreFunctions are the custom funtions for interpolation
var CoreFunctions = map[string]ast.Function{
"lower": interpolationFuncLower(),
"upper": interpolationFuncUpper(),
"env": interpolationFuncEnv(),
"join": interpolationFuncJoin(),
"has": interpolationFuncHas(),
"map": interpolationFuncMap(),
"list": interpolationFuncList(),
"concat": interpolationFuncConcat(),
"lower": interpolationFuncLower(),
"upper": interpolationFuncUpper(),
"env": interpolationFuncEnv(),
"join": interpolationFuncJoin(),
"has": interpolationFuncHas(),
"map": interpolationFuncMap(),
"keys": interpolationFuncKeys(),
"list": interpolationFuncList(),
"concat": interpolationFuncConcat(),
"replace": interpolationFuncReplace(),
}

// interpolationFuncEnv will extract a variable out of the env
Expand Down
14 changes: 9 additions & 5 deletions interpolation/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ type functionTestCase struct {
vars map[string]ast.Variable
}

func testInterpolationFunc(key string, interpolationFunc func() ast.Function) func(t *testing.T, testCase functionTestCase) {
type keyFuncs map[string]func() ast.Function

func testInterpolationFunc(funcMap keyFuncs) func(t *testing.T, testCase functionTestCase) {
configFuncs := make(map[string]ast.Function)
for key, interpolationFunc := range funcMap {
configFuncs[key] = interpolationFunc()
}
config := &hil.EvalConfig{
GlobalScope: &ast.BasicScope{
FuncMap: map[string]ast.Function{
key: interpolationFunc(),
},
FuncMap: configFuncs,
},
}

Expand Down Expand Up @@ -120,7 +124,7 @@ func TestInterpolationFuncEnv(t *testing.T) {
},
}

envTestFunc := testInterpolationFunc("env", interpolationFuncEnv)
envTestFunc := testInterpolationFunc(keyFuncs{"env": interpolationFuncEnv})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions interpolation/lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestInterpolationFuncList(t *testing.T) {
},
{
description: "Multi-type list",
text: `${list("foo", "${list("bar")}")}`,
expectation: []interface{}{"foo", []interface{}{"bar"}},
text: `${list("foo", "${list("bar")}", "${map("flip", "flop")}")}`,
expectation: []interface{}{"foo", []interface{}{"bar"}, map[string]interface{}{"flip": "flop"}},
},
{
description: "Empty list",
Expand All @@ -25,7 +25,7 @@ func TestInterpolationFuncList(t *testing.T) {
},
}

listTestFunc := testInterpolationFunc("list", interpolationFuncList)
listTestFunc := testInterpolationFunc(keyFuncs{"list": interpolationFuncList, "map": interpolationFuncMap})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestInterpoloationFuncConcat(t *testing.T) {
},
}

concatTestFunc := testInterpolationFunc("concat", interpolationFuncConcat)
concatTestFunc := testInterpolationFunc(keyFuncs{"concat": interpolationFuncConcat})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions interpolation/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestInterpolationFuncHas(t *testing.T) {
},
}

hasTestFunc := testInterpolationFunc("has", interpolationFuncHas)
hasTestFunc := testInterpolationFunc(keyFuncs{"has": interpolationFuncHas})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestInterpolationFuncMap(t *testing.T) {
},
}

mapTestFunc := testInterpolationFunc("map", interpolationFuncMap)
mapTestFunc := testInterpolationFunc(keyFuncs{"map": interpolationFuncMap})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestInterpolationFuncKeys(t *testing.T) {
},
}

keysTestFunc := testInterpolationFunc("keys", interpolationFuncKeys)
keysTestFunc := testInterpolationFunc(keyFuncs{"keys": interpolationFuncKeys})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions interpolation/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,21 @@ func interpolationFuncJoin() ast.Function {
},
}
}

// interpolationFuncReplace replaces the occurences of a value on the provided string with another value.
// The number of occurences to replace is the last argument to the function.
func interpolationFuncReplace() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString, ast.TypeString, ast.TypeString, ast.TypeInt},
ReturnType: ast.TypeString,
Callback: func(inputs []interface{}) (interface{}, error) {
input := inputs[0].(string)
search := inputs[1].(string)
replace := inputs[2].(string)
count := inputs[3].(int)

result := strings.Replace(input, search, replace, count)
return result, nil
},
}
}
34 changes: 31 additions & 3 deletions interpolation/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestInterpolationFuncLower(t *testing.T) {
},
}

lowerTestFunc := testInterpolationFunc("lower", interpolationFuncLower)
lowerTestFunc := testInterpolationFunc(keyFuncs{"lower": interpolationFuncLower})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand All @@ -43,7 +43,7 @@ func TestInterpolationFuncUpper(t *testing.T) {
},
}

lowerTestFunc := testInterpolationFunc("upper", interpolationFuncUpper)
lowerTestFunc := testInterpolationFunc(keyFuncs{"upper": interpolationFuncUpper})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down Expand Up @@ -105,11 +105,39 @@ func TestInterpolationFuncJoin(t *testing.T) {
},
}

joinTestFunc := testInterpolationFunc("join", interpolationFuncJoin)
joinTestFunc := testInterpolationFunc(keyFuncs{"join": interpolationFuncJoin})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
joinTestFunc(t, tc)
})
}
}

func TestInterpolationFuncReplace(t *testing.T) {
testCases := []functionTestCase{
{
description: "Replace N occurences",
text: `${replace("foo bar bar", " bar", "", -1)}`,
expectation: "foo",
},
{
description: "Replace 0 occurences",
text: `${replace("foo bar bar", " bar", "", 0)}`,
expectation: "foo bar bar",
},
{
description: "Replace 1 occurences",
text: `${replace("foo bar bar", " bar", "", 1)}`,
expectation: "foo bar",
},
}

replaceTestFunc := testInterpolationFunc(keyFuncs{"replace": interpolationFuncReplace})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
replaceTestFunc(t, tc)
})
}
}

0 comments on commit 7502528

Please sign in to comment.