Skip to content

Commit

Permalink
Fix tag regexes; s/script/statement/ tag|line
Browse files Browse the repository at this point in the history
* Fixes tag regexes to allow multiple tags per line
* Changes "script" tag/line references to use "statement"
* Adds tests for parse_env_delims / delim env vars
* Adds missing EMPTY tests for parse_*_delim funcs
  • Loading branch information
TekWizely committed Jul 9, 2021
1 parent 71bbbf5 commit 2ddfd67
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 28 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ $ NAME=TekWizely source <( bash-tpl test.tpl )
Hello ' <% TekWizely %> '
```

#### Script Tag
#### Statement Tag

Script tags allow you to inline more-complicated script statements, ie:
Statement tags allow you to inline more-complicated script statements, ie:

```
Hello <%% echo $NAME %>
Expand All @@ -185,17 +185,18 @@ printf "%s\n" Hello\ "$(echo $NAME)" # Trivial example to demonstrate the conve

**Delimiters:**

The delimiters for script tags are `<%%` &amp; `%>`
The delimiters for statement tags are `<%%` &amp; `%>`

More specifically, the delimiters are : Standard delimiters with open delimiter followed by a [Script Line Delimiter](#script-lines).
More specifically, the delimiters are : Standard delimiters with the open delimiter followed by the statement tag delimiter.

NOTE: No whitespace is allowed between the standard tag and the script line delimiter (ie. `<% %` would **not** be treated as a script tag).

NOTE: The script line delimiter is *not* part of the close tag (`%>`), **just** the open tag (`<%%`)
**NOTES:**
* The statement tag delimiter (ie the 2nd `%`) can be configured separate from the standard tag delimiters. See [Customizing Delimiters](#customizing-delimiters) for details.
* No whitespace is allowed between the standard tag and the statement tag delimiter (ie. `<% %` would **not** be treated as a statement tag).
* The statement tag delimiter is *not* part of the close tag (`%>`), **just** the open tag (`<%%`)

##### Example

A *slightly* more useful example of a script tag might be:
A *slightly* more useful example of a statement tag might be:

_test.tpl_
```
Expand All @@ -213,7 +214,7 @@ Hello TEKWIZELY
**NOTE:** As with standard tags, the value within the statement tag is 'trimmed' before being processed, ie: `'<%% echo $NAME %>'` is equivalent to `'<%%echo $NAME%>'`.

----------------
### Script Lines
### Statement Lines

_test.tpl_
```
Expand All @@ -236,7 +237,7 @@ Hello TekWizely
```

-----------------
### Script Blocks
### Statement Blocks

```
%
Expand Down
4 changes: 2 additions & 2 deletions bash-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ function reset_template_regexes() {

TAG_TEXT_REGEX="^([^${d1}]+|${d1}$|${d1}[^${d1}${d2}]+)(.*)"

TAG_STD_REGEX="^${d1}${d2}((([^${d3}])|(${d3}[^${d4}]?))*)${d3}${d4}(.*)"
TAG_STD_REGEX="^${d1}${d2}((([^${d3}])|(${d3}[^${d4}]))*)${d3}${d4}(.*)"

TAG_QUOTE_REGEX="^${d1}${d2}\"((([^\"])|(\"[^${d3}]?)|(\"${d3}[^${d4}]?))*)\"${d3}${d4}(.*)"
TAG_QUOTE_REGEX="^${d1}${d2}\"((([^\"])|(\"[^${d3}])|(\"${d3}[^${d4}]))*)\"${d3}${d4}(.*)"

TAG_STATEMENT_REGEX="^${d1}${d2}${ds}((([^${d3}])|(${d3}[^${d4}]))*)${d3}${d4}(.*)"

Expand Down
313 changes: 313 additions & 0 deletions test/delim-env.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@

setup() {
source "${BATS_TEST_DIRNAME}/../bash-tpl"
}

@test "BASH_TPL_TAG_DELIMS: Should do nothing when unset or empty" {

[[ -z "${TAG_START_DELIM1}" ]]
[[ -z "${TAG_START_DELIM2}" ]]
[[ -z "${TAG_STOP_DELIM1}" ]]
[[ -z "${TAG_STOP_DELIM2}" ]]

unset BASH_TPL_TAG_DELIMS
parse_env_delims

[[ -z "${TAG_START_DELIM1}" ]]
[[ -z "${TAG_START_DELIM2}" ]]
[[ -z "${TAG_STOP_DELIM1}" ]]
[[ -z "${TAG_STOP_DELIM2}" ]]

BASH_TPL_TAG_DELIMS=''
parse_env_delims

[[ -z "${TAG_START_DELIM1}" ]]
[[ -z "${TAG_START_DELIM2}" ]]
[[ -z "${TAG_STOP_DELIM1}" ]]
[[ -z "${TAG_STOP_DELIM2}" ]]
}

@test "BASH_TPL_TAG_DELIMS: Should error on invalid input" {
# No middle-space
#
BASH_TPL_TAG_DELIMS='{{}}'
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing tag delimiter values for BASH_TPL_TAG_DELIMS: '{{}}'" ]]

# All Spaces
#
BASH_TPL_TAG_DELIMS=' '
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing tag delimiter values for BASH_TPL_TAG_DELIMS: ' '" ]]
}

@test "BASH_TPL_TAG_DELIMS: Should process valid input" {

[[ -z "${TAG_START_DELIM1}" ]]
[[ -z "${TAG_START_DELIM2}" ]]
[[ -z "${TAG_STOP_DELIM1}" ]]
[[ -z "${TAG_STOP_DELIM2}" ]]

BASH_TPL_TAG_DELIMS='ab cd'
parse_env_delims

[[ "${TAG_START_DELIM1}" == 'a' ]]
[[ "${TAG_START_DELIM2}" == 'b' ]]
[[ "${TAG_STOP_DELIM1}" == 'c' ]]
[[ "${TAG_STOP_DELIM2}" == 'd' ]]

BASH_TPL_TAG_DELIMS='{{ }}'
parse_env_delims

[[ "${TAG_START_DELIM1}" == '{' ]]
[[ "${TAG_START_DELIM2}" == '{' ]]
[[ "${TAG_STOP_DELIM1}" == '}' ]]
[[ "${TAG_STOP_DELIM2}" == '}' ]]
}

@test "BASH_TPL_TAG_STMT_DELIM: Should do nothing when unset or empty" {

[[ -z "${TAG_STMT_DELIM}" ]]

unset BASH_TPL_TAG_STMT_DELIM
parse_env_delims

[[ -z "${TAG_STMT_DELIM}" ]]

BASH_TPL_TAG_STMT_DELIM=''
parse_env_delims

[[ -z "${TAG_STMT_DELIM}" ]]
}

@test "BASH_TPL_TAG_STMT_DELIM: Should error on invalid input" {
# Too long
#
BASH_TPL_TAG_STMT_DELIM='%%'
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing tag stmt delimiter value for BASH_TPL_TAG_STMT_DELIM: '%%'" ]]

# Space
#
BASH_TPL_TAG_STMT_DELIM=' '
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing tag stmt delimiter value for BASH_TPL_TAG_STMT_DELIM: ' '" ]]
}

@test "BASH_TPL_TAG_STMT_DELIM: Should process valid input" {

[[ -z "${TAG_STMT_DELIM}" ]]

BASH_TPL_TAG_STMT_DELIM='%'
parse_env_delims

[[ "${TAG_STMT_DELIM}" == '%' ]]

BASH_TPL_TAG_STMT_DELIM='$'
parse_env_delims

[[ "${TAG_STMT_DELIM}" == '$' ]]
}

@test "BASH_TPL_STMT_DELIM: Should do nothing when unset or empty" {

[[ -z "${STMT_DELIM}" ]]

unset BASH_TPL_STMT_DELIM
parse_env_delims

[[ -z "${STMT_DELIM}" ]]

BASH_TPL_STMT_DELIM=''
parse_env_delims

[[ -z "${STMT_DELIM}" ]]
}

@test "BASH_TPL_STMT_DELIM: Should error on invalid input" {
# Space
#
BASH_TPL_STMT_DELIM=' '
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing stmt delimiter value for BASH_TPL_STMT_DELIM: ' '" ]]
}

@test "BASH_TPL_STMT_DELIM: Should process valid input" {

[[ -z "${STMT_DELIM}" ]]

BASH_TPL_STMT_DELIM='%'
parse_env_delims

[[ "${STMT_DELIM}" == '%' ]]

BASH_TPL_STMT_DELIM='-->'
parse_env_delims

[[ "${STMT_DELIM}" == '-->' ]]
}

@test "BASH_TPL_STMT_BLOCK_DELIMS: Should do nothing when unset or empty" {

[[ -z "${STMT_BLOCK_START_DELIM}" ]]
[[ -z "${STMT_BLOCK_STOP_DELIM}" ]]
[[ -z "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

STMT_BLOCK_DELIM_UNDEFINED=1
[[ -n "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

unset BASH_TPL_STMT_BLOCK_DELIMS
parse_env_delims

[[ -z "${STMT_BLOCK_START_DELIM}" ]]
[[ -z "${STMT_BLOCK_STOP_DELIM}" ]]
[[ -n "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

BASH_TPL_STMT_BLOCK_DELIMS=''
parse_env_delims

[[ -z "${STMT_BLOCK_START_DELIM}" ]]
[[ -z "${STMT_BLOCK_STOP_DELIM}" ]]
[[ -n "${STMT_BLOCK_DELIM_UNDEFINED}" ]]
}

@test "BASH_TPL_STMT_BLOCK_DELIMS: Should error on invalid input" {
# All Spaces
#
BASH_TPL_STMT_BLOCK_DELIMS=' '
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing stmt-block delimiter values for BASH_TPL_STMT_BLOCK_DELIMS: ' '" ]]

# No middle-space
#
BASH_TPL_STMT_BLOCK_DELIMS='<%%>'
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing stmt-block delimiter values for BASH_TPL_STMT_BLOCK_DELIMS: '<%%>'" ]]
}

@test "BASH_TPL_STMT_BLOCK_DELIMS: Should process valid input" {

[[ -z "${STMT_BLOCK_START_DELIM}" ]]
[[ -z "${STMT_BLOCK_STOP_DELIM}" ]]
[[ -z "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

STMT_BLOCK_DELIM_UNDEFINED=1
[[ -n "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

BASH_TPL_STMT_BLOCK_DELIMS='ab cd'
parse_env_delims

[[ "${STMT_BLOCK_START_DELIM}" == 'ab' ]]
[[ "${STMT_BLOCK_STOP_DELIM}" == 'cd' ]]
[[ -z "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

STMT_BLOCK_DELIM_UNDEFINED=1
[[ -n "${STMT_BLOCK_DELIM_UNDEFINED}" ]]

BASH_TPL_STMT_BLOCK_DELIMS='<% %>'
parse_env_delims

[[ "${STMT_BLOCK_START_DELIM}" == '<%' ]]
[[ "${STMT_BLOCK_STOP_DELIM}" == '%>' ]]
[[ -z "${STMT_BLOCK_DELIM_UNDEFINED}" ]]
}

@test "BASH_TPL_DIR_DELIM: Should do nothing when unset or empty" {

[[ -z "${DIRECTIVE_DELIM}" ]]

unset BASH_TPL_DIR_DELIM
parse_env_delims

[[ -z "${DIRECTIVE_DELIM}" ]]

BASH_TPL_DIR_DELIM=''
parse_env_delims

[[ -z "${DIRECTIVE_DELIM}" ]]
}

@test "BASH_TPL_DIR_DELIM: Should error on invalid input" {
# Space
#
BASH_TPL_DIR_DELIM=' '
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing directive delimiter value for BASH_TPL_DIR_DELIM: ' '" ]]
}

@test "BASH_TPL_DIR_DELIM: Should process valid input" {

[[ -z "${DIRECTIVE_DELIM}" ]]

BASH_TPL_DIR_DELIM='.'
parse_env_delims

[[ "${DIRECTIVE_DELIM}" == '.' ]]

BASH_TPL_DIR_DELIM='-->'
parse_env_delims

[[ "${DIRECTIVE_DELIM}" == '-->' ]]
}

@test "BASH_TPL_CMT_DELIM: Should do nothing when unset or empty" {

[[ -z "${COMMENT_DELIM}" ]]
[[ -z "${COMMENT_DELIM_UNDEFINED}" ]]

COMMENT_DELIM_UNDEFINED=1
[[ -n "${COMMENT_DELIM_UNDEFINED}" ]]

unset BASH_TPL_CMT_DELIM
parse_env_delims

[[ -z "${COMMENT_DELIM}" ]]
[[ -n "${COMMENT_DELIM_UNDEFINED}" ]]

BASH_TPL_CMT_DELIM=''
parse_env_delims

[[ -z "${COMMENT_DELIM}" ]]
[[ -n "${COMMENT_DELIM_UNDEFINED}" ]]
}

@test "BASH_TPL_CMT_DELIM: Should error on invalid input" {
# Space
#
BASH_TPL_CMT_DELIM=' '
run parse_env_delims
[[ $status -eq 1 ]]
[[ "$output" == "Error: Invalid or missing comment delimiter value for BASH_TPL_CMT_DELIM: ' '" ]]
}

@test "BASH_TPL_CMT_DELIM: Should process valid input" {

[[ -z "${COMMENT_DELIM}" ]]
[[ -z "${COMMENT_DELIM_UNDEFINED}" ]]

COMMENT_DELIM_UNDEFINED=1
[[ -n "${COMMENT_DELIM_UNDEFINED}" ]]

BASH_TPL_CMT_DELIM='#'
parse_env_delims

[[ "${COMMENT_DELIM}" == '#' ]]
[[ -z "${COMMENT_DELIM_UNDEFINED}" ]]

COMMENT_DELIM_UNDEFINED=1
[[ -n "${COMMENT_DELIM_UNDEFINED}" ]]

BASH_TPL_CMT_DELIM='//'
parse_env_delims

[[ "${COMMENT_DELIM}" == '//' ]]
[[ -z "${COMMENT_DELIM_UNDEFINED}" ]]
}

0 comments on commit 2ddfd67

Please sign in to comment.