#!/usr/bin/env bash # SYNTAX: # catch STDOUT_VARIABLE STDERR_VARIABLE COMMAND [ARG1[ ARG2[ ...[ ARGN]]]] # https://stackoverflow.com/a/59592881 catch() { { IFS=$'\n' read -r -d '' "${1}"; IFS=$'\n' read -r -d '' "${2}"; (IFS=$'\n' read -r -d '' _ERRNO_; return ${_ERRNO_}); } < <((printf '\0%s\0%d\0' "$(((({ shift 2; "${@}"; echo "${?}" 1>&3-; } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) } tmp_out="$(mktemp)" if [ -f "${0%_test.c}.c" ]; then SRC="${0%_test.c}.c" else SRC="$($(type -P basename) -- "${0%_test.c}.c")" fi grep -A9999 -- "[S]TART_OF_C_FILE" "${0}" | cc -Wall -Wextra -Werror -o "${tmp_out}" -xc "${SRC}" - if [ ! "${?}" = "0" ] || [ ! -f "${tmp_out}" ]; then printf "\e[101;37m%s\e[m\n" "Could not compile" exit 1 fi catch stout sterr "${tmp_out}" printf "%s\n" "Testing ${SRC}" diff_out="$(diff --expand-tabs --left-column --width="60" --side-by-side --label="Expected Output" --label=" Test Output" -- <(printf '%s\n' "${sterr}") <(printf '%s\n' "${stout}") && printf '%s\n' "0" || printf '%s\n' "1")" if [ "$(printf '%s' "${diff_out}" | tail -1)" = "0" ]; then printf "\e[102;30m%s\e[m\n" "All tests passed" exit_status="0" else printf "Expected Output --> Test Output\n" printf '%s' "${diff_out}" | head -n -1 printf "\e[101;37m%s\e[m\n" "At least one test failed" exit_status="1" fi rm -f -- "${tmp_out}" exit -- "${exit_status}" // START_OF_C_FILE #include #include static int test_counter = 0; static int expect_counter = 0; #define test(func, ...) \ do { \ int old_stderr_fd = dup(2); \ dup2(1, 2); \ fprintf(stdout, "Case %d:", test_counter++); \ fflush(stdout); \ func(__VA_ARGS__); \ fflush(stderr); \ fflush(stdout); \ dup2(old_stderr_fd, 2); \ } while(0) #define expect(func, ...) \ do { \ int old_stdout_fd = dup(1); \ dup2(2, 1); \ fprintf(stderr, "Case %d:", expect_counter++); \ fflush(stderr); \ func(__VA_ARGS__); \ fflush(stderr); \ fflush(stdout); \ dup2(old_stdout_fd, 1); \ } while(0) // END_TEMPLATE int main(){ // test (printf, "\n"); // expect(printf, "\n"); return 0; }