diff --git a/bin/build_decode_typed b/bin/build_decode_typed new file mode 100755 index 0000000..aa2ade7 --- /dev/null +++ b/bin/build_decode_typed @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +set -e + +REPO_DIR="$(cd $(dirname ${BASH_SOURCE[0]} )/..; pwd )" + +DECODE_TYPE_CRATE_DIR="${DECODE_TYPE_CRATE_DIR?You must set this environment variable with the path to the crate containing your serialized type. +E.g. \'framed-rs/test_type\'}" +DECODE_TYPE_NAME="${DECODE_TYPE_NAME?You must set this environment variable with the full name of your serialized type including its module path. +E.g. \'framed_test_type::Test\'}" + +TC=${TC:stable} + +echo "DECODE_TYPE_CRATE_DIR = '${DECODE_TYPE_CRATE_DIR}'" +echo "DECODE_TYPE_NAME = '${DECODE_TYPE_NAME}'" + +TYPE_CRATE_NAME="$(cargo read-manifest --manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" | jq '.name' -r)" + +TMP_DIR="${REPO_DIR}/target/decode_typed/tmp" + +mkdir -p "${TMP_DIR}"; +rm -f "${TMP_DIR}/"*; + +export FRAMED_DECODE_DYNAMIC_RS="${TMP_DIR}/dynamic_${DECODE_TYPE_NAME}.rs"; +cat > "${FRAMED_DECODE_DYNAMIC_RS}" < "${FRAMED_DECODE_DYNAMIC_RS}" < target/test_output; -cat target/test_output | hd; -cat target/test_output | ${REPO_DIR}/bin/decode_typed; -seq 5 | xargs -I% cat target/test_output | ${REPO_DIR}/bin/decode_typed; +TEST_TMP="${REPO_DIR}/target/decode_typed_test" + +mkdir -p "${TEST_TMP}"; + +source "${REPO_DIR}/bin/build_decode_typed"; + +TEST_OUT="${TEST_TMP}/test_out"; +cargo +${TC} run -p framed_test_type > "${TEST_OUT}"; + +TEST_FRAME="$(cat "${TEST_OUT}";)"; + +function test_case() { + flags="${flags?expected}"; + expect="${expect?expected}"; + name="${name?expected}"; + + IFS=' ' read -r -a flags_arr <<< "$flags"; + + out="$(${DECODE_PATH} "${flags_arr[@]}" 2>&1 < /dev/stdin)"; + if [[ "$out" == "$expect" ]]; then + echo "Test '${name}' OK"; + else + echo "Test '${name}' failed"; + exit 1; + fi +} + + +< /dev/null \ +name="Empty input" flags="" \ +expect="" test_case; + +( cat "${TEST_OUT}" ) | \ +name="1 frame --out-format=Debug" flags="" \ +expect="Test { + a: 1, + b: 2 +}" test_case; + +( cat "${TEST_OUT}" ) | \ +name="1 frame --out-format=Csv" flags="--out-format=Csv" \ +expect="a,b +1,2" test_case; + +( cat "${TEST_OUT}" ) | \ +name="1 frame --out-format=Json" flags="--out-format=Json" \ +expect="{\"a\":1,\"b\":2}" test_case; + +( cat "${TEST_OUT}" "${TEST_OUT}" ) | \ +name="2 frames --out-format=Csv" flags="--out-format=Csv" \ +expect="a,b +1,2 +1,2" test_case; + +( printf "\x1\x1\x1\x1\x1\x1\x1\x1\x1\x0" ) | \ +name="Checksum error" flags="" \ +expect="WARN: Corrupt frame, error: ChecksumError" test_case; + +( printf "\x0" ) | \ +name="Frame too short" flags="" \ +expect="WARN: Corrupt frame, error: EofDuringFrame" test_case; + +( cat "${TEST_OUT}"; printf "\x0"; cat "${TEST_OUT}" ) | \ +name="Output continues after corrupt frame" flags="--out-format=Csv" \ +expect="a,b +1,2 +WARN: Corrupt frame, error: EofDuringFrame +1,2" test_case;