From 53a331bc8b2fc33bd2b7e25a23b4128f89ee0b52 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 7 Jun 2019 10:47:47 +0200 Subject: Add assignment, while, extern, function signature type, start on bytecode --- tests/main.c | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) (limited to 'tests/main.c') diff --git a/tests/main.c b/tests/main.c index 04f2343..3080807 100644 --- a/tests/main.c +++ b/tests/main.c @@ -7,6 +7,9 @@ #include #include +static int num_successful_tests = 0; +static int num_tests_run = 0; + #define REQUIRE_EQ_INT(expected, actual) do{\ if((expected) != (actual)) {\ fprintf(stderr, "%s:%d: Assert failed (%s vs %s).\nExpected %d, got %d.\n", __FILE__, __LINE__, #expected, #actual, (expected), (actual));\ @@ -42,7 +45,7 @@ static CHECK_RESULT int test_hash_map() { return 0; } -#define FAIL_TEST(name) do { fprintf(stderr, "Test failed: %s\n", (name)); exit(1); } while(0) +#define FAIL_TEST(name) do { fprintf(stderr, "Test failed: %s\n", (name)); return; } while(0) typedef struct { char *filepath; @@ -56,8 +59,8 @@ typedef struct { static void error_callback_assert(const char *err_msg, int err_msg_len, void *userdata) { ErrorExpectedData *expected_data; - expected_data = userdata; int expected_err_msg_len; + expected_data = userdata; expected_err_msg_len = strlen(expected_data->expected_error); if(expected_data->got_expected_error) { @@ -74,14 +77,14 @@ static void error_callback_assert(const char *err_msg, int err_msg_len, void *us expected_data->got_expected_error = bool_true; } - +#if 0 static void error_callback(const char *err_msg, int err_msg_len, void *userdata) { ErrorUnexpectedData *data; data = userdata; fprintf(stderr, "Test failed: %s with error: %.*s\n", data->filepath, err_msg_len, err_msg); exit(1); } - +#endif static char* get_full_path(const char *filepath) { #define PATH_LEN 4096 char *buf; @@ -120,33 +123,39 @@ static char* join_str(const char *str1, const char *str2, char delimiter) { static void test_load(const char *filepath) { amal_compiler compiler; amal_compiler_options options; + char *full_path; int result; amal_compiler_options_init(&options); + ++num_tests_run; + full_path = get_full_path(filepath); + /* options.error_callback = error_callback; ErrorUnexpectedData data; data.filepath = get_full_path(filepath); options.error_callback_userdata = &data; + */ result = amal_compiler_init(&compiler, &options); if(result != AMAL_COMPILER_OK) { - fprintf(stderr, "Failed to initialize compiler, error code: %d\n", result); - FAIL_TEST(data.filepath); + fprintf(stderr, "Failed to initialize compiler to test %s, error code: %d\n", full_path, result); + FAIL_TEST(full_path); } result = amal_compiler_load_file(&compiler, filepath); if(result != AMAL_COMPILER_OK) { - fprintf(stderr, "Failed to load file %s, result: %d\n", data.filepath, result); - FAIL_TEST(data.filepath); + fprintf(stderr, "Failed to load file %s, result: %d\n", full_path, result); + FAIL_TEST(full_path); } if(amal_compiler_deinit(&compiler) != 0) { - fprintf(stderr, "Failed to deinitialize compiler.\n"); - FAIL_TEST(data.filepath); + fprintf(stderr, "Failed to deinitialize compiler for test %s\n", full_path); + FAIL_TEST(full_path); } - fprintf(stderr, "Test succeeded: %s\n", data.filepath); - free(data.filepath); + fprintf(stderr, "Test succeeded as expected: %s\n", full_path); + ++num_successful_tests; + free(full_path); } static void test_load_error(const char *filepath, const char *expected_error) { @@ -155,6 +164,7 @@ static void test_load_error(const char *filepath, const char *expected_error) { int result; amal_compiler_options_init(&options); + ++num_tests_run; options.error_callback = error_callback_assert; ErrorExpectedData expected_data; expected_data.filepath = get_full_path(filepath); @@ -184,7 +194,8 @@ static void test_load_error(const char *filepath, const char *expected_error) { FAIL_TEST(expected_data.filepath); } - fprintf(stderr, "Test succeeded: %s\n", expected_data.filepath); + fprintf(stderr, "Test failed as expected: %s\n", expected_data.filepath); + ++num_successful_tests; free(expected_data.filepath); free(expected_data.expected_error); } @@ -205,11 +216,17 @@ int main(int argc, char **argv) { " pub const num = 45;\n" " ^\n"); test_load_error("tests/errors/closure_no_lhs.amal", - "1:1: error: Expected variable declaration, string, variable or function call\n" + "1:1: error: Expected string, variable, closure, struct, function call or import\n" "fn {}\n" "^\n"); + test_load_error("tests/errors/const_assign.amal", + "3:5: error: Can't assign to a const value\n" + " value = 34;\n" + " ^\n"); + fprintf(stderr, "##### %d/%d tests succeeded #####\n", num_successful_tests, num_tests_run); } else if(argc == 2) { test_load(argv[1]); + fprintf(stderr, "##### %d/%d tests succeeded #####\n", num_successful_tests, num_tests_run); } else { fprintf(stderr, "usage: test [test-file-path]\n"); fprintf(stderr, "examples:\n"); -- cgit v1.2.3