Files
drharness/tests/test_json_to_field.sh
darthrubik 4cc328a6fc feat: add json_to_field<T>() helper with if constexpr dispatch (issue #67)
- Add json_to_field<T>() helper in detail namespace for compile-time dispatch
- Uses if constexpr(has_json_to_v<T>) to choose between recursive json_to<T>()
  for custom types and direct .get_to() for primitives
- Custom types: calls json_to<T>() and prepends field name to error messages
- Primitive types: uses .get_to() directly (no recursion)
- Add MixedFieldsStruct test type with both primitive and custom type fields
- Add test_json_to_field.sh test suite to verify correct dispatch and error messages
2026-05-28 08:56:28 -05:00

57 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# test_json_to_field.sh - Tests for json_to_field<T>() helper with if constexpr dispatch
# Test MixedFieldsStruct with all fields (primitives + custom type)
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct '{"name": "test", "count": 42, "func": {"name": "test_func", "arguments": "{}"}}')
if [ "${OUTPUT}" != "success" ]; then
echo "FAIL: MixedFieldsStruct with all fields - expected 'success', got '${OUTPUT}'"
exit 1
fi
# Test MixedFieldsStruct with missing 'name' field (primitive)
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct '{"count": 42, "func": {"name": "test_func", "arguments": "{}"}}')
if [ "${OUTPUT}" != "error: missing required field: name" ]; then
echo "FAIL: MixedFieldsStruct missing name - expected 'error: missing required field: name', got '${OUTPUT}'"
exit 1
fi
# Test MixedFieldsStruct with missing 'count' field (primitive)
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct '{"name": "test", "func": {"name": "test_func", "arguments": "{}"}}')
if [ "${OUTPUT}" != "error: missing required field: count" ]; then
echo "FAIL: MixedFieldsStruct missing count - expected 'error: missing required field: count', got '${OUTPUT}'"
exit 1
fi
# Test MixedFieldsStruct with missing 'func' field (custom type)
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct '{"name": "test", "count": 42}')
if [ "${OUTPUT}" != "error: missing required field: func" ]; then
echo "FAIL: MixedFieldsStruct missing func - expected 'error: missing required field: func', got '${OUTPUT}'"
exit 1
fi
# Test MixedFieldsStruct with invalid nested FunctionCall (missing 'name')
# This verifies that error messages include field name context for nested type failures
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct '{"name": "test", "count": 42, "func": {"arguments": "{}"}}')
if [ "${OUTPUT}" != "error: field 'func': missing required field: name" ]; then
echo "FAIL: MixedFieldsStruct invalid nested func - expected 'error: field '\''func'\'': missing required field: name', got '${OUTPUT}'"
exit 1
fi
# Test MixedFieldsStruct with invalid nested FunctionCall (missing 'arguments')
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct '{"name": "test", "count": 42, "func": {"name": "test_func"}}')
if [ "${OUTPUT}" != "error: field 'func': missing required field: arguments" ]; then
echo "FAIL: MixedFieldsStruct invalid nested func missing args - expected 'error: field '\''func'\'': missing required field: arguments', got '${OUTPUT}'"
exit 1
fi
# Test MixedFieldsStruct with invalid JSON
set +e
OUTPUT=$("${DRHARNESS}" -R MixedFieldsStruct 'not valid json' 2>&1) || true
set -e
if [ "${OUTPUT}" != "error: invalid JSON" ]; then
echo "FAIL: MixedFieldsStruct invalid JSON - expected 'error: invalid JSON', got '${OUTPUT}'"
exit 1
fi
echo "All json_to_field tests passed!"