diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-01-29 23:31:16 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-02-03 17:22:43 +0100 |
commit | 7edad5cefa0f065aa83dffd2d7830aeaf9f38662 (patch) | |
tree | 86b727f434302ffb28cb59278243517f9765e170 /tests/custom/03_stdlib/32_match | |
parent | d5003fde57eab19588da7bfdbaefe93d47435eb6 (diff) |
tests: add functional tests for builtin functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/32_match')
-rw-r--r-- | tests/custom/03_stdlib/32_match | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/32_match b/tests/custom/03_stdlib/32_match new file mode 100644 index 0000000..b053044 --- /dev/null +++ b/tests/custom/03_stdlib/32_match @@ -0,0 +1,55 @@ +The `match()` function applies the given regular expression pattern on +the given subject value. + +Depending on whether the given regular expression sets the global (`g`) +modifier, either an array of match groups or the first match group is +returned. + +Returns `null` if the given pattern argument is not a regular expression +value, or if the subject is `null` or unspecified. + +-- Testcase -- +{% + print(join("\n", [ + // match all key=value pairs + match("kind=fruit name=strawberry color=red", /([[:alpha:]]+)=([^= ]+)/g), + + // match any word + match("The quick brown fox jumps over the lazy dog", /[[:alpha:]]+/g), + + // match the first three lowercase words + match("The quick brown fox jumps over the lazy dog", / ([[:lower:]]+) ([[:lower:]]+) ([[:lower:]]+)/), + + // special case: match any empty string sequence + match("foo", /()/g), + + // special case: match first empty string sequence + match("foo", /()/), + + // subject is implictly converted to string + match(true, /u/) + ]), "\n"); +%} +-- End -- + +-- Expect stdout -- +[ [ "kind=fruit", "kind", "fruit" ], [ "name=strawberry", "name", "strawberry" ], [ "color=red", "color", "red" ] ] +[ [ "The" ], [ "quick" ], [ "brown" ], [ "fox" ], [ "jumps" ], [ "over" ], [ "the" ], [ "lazy" ], [ "dog" ] ] +[ " quick brown fox", "quick", "brown", "fox" ] +[ [ "", "" ], [ "", "" ], [ "", "" ], [ "", "" ] ] +[ "", "" ] +[ "u" ] +-- End -- + + +Omitting the subject yields `null`. + +-- Testcase -- +{% + printf("%.J\n", match(null, /u/)); +%} +-- End -- + +-- Expect stdout -- +null +-- End -- |