aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-04-22 02:34:30 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commita76ba1b33e397638c4209dd77e6073e423ac07a8 (patch)
tree0ef62209546ba91d53a2fabb54f3b8ac9dcfafdf /tests
parent6a9466da5377d0bc73c7e5aa48deca3740d3de6f (diff)
Start on bytecode. Commit before os switch
Diffstat (limited to 'tests')
-rw-r--r--tests/main.c99
1 files changed, 69 insertions, 30 deletions
diff --git a/tests/main.c b/tests/main.c
index bc2fbb1..04f2343 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -50,6 +50,10 @@ typedef struct {
bool got_expected_error;
} ErrorExpectedData;
+typedef struct {
+ char *filepath;
+} ErrorUnexpectedData;
+
static void error_callback_assert(const char *err_msg, int err_msg_len, void *userdata) {
ErrorExpectedData *expected_data;
expected_data = userdata;
@@ -71,6 +75,13 @@ static void error_callback_assert(const char *err_msg, int err_msg_len, void *us
expected_data->got_expected_error = bool_true;
}
+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);
+}
+
static char* get_full_path(const char *filepath) {
#define PATH_LEN 4096
char *buf;
@@ -106,6 +117,38 @@ static char* join_str(const char *str1, const char *str2, char delimiter) {
return buf;
}
+static void test_load(const char *filepath) {
+ amal_compiler compiler;
+ amal_compiler_options options;
+ int result;
+
+ amal_compiler_options_init(&options);
+ 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);
+ }
+
+ 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);
+ }
+
+ if(amal_compiler_deinit(&compiler) != 0) {
+ fprintf(stderr, "Failed to deinitialize compiler.\n");
+ FAIL_TEST(data.filepath);
+ }
+
+ fprintf(stderr, "Test succeeded: %s\n", data.filepath);
+ free(data.filepath);
+}
+
static void test_load_error(const char *filepath, const char *expected_error) {
amal_compiler compiler;
amal_compiler_options options;
@@ -140,43 +183,39 @@ static void test_load_error(const char *filepath, const char *expected_error) {
fprintf(stderr, "Didn't get expected error message:\n%s\n", expected_error);
FAIL_TEST(expected_data.filepath);
}
+
+ fprintf(stderr, "Test succeeded: %s\n", expected_data.filepath);
free(expected_data.filepath);
free(expected_data.expected_error);
}
/* TODO: Restrict variables in global scope to const */
-int main() {
- /*amal_compiler compiler;
- int result;*/
-
+int main(int argc, char **argv) {
return_if_error(test_hash_map());
- /*
- result = amal_compiler_init(&compiler, NULL);
- if(result != AMAL_COMPILER_OK) {
- fprintf(stderr, "Failed to initialize compiler, error code: %d\n", result);
- return 1;
- }
-
- result = amal_compiler_load_file(&compiler, "tests/main.amal");
- if(result != AMAL_COMPILER_OK) {
- fprintf(stderr, "Failed to load file, error code: %d\n", result);
- return 1;
+ /* Run all tests */
+ if(argc == 1) {
+ test_load("tests/main.amal");
+ test_load_error("tests/errors/duplicate_declaration.amal",
+ "2:7: error: Variable with the name main was declared twice in the same scope\n"
+ "const main = fn {}\n"
+ " ^\n");
+ test_load_error("tests/errors/pub_in_closure.amal",
+ "2:5: error: Only declarations in structs can be public\n"
+ " 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"
+ "fn {}\n"
+ "^\n");
+ } else if(argc == 2) {
+ test_load(argv[1]);
+ } else {
+ fprintf(stderr, "usage: test [test-file-path]\n");
+ fprintf(stderr, "examples:\n");
+ fprintf(stderr, " test\n");
+ fprintf(stderr, " test tests/main.amal\n");
+ exit(1);
}
-
- return amal_compiler_deinit(&compiler);
- */
- test_load_error("tests/errors/duplicate_declaration.amal",
- "2:7: error: Variable with the name main was declared twice in the same scope\n"
- "const main = fn {}\n"
- " ^\n");
- test_load_error("tests/errors/pub_in_closure.amal",
- "2:5: error: Only declarations in structs can be public\n"
- " 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"
- "fn {}\n"
- "^\n");
return 0;
}