Fix: Make -J flag a minimal hook
The -J flag was outputting 'valid' or 'discarded' instead of the raw JSON result. This was test logic that should be in the test scripts. Changed the -J flag to output the raw JSON result (pretty-printed) or an error message. Updated the test script to use jq for JSON validation. All other debug flags (-E, -e, -R, -P, -B, -A, -T) were already minimal hooks and didn't need to be changed.
This commit is contained in:
+3
-3
@@ -423,10 +423,10 @@ int main(int argc, char** argv) {
|
||||
if (test_json_parse) {
|
||||
auto result = parse_json_safely(test_json_parse);
|
||||
if (result.is_discarded()) {
|
||||
std::cout << "discarded" << std::endl;
|
||||
} else {
|
||||
std::cout << "valid" << std::endl;
|
||||
std::cerr << "error: invalid JSON" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << result.dump(2) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,43 +3,64 @@
|
||||
|
||||
# Test parse_json_safely with valid JSON
|
||||
OUTPUT=$("${DRHARNESS}" -J '{"key": "value"}')
|
||||
if [ "${OUTPUT}" != "valid" ]; then
|
||||
echo "FAIL: parse_json_safely - expected 'valid' for valid JSON, got '${OUTPUT}'"
|
||||
if ! echo "$OUTPUT" | jq -e '.key == "value"' > /dev/null 2>&1; then
|
||||
echo "FAIL: parse_json_safely - expected valid JSON with key='value', got '${OUTPUT}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Test parse_json_safely with valid JSON object (empty)
|
||||
OUTPUT=$("${DRHARNESS}" -J '{}')
|
||||
if [ "${OUTPUT}" != "valid" ]; then
|
||||
echo "FAIL: parse_json_safely - expected 'valid' for empty object, got '${OUTPUT}'"
|
||||
if ! echo "$OUTPUT" | jq -e 'type == "object"' > /dev/null 2>&1; then
|
||||
echo "FAIL: parse_json_safely - expected valid empty object, got '${OUTPUT}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Test parse_json_safely with valid JSON array
|
||||
OUTPUT=$("${DRHARNESS}" -J '[1, 2, 3]')
|
||||
if [ "${OUTPUT}" != "valid" ]; then
|
||||
echo "FAIL: parse_json_safely - expected 'valid' for array, got '${OUTPUT}'"
|
||||
if ! echo "$OUTPUT" | jq -e 'type == "array" and length == 3' > /dev/null 2>&1; then
|
||||
echo "FAIL: parse_json_safely - expected valid array, got '${OUTPUT}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Test parse_json_safely with invalid JSON (missing quotes)
|
||||
OUTPUT=$("${DRHARNESS}" -J '{key: value}')
|
||||
if [ "${OUTPUT}" != "discarded" ]; then
|
||||
echo "FAIL: parse_json_safely - expected 'discarded' for invalid JSON, got '${OUTPUT}'"
|
||||
set +e
|
||||
OUTPUT=$("${DRHARNESS}" -J '{key: value}' 2>&1)
|
||||
EXIT_CODE=$?
|
||||
set -e
|
||||
if [ ${EXIT_CODE} -eq 0 ]; then
|
||||
echo "FAIL: parse_json_safely - expected non-zero exit code for invalid JSON, got 0"
|
||||
exit 1
|
||||
fi
|
||||
if ! echo "$OUTPUT" | grep -q "error: invalid JSON"; then
|
||||
echo "FAIL: parse_json_safely - expected 'error: invalid JSON' for invalid JSON, got '${OUTPUT}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Test parse_json_safely with invalid JSON (random text)
|
||||
OUTPUT=$("${DRHARNESS}" -J 'not json at all')
|
||||
if [ "${OUTPUT}" != "discarded" ]; then
|
||||
echo "FAIL: parse_json_safely - expected 'discarded' for random text, got '${OUTPUT}'"
|
||||
set +e
|
||||
OUTPUT=$("${DRHARNESS}" -J 'not json at all' 2>&1)
|
||||
EXIT_CODE=$?
|
||||
set -e
|
||||
if [ ${EXIT_CODE} -eq 0 ]; then
|
||||
echo "FAIL: parse_json_safely - expected non-zero exit code for invalid JSON, got 0"
|
||||
exit 1
|
||||
fi
|
||||
if ! echo "$OUTPUT" | grep -q "error: invalid JSON"; then
|
||||
echo "FAIL: parse_json_safely - expected 'error: invalid JSON' for random text, got '${OUTPUT}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Test parse_json_safely with empty string
|
||||
OUTPUT=$("${DRHARNESS}" -J '')
|
||||
if [ "${OUTPUT}" != "discarded" ]; then
|
||||
echo "FAIL: parse_json_safely - expected 'discarded' for empty string, got '${OUTPUT}'"
|
||||
set +e
|
||||
OUTPUT=$("${DRHARNESS}" -J '' 2>&1)
|
||||
EXIT_CODE=$?
|
||||
set -e
|
||||
if [ ${EXIT_CODE} -eq 0 ]; then
|
||||
echo "FAIL: parse_json_safely - expected non-zero exit code for empty string, got 0"
|
||||
exit 1
|
||||
fi
|
||||
if ! echo "$OUTPUT" | grep -q "error: invalid JSON"; then
|
||||
echo "FAIL: parse_json_safely - expected 'error: invalid JSON' for empty string, got '${OUTPUT}'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user