Fixing standalone detection with blocks

27 tests pass, 18 still fail
This commit is contained in:
Tyler Akins 2023-04-22 08:16:49 -05:00
parent 00af7d49f0
commit 0f150ccb19
No known key found for this signature in database
GPG Key ID: 8F3B8C432F4393BD
2 changed files with 65 additions and 11 deletions

32
mo
View File

@ -517,7 +517,7 @@ mo::parseBlock() {
mo::tokensToString moTokensString "${moTokens[@]}" mo::tokensToString moTokensString "${moTokens[@]}"
mo::debug "Parsing block: $moTokensString" mo::debug "Parsing block: $moTokensString"
if mo::standaloneCheck; then if mo::standaloneCheck "$MO_STANDALONE_CONTENT"; then
mo::standaloneProcess mo::standaloneProcess
fi fi
@ -671,7 +671,7 @@ mo::parsePartial() {
MO_UNPARSED="${MO_UNPARSED#*"$MO_CLOSE_DELIMITER"}" MO_UNPARSED="${MO_UNPARSED#*"$MO_CLOSE_DELIMITER"}"
moIndentation="" moIndentation=""
if mo::standaloneCheck; then if mo::standaloneCheck "$MO_STANDALONE_CONTENT"; then
moN=$'\n' moN=$'\n'
moR=$'\r' moR=$'\r'
moIndentation="$moN${MO_PARSED//"$moR"/"$moN"}" moIndentation="$moN${MO_PARSED//"$moR"/"$moN"}"
@ -725,7 +725,7 @@ mo::parseComment() {
MO_UNPARSED=${MO_UNPARSED#*"$MO_CLOSE_DELIMITER"} MO_UNPARSED=${MO_UNPARSED#*"$MO_CLOSE_DELIMITER"}
mo::debug "Parsing comment" mo::debug "Parsing comment"
if mo::standaloneCheck; then if mo::standaloneCheck "$MO_STANDALONE_CONTENT"; then
mo::standaloneProcess mo::standaloneProcess
fi fi
} }
@ -746,7 +746,7 @@ mo::parseDelimiter() {
MO_UNPARSED=${MO_UNPARSED#*="$MO_CLOSE_DELIMITER"} MO_UNPARSED=${MO_UNPARSED#*="$MO_CLOSE_DELIMITER"}
mo::debug "Parsing delimiters: $moOpen $moClose" mo::debug "Parsing delimiters: $moOpen $moClose"
if mo::standaloneCheck; then if mo::standaloneCheck "$MO_STANDALONE_CONTENT"; then
mo::standaloneProcess mo::standaloneProcess
fi fi
@ -1234,6 +1234,8 @@ mo::evaluateFunction() {
# it on a line. There must be a new line before and there must be a newline # it on a line. There must be a new line before and there must be a newline
# after or the end of a string # after or the end of a string
# #
# $1 - The content before the tag.
#
# Returns 0 if this is a standalone tag, 1 otherwise. # Returns 0 if this is a standalone tag, 1 otherwise.
mo::standaloneCheck() { mo::standaloneCheck() {
local moContent moN moR moT local moContent moN moR moT
@ -1243,7 +1245,7 @@ mo::standaloneCheck() {
moT=$'\t' moT=$'\t'
# Check the content before # Check the content before
moContent=${MO_STANDALONE_CONTENT//"$moR"/"$moN"} moContent=${1//"$moR"/"$moN"}
# By default, signal to the next check that this one failed # By default, signal to the next check that this one failed
MO_STANDALONE_CONTENT="" MO_STANDALONE_CONTENT=""
@ -1283,7 +1285,11 @@ mo::standaloneCheck() {
} }
# Internal: Process content before and after a tag. Remove prior whitespace up to the previous newline. Remove following whitespace up to and including the next newline. # Internal: Process content before and after a tag. Remove prior whitespace up
# to the previous newline. Remove following whitespace up to and including the
# next newline.
#
# No arguments.
# #
# Returns nothing. # Returns nothing.
mo::standaloneProcess() { mo::standaloneProcess() {
@ -1380,14 +1386,14 @@ mo::escape() {
# #
# Returns nothing. # Returns nothing.
mo::getContentUntilClose() { mo::getContentUntilClose() {
local moChunk moResult moTemp moTokensString moTokens moTarget moTagStack moResultTemp local moChunk moResult moTemp moTokensString moTokens moTarget moTagStack moResultTemp moStandaloneTemp
moTarget=$1 moTarget=$1
moTagStack=("$2") moTagStack=("$2")
mo::debug "Get content until close tag: ${moTagStack[0]}" mo::debug "Get content until close tag: ${moTagStack[0]}"
moResult="" moResult=""
while [[ -n "$MO_UNPARSED" ]]; do while [[ -n "$MO_UNPARSED" ]] && [[ "${#moTagStack[@]}" -gt 0 ]]; do
moChunk=${MO_UNPARSED%%"$MO_OPEN_DELIMITER"*} moChunk=${MO_UNPARSED%%"$MO_OPEN_DELIMITER"*}
moResult="$moResult$moChunk" moResult="$moResult$moChunk"
MO_UNPARSED=${MO_UNPARSED:${#moChunk}} MO_UNPARSED=${MO_UNPARSED:${#moChunk}}
@ -1482,9 +1488,13 @@ mo::getContentUntilClose() {
fi fi
done done
# FIXME - handle standalone if mo::standaloneCheck "$moResult"; then
mo::debug "FIXME handle standalone" moResultTemp=$MO_PARSED
mo::debug "Block: $moResult" MO_PARSED=$moResult
mo::standaloneProcess
moResult=$MO_PARSED
MO_PARSED=$moResultTemp
fi
local "$moTarget" && mo::indirect "$moTarget" "$moResult" local "$moTarget" && mo::indirect "$moTarget" "$moResult"
} }

View File

@ -1,4 +1,42 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#
# Run one or more tests.
#
# Command-line usage to run all tests.
#
# ./run-tests
#
# To run only one test, run "tests/test-name".
#
# Usage within a test as a template. Source run-tests to get functions, export
# any necessary variables, then call runTest.
#
# #!/usr/bin/env bash
# cd "${0%/*}" || exit 1
# . ../run-tests
#
# export template="This is a template"
# export expected="This is a template"
# runTest
#
# When used within the test, you control various aspects with environment
# variables or functions.
#
# - The content passed into mo is either the variable "$template" or the output
# of the function called template.
# - The expected result is either "$expected" or the function called expected.
# - The expected return code is "$returnCode" and defaults to 0.
# - The arguments to pass to mo is the array "${arguments[@]}" and defaults to ().
#
# When $MO_DEBUG is set to a non-empty value, the test does not run, but mo is
# simply executed directly. This allows for calling mo in the same manner as
# the test but does not buffer output nor expect the output to match the
# expected.
#
# When $MO_DEBUG_TEST is set to a non-empty value, the expected and actual
# results are shown using "declare -p" to provide an easier time seeing the
# differences, especially with whitespace.
testCase() { testCase() {
echo "Input: $1" echo "Input: $1"
echo "Expected: $2" echo "Expected: $2"
@ -38,6 +76,12 @@ runTest() (
getValue testTemplate template getValue testTemplate template
getValue testExpected expected getValue testExpected expected
if [[ -n "${MO_DEBUG:-}" ]]; then
echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1
return $?
fi
testActual=$(echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1; echo -n "$hardSpace$?") testActual=$(echo -n "$testTemplate" | mo ${arguments[@]+"${arguments[@]}"} 2>&1; echo -n "$hardSpace$?")
testReturnCode=${testActual##*$hardSpace} testReturnCode=${testActual##*$hardSpace}
testActual=${testActual%$hardSpace*} testActual=${testActual%$hardSpace*}