test(hemar): many tests but not pass

This commit is contained in:
2025-12-09 13:49:43 +00:00
parent 73dc9aee9e
commit 6fb3ee1af8
10 changed files with 260 additions and 79 deletions

View File

@@ -503,6 +503,7 @@ if [ -z "${AS_LIBRARY+x}" ]; then
log notice "running" log notice "running"
AST=$(mktemp) AST=$(mktemp)
yq -o j -i "[]" "$AST"
AST_key='.' AST_key='.'
trap 'rm -f "$AST"' EXIT INT HUP trap 'rm -f "$AST"' EXIT INT HUP
@@ -578,11 +579,14 @@ if [ -z "${AS_LIBRARY+x}" ]; then
fi fi
buf=$(cat "$STAGE_BUFFER_1") buf=$(cat "$STAGE_BUFFER_1")
# Only add text element if buffer is not empty
if [ -n "$buf" ]; then
yq -o j -i "$AST_key += [{ yq -o j -i "$AST_key += [{
\"type\": \"text\", \"type\": \"text\",
\"value\": \"$(json_escape "$buf")\" \"value\": \"$(json_escape "$buf")\"
}]" "$AST" }]" "$AST"
fi fi
fi
# return the output # return the output
# shellcheck disable=SC2086 # shellcheck disable=SC2086

View File

@@ -6,11 +6,20 @@ json_diff() {
temp1=$(mktemp) temp1=$(mktemp)
temp2=$(mktemp) temp2=$(mktemp)
yq -I=0 -o=j -n "$1" >"$temp1" # Normalize JSON strings for comparison
yq -I=0 -o=j -n "$2" >"$temp2" 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 if ! diff -q "$temp1" "$temp2" >/dev/null 2>&1; then
log error "$(yq -o=j -n "$1")" and "$(yq -o=j -n "$2")" log error "JSON mismatch:"
log error " Expected: $WHITE$(cat "$temp2")"
log error " Got: $WHITE$(cat "$temp1")"
exit 1 exit 1
fi fi
} }

View File

@@ -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"

View File

@@ -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"

View File

@@ -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)"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"