From 30100855cf2baaa7a34bfa5381b891e50154e568 Mon Sep 17 00:00:00 2001 From: perro Date: Wed, 15 Mar 2023 15:12:48 -0700 Subject: [PATCH] Finally test.sh again ready --- README.md | 14 +++++----- dist/linp.lua | 2 +- scripts/test.sh | 68 ++++++++++++++++++++++++++++++++++++++++--------- src/pandoc.lua | 2 +- 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6488557..5935ad8 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ For example, if `DOC` is `source.md` and the output `FORMAT` is HTML, do: Learn how to do LiNP [here]. -## Test +## Develop Clone this repo: @@ -38,17 +38,15 @@ Enter the repo: cd literate-pandoc -Inside, do: +Inside, do the tests: - sh scripts/test.sh FORMAT1 FORMAT2 + sh scripts/test.sh -For example, if `FORMAT1` is Markdwon and `FORMAT2` is HTML, do: +For other kind of tests, do: - sh tests/test.sh markdown html + sh scripts/test.sh -h -For distribution tests, do: - - sh scripts/test.sh FORMAT1 FORMAT2 +Contribute! ## Acknowledgments diff --git a/dist/linp.lua b/dist/linp.lua index 60f8ecd..c29cbc7 100644 --- a/dist/linp.lua +++ b/dist/linp.lua @@ -6352,7 +6352,7 @@ return { if is_chosen(block) then raw = sanitize(block.content) doc = lit.parse(raw) - print(doc) + -- print(doc) end end, } diff --git a/scripts/test.sh b/scripts/test.sh index 404c645..66e162e 100644 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,13 +2,52 @@ # Variables FILTER=dist/linp.lua +FILES="tests/*.md" +VERBOSE=false +CMD="sh $0" -# Checks args -if [ -z "$@" ]; then - echo "ERROR: At least one argument is needed. For example:" - echo " sh $0 native" - echo " sh $0 native markdown" - exit 1 +# Prints help +echo_help () { + echo "Usage:" + echo " $CMD [-vh] [FILES]" + echo "Examples:" + echo " $CMD Tests with 'tests/*.md'" + echo " $CMD -v Verbose tests with 'tests/*.md'" + echo " $CMD tests/fail* Tests with 'tests/fail*'" + echo " $CMD -v tests/fail* Verbose tests with 'tests/fail*'" + echo " $CMD -h Display this help" + exit +} + +# Obtains result as "pass" | "fail" | "diff" (AST doesn't match) +get_result () { + result=$([[ $1 -eq 0 ]] && echo "pass" || echo "fail") + if [ -f $2.json ]; then + diff1=$(cat tmp.json | jq) + diff2=$(cat $2.json | jq) + difference=$(diff <( printf '%s\n' "$diff1" ) <( printf '%s\n' "$diff2" )) + result=$([[ $? -eq 0 ]] && echo "pass" || echo "diff") + fi + echo $result +} + +# Checks options +while getopts ':vh' opt; do + case "$opt" in + v) VERBOSE=true ; shift ;; + h) echo_help ;; + ?) echo "ERROR: only -v or -h is allowed" ; exit 1 ;; + esac +done + +# Checks for files +if [ $# -ne 0 ]; then + for file in $*; do + if [ ! -f $file ]; then + echo "ERROR: '$file' is not a file; do '$CMD -h' for more info" ; exit 1 + fi + done + FILES=$* fi # Makes distribution bundle @@ -16,10 +55,15 @@ sh scripts/make_dist.sh # Does tests clear && echo "🐾 Starting tests" -for arg in "$@"; do - echo && echo "⚗️ Test in '$arg' format:" - mds=$'\n\n'`(pandoc -t markdown tests/*.md)` - rst=$'\n\n'`(pandoc -t markdown tests/*.rst)` - org=$'\n\n'`(pandoc -t markdown tests/*.org)` - echo "$mds" "$rst" "$org" | pandoc -L $FILTER -t $arg +for testfile in $FILES; do + echo "⚗️ $testfile:" + expectation=${testfile:6:4} + ast=$(pandoc -L $FILTER -t json -o tmp.json $testfile) + result=$(get_result $? $testfile) + echo " Expect: $expectation" + echo " Result: $result" + if [ "$VERBOSE" = true ]; then + pandoc -L $FILTER -t native $testfile + fi + rm tmp.json done diff --git a/src/pandoc.lua b/src/pandoc.lua index e0f6807..8873cc6 100644 --- a/src/pandoc.lua +++ b/src/pandoc.lua @@ -26,7 +26,7 @@ return { if is_chosen(block) then raw = sanitize(block.content) doc = lit.parse(raw) - print(doc) + -- print(doc) end end, }