From 6fb3ee1af83e3423004d337394a3d3518e7dbbc8 Mon Sep 17 00:00:00 2001 From: yukkop Date: Tue, 9 Dec 2025 13:49:43 +0000 Subject: [PATCH] test(hemar): many tests but not pass --- package/hemar/parser/hemar.sh | 12 +++-- test/package/hemar/lauch.sh | 19 ++++++-- test/package/hemar/test/parser-edge-cases.sh | 47 +++++++++++++++++++ .../hemar/test/parser-escaped-brackets.sh | 27 +++++++++++ test/package/hemar/test/parser-for-loops.sh | 38 +++++++++++++++ .../hemar/test/parser-interpolation.sh | 42 +++++++++++++++++ test/package/hemar/test/parser-paths.sh | 47 +++++++++++++++++++ test/package/hemar/test/parser-text.sh | 37 +++++++++++++++ test/package/hemar/test/simply-text.sh | 32 ------------- .../package/hemar/test/some-interpolations.sh | 38 --------------- 10 files changed, 260 insertions(+), 79 deletions(-) create mode 100644 test/package/hemar/test/parser-edge-cases.sh create mode 100644 test/package/hemar/test/parser-escaped-brackets.sh create mode 100644 test/package/hemar/test/parser-for-loops.sh create mode 100644 test/package/hemar/test/parser-interpolation.sh create mode 100644 test/package/hemar/test/parser-paths.sh create mode 100644 test/package/hemar/test/parser-text.sh delete mode 100644 test/package/hemar/test/simply-text.sh delete mode 100644 test/package/hemar/test/some-interpolations.sh diff --git a/package/hemar/parser/hemar.sh b/package/hemar/parser/hemar.sh index 6264103..6ba1ca5 100644 --- a/package/hemar/parser/hemar.sh +++ b/package/hemar/parser/hemar.sh @@ -503,6 +503,7 @@ if [ -z "${AS_LIBRARY+x}" ]; then log notice "running" AST=$(mktemp) + yq -o j -i "[]" "$AST" AST_key='.' trap 'rm -f "$AST"' EXIT INT HUP @@ -578,10 +579,13 @@ if [ -z "${AS_LIBRARY+x}" ]; then fi buf=$(cat "$STAGE_BUFFER_1") - yq -o j -i "$AST_key += [{ - \"type\": \"text\", - \"value\": \"$(json_escape "$buf")\" - }]" "$AST" + # Only add text element if buffer is not empty + if [ -n "$buf" ]; then + yq -o j -i "$AST_key += [{ + \"type\": \"text\", + \"value\": \"$(json_escape "$buf")\" + }]" "$AST" + fi fi # return the output diff --git a/test/package/hemar/lauch.sh b/test/package/hemar/lauch.sh index 107c0f0..29971df 100644 --- a/test/package/hemar/lauch.sh +++ b/test/package/hemar/lauch.sh @@ -6,12 +6,21 @@ json_diff() { temp1=$(mktemp) temp2=$(mktemp) - yq -I=0 -o=j -n "$1" >"$temp1" - yq -I=0 -o=j -n "$2" >"$temp2" + # Normalize JSON strings for comparison + printf '%s' "$1" | yq -I=0 -o=j . >"$temp1" 2>/dev/null || { + log error "first argument is not valid JSON: $WHITE$1" + exit 1 + } + printf '%s' "$2" | yq -I=0 -o=j . >"$temp2" 2>/dev/null || { + log error "second argument is not valid JSON: $WHITE$2" + exit 1 + } - if ! diff -q "$temp1" "$temp2"; then - log error "$(yq -o=j -n "$1")" and "$(yq -o=j -n "$2")" - exit 1 + if ! diff -q "$temp1" "$temp2" >/dev/null 2>&1; then + log error "JSON mismatch:" + log error " Expected: $WHITE$(cat "$temp2")" + log error " Got: $WHITE$(cat "$temp1")" + exit 1 fi } diff --git a/test/package/hemar/test/parser-edge-cases.sh b/test/package/hemar/test/parser-edge-cases.sh new file mode 100644 index 0000000..63cd06c --- /dev/null +++ b/test/package/hemar/test/parser-edge-cases.sh @@ -0,0 +1,47 @@ +#!/bin/dash + +# Test: Edge cases and error handling +# Tests various edge cases and malformed input + +log notice "test case: ${WHITE}incomplete tag - single brace" +answer="$(printf '%s' 'text {' | hemar -c)" +expected='[{"type":"text","value":"text {"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}incomplete tag - {[ without closing" +answer="$(printf '%s' 'text {[' | hemar -c)" +expected='[{"type":"text","value":"text {"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}whitespace in tag" +answer="$(printf '%s' '{[ key ]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}newlines in tag" +answer="$(printf '{[\nkey\n]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}empty interpolation tag" +answer="$(printf '%s' '{[ ]}' | hemar -c)" +expected='[{"type":"interpolation","path":""}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}text with only braces" +answer="$(printf '%s' '{ }' | hemar -c)" +expected='[{"type":"text","value":"{ }"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}text with only brackets" +answer="$(printf '%s' '[ ]' | hemar -c)" +expected='[{"type":"text","value":"[ ]"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}consecutive interpolations" +answer="$(printf '%s' '{[a]}{[b]}{[c]}' | hemar -c)" +expected='[{"type":"interpolation","path":"a"},{"type":"interpolation","path":"b"},{"type":"interpolation","path":"c"}]' +json_diff "$answer" "$expected" + +log notice "test passed" + diff --git a/test/package/hemar/test/parser-escaped-brackets.sh b/test/package/hemar/test/parser-escaped-brackets.sh new file mode 100644 index 0000000..049fd09 --- /dev/null +++ b/test/package/hemar/test/parser-escaped-brackets.sh @@ -0,0 +1,27 @@ +#!/bin/dash + +# Test: Escaped brackets +# Tests that {[ {[ ]} correctly outputs literal {[ + +log notice "test case: ${WHITE}escaped bracket" +answer="$(printf '%s' '{[ {[ ]}' | hemar -c)" +expected='[{"type":"text","value":"{["}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}escaped bracket with text" +answer="$(printf '%s' 'text {[ {[ ]} more text' | hemar -c)" +expected='[{"type":"text","value":"text {[ more text"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}multiple escaped brackets" +answer="$(printf '%s' '{[ {[ ]} {[ {[ ]}' | hemar -c)" +expected='[{"type":"text","value":"{[ {["}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}escaped bracket merged with previous text" +answer="$(printf '%s' 'hello{[ {[ ]}world' | hemar -c)" +expected='[{"type":"text","value":"hello{[world"}]' +json_diff "$answer" "$expected" + +log notice "test passed" + diff --git a/test/package/hemar/test/parser-for-loops.sh b/test/package/hemar/test/parser-for-loops.sh new file mode 100644 index 0000000..7eb2d75 --- /dev/null +++ b/test/package/hemar/test/parser-for-loops.sh @@ -0,0 +1,38 @@ +#!/bin/dash + +# Test: For loops (MVP feature - currently unimplemented) +# These tests document expected behavior when for loops are implemented + +log notice "test case: ${WHITE}for loop structure (should fail until implemented)" +if answer="$(printf '%s' '{[ for i in items ]}' | hemar -c 2>&1)"; then + log error "test failed: ${WHITE}for loop should not be implemented yet, but parser succeeded" + exit 1 +fi +log notice "test case: ${WHITE}for loop correctly rejected (expected behavior)" + +log notice "test case: ${WHITE}for loop with done (should fail until implemented)" +if answer="$(printf '{[ for i in items ]}\n content\n{[ done ]}' | hemar -c 2>&1)"; then + log error "test failed: ${WHITE}for loop should not be implemented yet, but parser succeeded" + exit 1 +fi +log notice "test case: ${WHITE}for loop with done correctly rejected (expected behavior)" + +# When for loops are implemented, these should be the expected outputs: +# +# log notice "test case: ${WHITE}simple for loop" +# answer="$(printf '{[ for i in items ]}\n{[ done ]}' | hemar -c)" +# expected='[{"type":"section","variable":"i","path":"items","body":[]}]' +# json_diff "$answer" "$expected" +# +# log notice "test case: ${WHITE}for loop with content" +# answer="$(printf '{[ for i in items ]}\n hello {[i]}\n{[ done ]}' | hemar -c)" +# expected='[{"type":"section","variable":"i","path":"items","body":[{"type":"text","value":" hello "},{"type":"interpolation","path":"i"}]}]' +# json_diff "$answer" "$expected" +# +# log notice "test case: ${WHITE}nested for loops" +# answer="$(printf '{[ for i in items ]}\n {[ for j in i.subitems ]}\n {[j]}\n {[ done ]}\n{[ done ]}' | hemar -c)" +# expected='[{"type":"section","variable":"i","path":"items","body":[{"type":"text","value":" "},{"type":"section","variable":"j","path":"i.subitems","body":[{"type":"text","value":" "},{"type":"interpolation","path":"j"}]}]}]' +# json_diff "$answer" "$expected" + +log notice "test passed (for loops not yet implemented - this is expected)" + diff --git a/test/package/hemar/test/parser-interpolation.sh b/test/package/hemar/test/parser-interpolation.sh new file mode 100644 index 0000000..9e78b56 --- /dev/null +++ b/test/package/hemar/test/parser-interpolation.sh @@ -0,0 +1,42 @@ +#!/bin/dash + +# Test: Interpolation parsing +# Tests that {[path]} tags are correctly parsed into interpolation elements + +log notice "test case: ${WHITE}simple interpolation" +answer="$(printf '%s' '{[hello]}' | hemar -c)" +expected='[{"type":"interpolation","path":"hello"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}interpolation with text before and after" +answer="$(printf '%s' 'foo {[bar]} baz' | hemar -c)" +expected='[{"type":"text","value":"foo "},{"type":"interpolation","path":"bar"},{"type":"text","value":" baz"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}root path" +answer="$(printf '%s' '{[.]}' | hemar -c)" +expected='[{"type":"interpolation","path":"."}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}simple path" +answer="$(printf '%s' '{[key]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}dot-separated path" +answer="$(printf '%s' '{[key.subkey]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key.subkey"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}long path" +answer="$(printf '%s' '{[key.subkey.subsubkey]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key.subkey.subsubkey"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}multiple interpolations" +answer="$(printf '%s' '{[a]} {[b]} {[c]}' | hemar -c)" +expected='[{"type":"interpolation","path":"a"},{"type":"text","value":" "},{"type":"interpolation","path":"b"},{"type":"text","value":" "},{"type":"interpolation","path":"c"}]' +json_diff "$answer" "$expected" + +log notice "test passed" + diff --git a/test/package/hemar/test/parser-paths.sh b/test/package/hemar/test/parser-paths.sh new file mode 100644 index 0000000..566d2f9 --- /dev/null +++ b/test/package/hemar/test/parser-paths.sh @@ -0,0 +1,47 @@ +#!/bin/dash + +# Test: Path parsing +# Tests various path formats: quoted strings, indices, mixed paths + +log notice "test case: ${WHITE}quoted string in path" +answer="$(printf '%s' '{["key with spaces"]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key with spaces"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}quoted dot in path" +answer="$(printf '%s' '{[".key"]}' | hemar -c)" +expected='[{"type":"interpolation","path":".key"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}quoted vs unquoted dot" +answer="$(printf '%s' '{["."]} {[.]}' | hemar -c)" +expected='[{"type":"interpolation","path":"."},{"type":"text","value":" "},{"type":"interpolation","path":"."}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}path with index" +answer="$(printf '%s' '{[key[0]]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key[0]"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}path with multiple indices" +answer="$(printf '%s' '{[[0][1][2]]}' | hemar -c)" +expected='[{"type":"interpolation","path":"[0][1][2]"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}path with negative index" +answer="$(printf '%s' '{[key[-1]]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key[-1]"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}complex path with mixed segments" +answer="$(printf '%s' '{["key".subkey[0]."subsubkey"]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key.subkey[0].subsubkey"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}quoted string with escaped quote" +answer="$(printf '%s' '{["key""with""quotes"]}' | hemar -c)" +expected='[{"type":"interpolation","path":"key\"with\"quotes"}]' +json_diff "$answer" "$expected" + +log notice "test passed" + diff --git a/test/package/hemar/test/parser-text.sh b/test/package/hemar/test/parser-text.sh new file mode 100644 index 0000000..6452db2 --- /dev/null +++ b/test/package/hemar/test/parser-text.sh @@ -0,0 +1,37 @@ +#!/bin/dash + +# Test: Text parsing +# Tests that plain text is correctly parsed into text elements + +log notice "test case: ${WHITE}simple text" +answer="$(printf '%s' 'some text' | hemar -c)" +expected='[{"type":"text","value":"some text"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}text with brackets and braces" +answer="$(printf '%s' 'some [] {} text' | hemar -c)" +expected='[{"type":"text","value":"some [] {} text"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}text ending with single brace" +answer="$(printf '%s' 'some {' | hemar -c)" +expected='[{"type":"text","value":"some {"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}empty input" +answer="$(printf '%s' '' | hemar -c)" +expected='[]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}text with newlines" +answer="$(printf 'line1\nline2\nline3' | hemar -c)" +expected='[{"type":"text","value":"line1\nline2\nline3"}]' +json_diff "$answer" "$expected" + +log notice "test case: ${WHITE}text with special characters" +answer="$(printf '%s' 'text with "quotes" and \backslashes' | hemar -c)" +expected='[{"type":"text","value":"text with \"quotes\" and \\backslashes"}]' +json_diff "$answer" "$expected" + +log notice "test passed" + diff --git a/test/package/hemar/test/simply-text.sh b/test/package/hemar/test/simply-text.sh deleted file mode 100644 index f9650e1..0000000 --- a/test/package/hemar/test/simply-text.sh +++ /dev/null @@ -1,32 +0,0 @@ -#answer="$(printf '%s' 'some text' | hemar -c)" -# -#expected="$(printf '%s' '[ -# { -# "type": "text", -# "value": "some text" -# } -#]')" -# -#json_diff "$answer" "$expected" -# -#answer="$(printf '%s' 'some [] {} text' | hemar -c)" -# -#expected="$(printf '%s' '[ -# { -# "type": "text", -# "value": "some [] {} text" -# } -#]')" -# -#json_diff "$answer" "$expected" -# -#answer="$(printf '%s' 'some {' | hemar -c)" -# -#expected="$(printf '%s' '[ -# { -# "type": "text", -# "value": "some {" -# } -#]')" -# -#json_diff "$answer" "$expected" diff --git a/test/package/hemar/test/some-interpolations.sh b/test/package/hemar/test/some-interpolations.sh deleted file mode 100644 index 8236f3e..0000000 --- a/test/package/hemar/test/some-interpolations.sh +++ /dev/null @@ -1,38 +0,0 @@ -#answer="$(printf '%s' 'text begind {[ "inn \\e\"r"-t\"ext ]}' | hemar -c)" -# -#expected="$(printf '%s' '[ -# { -# "type": "text", -# "value": "text begind " -# }, -# { -# "type": "interpolation", -# "path": "inn \\e\"r-t\"ext" -# } -#]')" -# -#json_diff "$answer" "$expected" -# -#[ "$(printf '%s' "$answer" | yq '.[1] | .path')" = 'inn \e"r-t"ext' ] || { -# log error 'unexpected' -# exit 1 -#} -# -#answer="$(printf '%s' 'text begind {[ [" "] ]}' | hemar -c)" -# -#expected="$(printf '%s' '[ -# { -# "type": "text", -# "value": "text begind " -# }, -# { -# "type": "interpolation", -# "path": "[ ]" -# } -#]')" -# -#json_diff "$answer" "$expected" -# -#answer="$(printf '%s' 'text begind {[ [" "\ ] ]}' | hemar -c)" -# -#json_diff "$answer" "$expected"