COMPILER_PATH="../../olang" TEST_FILE="$1" PROGRAM="$TEST_FILE.test" COMPILER_OUTPUT_FILE="$TEST_FILE.test.compiler_output" COMPILER_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_compiler_output" AST_OUTPUT_FILE="$TEST_FILE.test.ast_output" AST_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_ast_output" TOKENS_OUTPUT_FILE="$TEST_FILE.test.tokens_output" TOKENS_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_tokens_output" TOKENS_OUTPUT_FILE="$TEST_FILE.test.tokens_output" TOKENS_EXPECT_OUTPUT_FILE="$TEST_FILE.test.expected_tokens_output" PROGRAM_OUTPUT="$TEST_FILE.test.program_output" extract_comment() { tag="$1" comment="# $tag: " cat $TEST_FILE | grep "$comment" | sed -e "s/$comment//" } # UI COLOR_RED=1 COLOR_GREEN=2 COLOR_YELLOW=3 COLOR_BLUE=4 COLOR_CYAN=6 COLOR_GRAY=7 colored() { text="$1" color="$2" if [ -t 1 ]; then if tput setaf 1 &> /dev/null; then printf "$(tput setaf $color)$text$(tput sgr0)" return fi fi printf "$text" } print_test_description() { colored "$TEST_FILE: \n" $COLOR_CYAN colored "$(extract_comment spec)\n\n" $COLOR_GRAY } print_passed() { context="$1" printf "$context: " colored "passed" $COLOR_GREEN echo } print_failed() { context="$1" printf "$context: " colored "failed" $COLOR_RED echo } print_skiped() { context="$1" printf "$context: " colored "not set" $COLOR_GRAY echo } # Assertions expect_exit_code() { context="$1" expected="$2" actual="$3" if [ -z "$expected" ]; then print_skiped "$context" echo "no expected exit code set, it exited with \"$actual\"." else if [ "$expected" = "$actual" ]; then print_passed "$context" else print_failed "$context" colored "Expected exit code: " $COLOR_YELLOW colored "$expected" $COLOR_GRAY colored ", actual: " $COLOR_YELLOW colored "$actual\n" $COLOR_GRAY exit 1 fi fi } expect_output() { context="$1" expected_file="$2" actual_file="$3" if [ "$(cat $expected_file | wc -l)" = "0" ]; then print_skiped "$context" return fi if cmp -s "$expected_file" "$actual_file"; then print_passed "$context" else if [ "$(cat "$expected_file")" = "%empty%" ]; then if [ "$(cat $actual_file | wc -c)" = "0" ]; then print_passed "$context" return fi fi print_failed "$context" colored "$context not match:\n" $COLOR_YELLOW diff "$actual_file" "$expected_file" -u --color exit 1 fi } cleanup() { rm -f $PROGRAM* } test_compiler() { $COMPILER_PATH $TEST_FILE -o $PROGRAM > "$COMPILER_OUTPUT_FILE" 2>&1 EXIT=$? EXPECTED_EXIT=$(extract_comment compiler_exit) expect_exit_code compiler_exit "$EXPECTED_EXIT" "$EXIT" extract_comment compiler_output > $COMPILER_EXPECT_OUTPUT_FILE expect_output compiler_output "$COMPILER_EXPECT_OUTPUT_FILE" "$COMPILER_OUTPUT_FILE" if [ "$EXIT" = "1" ]; then echo "program execution skiped since the compiler failed as expected." cleanup exit 0 fi } test_ast() { $COMPILER_PATH $TEST_FILE --dump-ast > "$AST_OUTPUT_FILE" 2>&1 extract_comment ast > $AST_EXPECT_OUTPUT_FILE expect_output "ast" "$AST_EXPECT_OUTPUT_FILE" "$AST_OUTPUT_FILE" } test_tokens() { extract_comment tokens > $TOKENS_EXPECT_OUTPUT_FILE TOKEN_LINES=$(cat $TOKENS_EXPECT_OUTPUT_FILE | wc -l) $COMPILER_PATH $TEST_FILE --dump-tokens | head -n $TOKEN_LINES > "$TOKENS_OUTPUT_FILE" 2>&1 expect_output tokens $TOKENS_OUTPUT_FILE $TOKENS_EXPECT_OUTPUT_FILE print_passed tokens } test_program() { ./$PROGRAM EXIT=$? EXPECTED_EXIT=$(extract_comment program_exit) expect_exit_code "program_exit" "$EXPECTED_EXIT" "$EXIT" } main() { print_test_description test_compiler test_program test_tokens test_ast cleanup } main