Attempting to address shortcomings and whitespace issues

This commit is contained in:
Tyler Akins
2023-04-07 19:35:25 -05:00
parent 8e9fd680d4
commit febd3467c8
123 changed files with 2008 additions and 1137 deletions

9
tests/ampersand Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
thing="Works"
template="{{&thing}}"
expected="Works"
runTest

21
tests/array Executable file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
repo=( "resque" "hub" "rip" )
template() {
cat <<EOF
{{#repo}}
<b>{{@key}} - {{.}}</b>
{{/repo}}
EOF
}
expected() {
cat <<EOF
<b>0 - resque</b>
<b>1 - hub</b>
<b>2 - rip</b>
EOF
}
runTest

View File

@@ -1 +0,0 @@
repo=( "resque" "hub" "rip" )

View File

@@ -1,3 +0,0 @@
<b>0 - resque</b>
<b>1 - hub</b>
<b>2 - rip</b>

View File

@@ -1,3 +0,0 @@
{{#repo}}
<b>{{@key}} - {{.}}</b>
{{/repo}}

24
tests/assoc-array Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
declare -A repo
repo[resque]="Resque"
repo[hub]="Hub"
repo[rip]="Rip"
template() {
cat <<EOF
{{#repo}}
<b>{{@key}} - {{.}}</b>
{{/repo}}
EOF
}
expected() {
cat <<EOF
<b>hub - Hub</b>
<b>rip - Rip</b>
<b>resque - Resque</b>
EOF
}
runTest

View File

@@ -1,4 +0,0 @@
declare -A repo
repo[resque]="Resque"
repo[hub]="Hub"
repo[rip]="Rip"

View File

@@ -1,3 +0,0 @@
<b>hub - Hub</b>
<b>rip - Rip</b>
<b>resque - Resque</b>

View File

@@ -1,3 +0,0 @@
{{#repo}}
<b>{{@key}} - {{.}}</b>
{{/repo}}

8
tests/comment Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
template="Wor{{!comment}}ks"
expected="Works"
runTest

19
tests/comment-newline Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
template() {
cat <<EOF
<h1>Today{{! ignore me
and this can
run through multiple
lines}}.</h1>
EOF
}
expected() {
cat <<EOF
<h1>Today.</h1>
EOF
}
runTest

View File

@@ -1 +0,0 @@
<h1>Today.</h1>

View File

@@ -1,4 +0,0 @@
<h1>Today{{! ignore me
and this can
run through multiple
lines}}.</h1>

8
tests/comment-with-spaces Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
template="Wor{{! comment }}ks"
expected="Works"
runTest

View File

View File

@@ -1 +0,0 @@
<h1>Today.</h1>

View File

@@ -1 +0,0 @@
<h1>Today{{! ignore me }}.</h1>

10
tests/concatenated-variables Executable file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
thing="Wor"
thing2="ks"
template="{{thing thing2}}"
expected="Works"
runTest

9
tests/delimiters Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
thing="Works"
template="{{=| |=}}|thing|"
expected="Works"
runTest

9
tests/double-hyphen Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
arguments=(-- --help)
template=""
expected="cat: --help: No such file or directory"$'\n'
runTest

View File

@@ -1 +0,0 @@
cat: --help: No such file or directory

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
# This should display a message indicating that the file --help
# could not be found. It should not display a help messsage.
cd "${0%/*}" || exit 1
../mo -- --help 2>&1

8
tests/double-quote Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
template='{{"Works"}}'
expected="Works"
runTest

24
tests/fail-not-set Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
unset __NO_SUCH_VAR
POPULATED="words"
EMPTY=""
arguments=(--fail-not-set)
returnCode=1
template() {
cat <<EOF
Populated: {{POPULATED}};
Empty: {{EMPTY}};
Unset: {{__NO_SUCH_VAR}};
EOF
}
expected() {
cat <<EOF
ERROR: Environment variable not set: __NO_SUCH_VAR
EOF
}
runTest

View File

@@ -1 +0,0 @@
Env variable not set: __NO_SUCH_VAR

View File

@@ -1,9 +0,0 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
unset __NO_SUCH_VAR
POPULATED="words" EMPTY="" ../mo --fail-not-set ./fail-not-set-file.template 2>&1
if [[ $? -ne 1 ]]; then
echo "Did not return 1"
fi

View File

@@ -1,3 +0,0 @@
Populated: {{POPULATED}};
Empty: {{EMPTY}};
Unset: {{__NO_SUCH_VAR}};

View File

@@ -1,3 +0,0 @@
Populated: words;
Empty: ;
Unset: Env variable not set: __NO_SUCH_VAR

View File

@@ -1,13 +0,0 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
unset __NO_SUCH_VAR
POPULATED="words" EMPTY="" ../mo --fail-not-set 2>&1 <<EOF
Populated: {{POPULATED}};
Empty: {{EMPTY}};
Unset: {{__NO_SUCH_VAR}};
EOF
if [[ $? -ne 1 ]]; then
echo "Did not return 1"
fi

18
tests/fail-on-function Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
failFunction() {
false
}
arguments=(--fail-on-function)
returnCode=1
template="Fail on function? {{failFunction}}"
expected() {
cat <<EOF
ERROR: Function 'failFunction' with args () failed with status code 1
EOF
}
runTest

View File

@@ -1 +0,0 @@
Fail on function? Function 'failFunction' with args () failed with status code 1

View File

@@ -1,18 +0,0 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
failFunction() {
false
}
# Must be sourced to use functions
# shellcheck disable=SC1091
. ../mo
mo --fail-on-function 2>&1 <<EOF
Fail on function? {{failFunction}}
EOF
if [[ $? -ne 1 ]]; then
echo "Did not return 1"
fi

22
tests/false-is-empty-arg Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
USER=j.doe
ADMIN=false
arguments=(--false)
template() {
cat <<EOF
The user {{USER}} exists.
{{#ADMIN}}
WRONG - should not be an admin.
{{/ADMIN}}
EOF
}
expected() {
cat <<EOF
The user j.doe exists.
EOF
}
runTest

View File

@@ -1 +0,0 @@
The user j.doe exists.

View File

@@ -1,4 +0,0 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
USER=j.doe ADMIN=false ../mo --false false-is-empty-arg.template

View File

@@ -1,4 +0,0 @@
The user {{USER}} exists.
{{#ADMIN}}
WRONG - should not be an admin.
{{/ADMIN}}

22
tests/false-is-empty-env Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
USER=j.doe
ADMIN=false
MO_FALSE_IS_EMPTY=yeppers
template() {
cat <<EOF
The user {{USER}} exists.
{{#ADMIN}}
WRONG - should not be an admin.
{{/ADMIN}}
EOF
}
expected() {
cat <<EOF
The user j.doe exists.
EOF
}
runTest

View File

@@ -1,2 +0,0 @@
MO_FALSE_IS_EMPTY=yeppers
someFalseValue=false

View File

@@ -1 +0,0 @@
Works

View File

@@ -1,4 +0,0 @@
Works
{{#someFalseValue}}
Never shown!
{{/someFalseValue}}

20
tests/false-list Executable file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
person=""
template() {
cat <<EOF
Shown.
{{#person}}
Never shown!
{{/person}}
EOF
}
expected() {
cat <<EOF
Shown.
EOF
}
runTest

View File

@@ -1 +0,0 @@
person=""

View File

@@ -1 +0,0 @@
Shown.

View File

@@ -1,4 +0,0 @@
Shown.
{{#person}}
Never shown!
{{/person}}

24
tests/function Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
name=Willy
wrapped() {
# This eats the newline in the content
echo "<b>$(cat)</b>"
}
template() {
cat <<EOF
{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
... this is the last line.
EOF
}
expected() {
cat <<EOF
<b> Willy is awesome.</b>... this is the last line.
EOF
}
runTest

35
tests/function-args Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
name=Willy
MO_ALLOW_FUNCTION_ARGUMENTS=true
pipeTo() {
cat | "$1"
}
testArgs() {
printf "%d" "$#"
# Display all arguments
printf " %q" ${@+"$@"}
}
template() {
cat <<EOF
No args: {{testArgs}} - done
One arg: {{testArgs one}} - done
Getting name in a string: {{testArgs "The name is $name"}} - done
Reverse this: {{#pipeTo rev}}abcde{{/pipeTo}}
EOF
}
expected() {
cat <<EOF
No args: 0 '' - done
One arg: 1 one - done
Getting name in a string: 1 The\ name\ is\ Willy - done
Reverse this: edcba
EOF
}
runTest

25
tests/function-args-read Executable file
View File

@@ -0,0 +1,25 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
testArgs() {
echo "$MO_FUNCTION_ARGS"
}
template() {
cat <<EOF
No args: [{{testArgs}}] - done
One arg: [{{testArgs one}}] - done
Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done
Evil: [{{testArgs bla; cat /etc/issue}}] - done
EOF
}
expected() {
cat <<EOF
No args: [] - done
One arg: [one] - done
Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done
Evil: [bla; cat /etc/issue] - done
EOF
}
runTest

View File

@@ -1,3 +0,0 @@
testArgs() {
echo "$MO_FUNCTION_ARGS"
}

View File

@@ -1,4 +0,0 @@
No args: [] - done
One arg: [one] - done
Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done
Evil: [bla; cat /etc/issue] - done

View File

@@ -1,4 +0,0 @@
No args: [{{testArgs}}] - done
One arg: [{{testArgs one}}] - done
Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done
Evil: [{{testArgs bla; cat /etc/issue}}] - done

View File

@@ -1,13 +0,0 @@
name=Willy
MO_ALLOW_FUNCTION_ARGUMENTS=true
pipeTo() {
cat | "$1"
}
testArgs() {
printf "%d" "$#"
# Display all arguments
printf " %q" ${@+"$@"}
}

View File

@@ -1,4 +0,0 @@
No args: 0 '' - done
One arg: 1 one - done
Getting name in a string: 1 The\ name\ is\ Willy - done
Reverse this: edcba

View File

@@ -1,4 +0,0 @@
No args: {{testArgs}} - done
One arg: {{testArgs one}} - done
Getting name in a string: {{testArgs "The name is $name"}} - done
Reverse this: {{#pipeTo rev}}abcde{{/pipeTo}}

View File

@@ -1,5 +0,0 @@
name=Willy
wrapped() {
# This eats the newline in the content
echo "<b>$(cat)</b>"
}

View File

@@ -1 +0,0 @@
<b> Willy is awesome.</b>... this is the last line.

View File

@@ -1,4 +0,0 @@
{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
... this is the last line.

28
tests/globals-in-loop Executable file
View File

@@ -0,0 +1,28 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
STR=abc
DATA=(111 222)
template() {
cat <<EOF
Issue #7
{{STR}}
{{#DATA}}
Item: {{.}}
String: {{STR}}
{{/DATA}}
EOF
}
expected() {
cat <<EOF
Issue #7
abc
Item: 111
String: abc
Item: 222
String: abc
EOF
}
runTest

View File

@@ -1,2 +0,0 @@
STR=abc
DATA=(111 222)

View File

@@ -1,6 +0,0 @@
Issue #7
abc
Item: 111
String: abc
Item: 222
String: abc

View File

@@ -1,6 +0,0 @@
Issue #7
{{STR}}
{{#DATA}}
Item: {{.}}
String: {{STR}}
{{/DATA}}

14
tests/help.expected → tests/help Normal file → Executable file
View File

@@ -1,3 +1,11 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
template=""
arguments=(--help)
expected() {
cat <<EOF
Mo is a mustache template rendering software written in bash. It inserts
environment variables into templates.
@@ -25,4 +33,8 @@ Options:
Load FILE into the environment before processing templates.
Can be used multiple times.
MO_VERSION=2.4.1
MO_VERSION=3.0.0
EOF
}
runTest

View File

@@ -1,4 +0,0 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
../mo --help

56
tests/indented-partials Executable file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
thisIsTrue=true
template() {
cat <<EOF
With spacing
{{> fixtures/indented-partials.partial}}
{{> fixtures/indented-partials.partial}}
Without spacing
{{> fixtures/indented-partials.partial}}
{{> fixtures/indented-partials.partial}}
With text
{{> fixtures/indented-partials.partial}}
text
{{> fixtures/indented-partials.partial}}
In a conditional
{{#thisIsTrue}}
{{> fixtures/indented-partials.partial}}
{{/thisIsTrue}}
EOF
}
expected() {
cat <<EOF
With spacing
first line
second line
first line
second line
Without spacing
first line
second line
first line
second line
With text
first line
second line
text
first line
second line
In a conditional
first line
second line
EOF
}
runTest

View File

@@ -1 +0,0 @@
thisIsTrue=true

View File

@@ -1,23 +0,0 @@
With spacing
first line
second line
first line
second line
Without spacing
first line
second line
first line
second line
With text
first line
second line
text
first line
second line
In a conditional
first line
second line

View File

@@ -1,18 +0,0 @@
With spacing
{{> indented-partials.partial}}
{{> indented-partials.partial}}
Without spacing
{{> indented-partials.partial}}
{{> indented-partials.partial}}
With text
{{> indented-partials.partial}}
text
{{> indented-partials.partial}}
In a conditional
{{#thisIsTrue}}
{{> indented-partials.partial}}
{{/thisIsTrue}}

14
tests/invalid-option Executable file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
person=""
template=""
arguments=(--something)
expected() {
cat <<EOF
cat: --something: No such file or directory
EOF
}
runTest

View File

@@ -1 +0,0 @@
cat: --something: No such file or directory

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env bash
# This should display a message indicating that the file --something
# could not be found.
cd "${0%/*}" || exit 1
../mo --something 2>&1

22
tests/inverted Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
repo=()
template() {
cat <<EOF
{{#repo}}
<b>{{.}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
EOF
}
expected() {
cat <<EOF
No repos :(
EOF
}
runTest

View File

@@ -1 +0,0 @@
repo=()

View File

@@ -1,2 +0,0 @@
No repos :(

View File

@@ -1,6 +0,0 @@
{{#repo}}
<b>{{.}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}

24
tests/miss Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
name="Chris"
company="<b>GitHub</b>"
template() {
cat <<EOF
* .{{name}}.
* .{{age}}.
* .{{company}}.
* .{{{company}}}.
EOF
}
expected() {
cat <<EOF
* .Chris.
* ..
* .<b>GitHub</b>.
* .<b>GitHub</b>.
EOF
}
runTest

View File

@@ -1,2 +0,0 @@
name="Chris"
company="<b>GitHub</b>"

View File

@@ -1,4 +0,0 @@
* .Chris.
* ..
* .<b>GitHub</b>.
* .<b>GitHub</b>.

View File

@@ -1,4 +0,0 @@
* .{{name}}.
* .{{age}}.
* .{{company}}.
* .{{{company}}}.

31
tests/multi-line-partial Executable file
View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
multilineData=$'line 1\nline 2'
template() {
cat <<EOF
Partial:
{{> fixtures/multi-line-partial.partial}}
Indented:
{{> fixtures/multi-line-partial.partial}}
EOF
}
expected() {
cat <<EOF
Partial:
line 1
line 2
Indented:
line 1
line 2
EOF
}
runTest

View File

@@ -1 +0,0 @@
multilineData=$'line 1\nline 2'

View File

@@ -1,9 +0,0 @@
Partial:
line 1
line 2
Indented:
line 1
line 2

View File

@@ -1,7 +0,0 @@
Partial:
{{> multi-line-partial.partial}}
Indented:
{{> multi-line-partial.partial}}

33
tests/mush Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
USER=jwerle
GENDER=male
THING=apple
COLOR=red
PERSON=tobi
ADJECTIVE=cool
template() {
cat <<EOF
{{! this is a comment }}
{{USER}} is {{GENDER}}
{{THING}} is {{COLOR}}
{{PERSON}} is {{ADJECTIVE}}
{{USER}} is friends with {{PERSON}}
{{var}} {{value}}
EOF
}
expected() {
cat <<EOF
jwerle is male
apple is red
tobi is cool
jwerle is friends with tobi
EOF
}
runTest

View File

@@ -1,6 +0,0 @@
USER=jwerle
GENDER=male
THING=apple
COLOR=red
PERSON=tobi
ADJECTIVE=cool

View File

@@ -1,6 +0,0 @@
jwerle is male
apple is red
tobi is cool
jwerle is friends with tobi

View File

@@ -1,7 +0,0 @@
{{! this is a comment }}
{{USER}} is {{GENDER}}
{{THING}} is {{COLOR}}
{{PERSON}} is {{ADJECTIVE}}
{{USER}} is friends with {{PERSON}}
{{var}} {{value}}

8
tests/no-content Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
template="Works"
expected="Works"
runTest

22
tests/partial Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
names=( "Tyler" )
template() {
cat <<EOF
<h2>Names</h2>
{{#names}}
{{> partial.partial}}
{{/names}}
EOF
}
expected() {
cat <<EOF
<h2>Names</h2>
<strong>Tyler</strong>
EOF
}
runTest

18
tests/partial-missing Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
. ../run-tests
returnCode=1
person=""
template() {
cat <<EOF
Won't be there: {{> fixtures/partial-missing.partial}}
EOF
}
expected() {
cat <<EOF
cat: partial-missing.partial: No such file or directory
EOF
}
runTest

View File

@@ -1 +0,0 @@
cat: partial-missing.partial: No such file or directory

View File

@@ -1,8 +0,0 @@
#!/usr/bin/env bash
cd "${0%/*}" || exit 1
../mo partial-missing.template 2>&1
if [[ $? -ne 1 ]]; then
echo "Did not return 1"
fi

View File

@@ -1 +0,0 @@
Won't be there: {{> partial-missing.partial}}

View File

@@ -1 +0,0 @@
names=( "Tyler" )

View File

@@ -1,3 +0,0 @@
<h2>Names</h2>
<strong>Tyler</strong>

Some files were not shown because too many files have changed in this diff Show More