Skip to content

Commit

Permalink
feat(tests): add match test
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaLohinski committed Feb 27, 2024
1 parent 2fd51de commit eaa870d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
13 changes: 12 additions & 1 deletion builtins/tests.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
## The `empty` test
Check if the input is empty. Works on strings, lists and dictionaries.

Check if the input is empty. Works on strings, lists and dictionaries.

## The `match` test

Expects a string holding a regular expression to be passed as an argument to match against the input. Returns `true` if the input matches the expression and `false` otherwise. For example:

```
{{ "123" is match("^[0-9]+") }}
```

will evaluate to `True`.
11 changes: 11 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1467,8 +1467,19 @@ Classic type casting tests.


### The `empty` test

Check if the input is empty. Works on strings, lists and dictionaries.

### The `match` test

Expects a string holding a regular expression to be passed as an argument to match against the input. Returns `true` if the input matches the expression and `false` otherwise. For example:

```
{{ "123" is match("^[0-9]+") }}
```

will evaluate to `True`.



## Methods
Expand Down
36 changes: 36 additions & 0 deletions internal/provider/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,40 @@ var _ = Context("tests", func() {
itShouldFailToRender(terraformCode, "invalid call to test 'empty': True is neither a list, a dict nor a string")
})
})
Context("match", func() {
BeforeEach(func() {
*template = `{{- input is match("^f(o)+$") -}}`
})
Context("when the input is a string that matches", func() {
BeforeEach(func() {
*context = `input = "foo"`
})
itShouldSetTheExpectedResult(terraformCode, "True")
})
Context("when the input is a string that does not matches", func() {
BeforeEach(func() {
*context = `input = "bar"`
})
itShouldSetTheExpectedResult(terraformCode, "False")
})
Context("when the argument is not a valid regex", func() {
BeforeEach(func() {
*context = `input = "foo"`
*template = `{{- input is match("{.*[") -}}`
})
itShouldFailToRender(terraformCode, "failed to compile: {.*\\[: error parsing regexp")
})
Context("when the input is an error", func() {
BeforeEach(func() {
*template = `{{- ("thrown" | fail) is match(".*") -}}`
})
itShouldFailToRender(terraformCode, "thrown")
})
Context("when the input is invalid", func() {
BeforeEach(func() {
*context = `input = true`
})
itShouldFailToRender(terraformCode, "True is not a string")
})
})
})
24 changes: 24 additions & 0 deletions lib/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

var Tests = exec.NewTestSet(map[string]exec.TestFunction{
"empty": testEmpty,
"match": testMatch,
})

func testEmpty(ctx *exec.Context, in *exec.Value, params *exec.VarArgs) (bool, error) {
Expand All @@ -22,3 +23,26 @@ func testEmpty(ctx *exec.Context, in *exec.Value, params *exec.VarArgs) (bool, e
return in.Len() == 0, nil
}
}

func testMatch(ctx *exec.Context, in *exec.Value, params *exec.VarArgs) (bool, error) {
if in.IsError() {
return false, errors.New(in.Error())
}
var (
regex string
)
if err := params.Take(
exec.KeywordArgument("regex", exec.AsValue(2), exec.StringArgument(&regex)),
); err != nil {
return false, exec.AsValue(exec.ErrInvalidCall(err))
}
if !in.IsString() {
return false, exec.AsValue(exec.ErrInvalidCall(fmt.Errorf("%s is not a string", in.String())))
}
matcher, err := regexp.Compile(regex)
if err != nil {
return false, exec.AsValue(exec.ErrInvalidCall(fmt.Errorf("failed to compile: %s: %s", regex, err)))
}

return matcher.MatchString(in.String()), nil
}

0 comments on commit eaa870d

Please sign in to comment.