#!/bin/sh 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" # Misc extract_comment() { tag="$1" comment="# $tag:" grep "$comment" "$TEST_FILE" | sed -e "s/$comment //" } cleanup() { rm -f "$PROGRAM*" } # UI COLOR_RED=1 COLOR_GREEN=2 COLOR_YELLOW=3 COLOR_CYAN=6 COLOR_GRAY=7 colored() { text="$1" color=$(tput setaf "$2") reset=$(tput sgr0) if [ -t 1 ]; then if tput setaf 1 > /dev/null 2>&1; then printf "%s%s%s" "$color" "$text" "$reset" return fi fi printf "%s" "$text" } print_test_description() { colored "$TEST_FILE: " $COLOR_CYAN printf "\n" colored "$(extract_comment spec)" $COLOR_GRAY printf "\n\n" } print_passed() { context="$1" printf "%s: " "$context" colored "passed" $COLOR_GREEN echo } print_failed() { context="$1" printf "%s: " "$context" colored "failed" $COLOR_RED echo } print_skiped() { context="$1" printf "%s: " "$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 [ "$(wc -l < "$expected_file")" = "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 [ "$(wc -c < "$actual_file")" = "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 } # Tests 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=$(wc -l < "$TOKENS_EXPECT_OUTPUT_FILE") $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