| ... | @@ -10,6 +10,7 @@ | ... | @@ -10,6 +10,7 @@ |
| 10 | #include "os.hpp" | 10 | #include "os.hpp" |
| 11 | | 11 | |
| 12 | #include <stdio.h> | 12 | #include <stdio.h> |
| | 13 | #include <stdarg.h> |
| 13 | | 14 | |
| 14 | struct TestSourceFile { | 15 | struct TestSourceFile { |
| 15 | const char *relative_path; | 16 | const char *relative_path; |
| ... | @@ -25,9 +26,10 @@ struct TestCase { | ... | @@ -25,9 +26,10 @@ struct TestCase { |
| 25 | ZigList<const char *> program_args; | 26 | ZigList<const char *> program_args; |
| 26 | }; | 27 | }; |
| 27 | | 28 | |
| 28 | ZigList<TestCase*> test_cases = {0}; | 29 | static ZigList<TestCase*> test_cases = {0}; |
| 29 | const char *tmp_source_path = ".tmp_source.zig"; | 30 | static const char *tmp_source_path = ".tmp_source.zig"; |
| 30 | const char *tmp_exe_path = "./.tmp_exe"; | 31 | static const char *tmp_exe_path = "./.tmp_exe"; |
| | 32 | static const char *zig_exe = "./zig"; |
| 31 | | 33 | |
| 32 | static void add_simple_case(const char *case_name, const char *source, const char *output) { | 34 | static void add_simple_case(const char *case_name, const char *source, const char *output) { |
| 33 | TestCase *test_case = allocate<TestCase>(1); | 35 | TestCase *test_case = allocate<TestCase>(1); |
| ... | @@ -45,7 +47,32 @@ static void add_simple_case(const char *case_name, const char *source, const cha | ... | @@ -45,7 +47,32 @@ static void add_simple_case(const char *case_name, const char *source, const cha |
| 45 | test_cases.append(test_case); | 47 | test_cases.append(test_case); |
| 46 | } | 48 | } |
| 47 | | 49 | |
| 48 | static void add_all_test_cases(void) { | 50 | static void add_compile_fail_case(const char *case_name, const char *source, int count, ...) { |
| | 51 | va_list ap; |
| | 52 | va_start(ap, count); |
| | 53 | |
| | 54 | TestCase *test_case = allocate<TestCase>(1); |
| | 55 | test_case->case_name = case_name; |
| | 56 | test_case->source = source; |
| | 57 | |
| | 58 | for (int i = 0; i < count; i += 1) { |
| | 59 | const char *arg = va_arg(ap, const char *); |
| | 60 | test_case->compile_errors.append(arg); |
| | 61 | } |
| | 62 | |
| | 63 | test_case->compiler_args.append("build"); |
| | 64 | test_case->compiler_args.append(tmp_source_path); |
| | 65 | test_case->compiler_args.append("--output"); |
| | 66 | test_case->compiler_args.append(tmp_exe_path); |
| | 67 | test_case->compiler_args.append("--release"); |
| | 68 | test_case->compiler_args.append("--strip"); |
| | 69 | |
| | 70 | test_cases.append(test_case); |
| | 71 | |
| | 72 | va_end(ap); |
| | 73 | } |
| | 74 | |
| | 75 | static void add_compiling_test_cases(void) { |
| 49 | add_simple_case("hello world with libc", R"SOURCE( | 76 | add_simple_case("hello world with libc", R"SOURCE( |
| 50 | #link("c") | 77 | #link("c") |
| 51 | extern { | 78 | extern { |
| ... | @@ -102,23 +129,102 @@ static void add_all_test_cases(void) { | ... | @@ -102,23 +129,102 @@ static void add_all_test_cases(void) { |
| 102 | )SOURCE", "OK\n"); | 129 | )SOURCE", "OK\n"); |
| 103 | } | 130 | } |
| 104 | | 131 | |
| | 132 | static void add_compile_failure_test_cases(void) { |
| | 133 | add_compile_fail_case("multiple function definitions", R"SOURCE( |
| | 134 | fn a() {} |
| | 135 | fn a() {} |
| | 136 | )SOURCE", 1, "Line 3, column 1: redefinition of 'a'"); |
| | 137 | |
| | 138 | add_compile_fail_case("bad directive", R"SOURCE( |
| | 139 | #bogus1("") |
| | 140 | extern { |
| | 141 | fn b(); |
| | 142 | } |
| | 143 | #bogus2("") |
| | 144 | fn a() {} |
| | 145 | )SOURCE", 2, "Line 2, column 1: invalid directive: 'bogus1'", |
| | 146 | "Line 6, column 1: invalid directive: 'bogus2'"); |
| | 147 | |
| | 148 | add_compile_fail_case("unreachable with return", R"SOURCE( |
| | 149 | fn a() -> unreachable {return;} |
| | 150 | )SOURCE", 1, "Line 2, column 24: return statement in function with unreachable return type"); |
| | 151 | |
| | 152 | add_compile_fail_case("control reaches end of non-void function", R"SOURCE( |
| | 153 | fn a() -> i32 {} |
| | 154 | )SOURCE", 1, "Line 2, column 1: control reaches end of non-void function"); |
| | 155 | |
| | 156 | add_compile_fail_case("undefined function call", R"SOURCE( |
| | 157 | fn a() { |
| | 158 | b(); |
| | 159 | } |
| | 160 | )SOURCE", 1, "Line 3, column 5: undefined function: 'b'"); |
| | 161 | |
| | 162 | add_compile_fail_case("wrong number of arguments", R"SOURCE( |
| | 163 | fn a() { |
| | 164 | b(1); |
| | 165 | } |
| | 166 | fn b(a: i32, b: i32, c: i32) { } |
| | 167 | )SOURCE", 1, "Line 3, column 5: wrong number of arguments. Expected 3, got 1."); |
| | 168 | |
| | 169 | add_compile_fail_case("invalid type", R"SOURCE( |
| | 170 | fn a() -> bogus {} |
| | 171 | )SOURCE", 1, "Line 2, column 11: invalid type name: 'bogus'"); |
| | 172 | |
| | 173 | add_compile_fail_case("pointer to unreachable", R"SOURCE( |
| | 174 | fn a() -> *mut unreachable {} |
| | 175 | )SOURCE", 1, "Line 2, column 11: pointer to unreachable not allowed"); |
| | 176 | |
| | 177 | add_compile_fail_case("unreachable code", R"SOURCE( |
| | 178 | fn a() { |
| | 179 | return; |
| | 180 | b(); |
| | 181 | } |
| | 182 | |
| | 183 | fn b() {} |
| | 184 | )SOURCE", 1, "Line 4, column 5: unreachable code"); |
| | 185 | } |
| | 186 | |
| | 187 | static void print_compiler_invokation(TestCase *test_case, Buf *zig_stderr) { |
| | 188 | printf("%s", zig_exe); |
| | 189 | for (int i = 0; i < test_case->compiler_args.length; i += 1) { |
| | 190 | printf(" %s", test_case->compiler_args.at(i)); |
| | 191 | } |
| | 192 | printf("\n"); |
| | 193 | printf("%s\n", buf_ptr(zig_stderr)); |
| | 194 | } |
| | 195 | |
| 105 | static void run_test(TestCase *test_case) { | 196 | static void run_test(TestCase *test_case) { |
| 106 | os_write_file(buf_create_from_str(tmp_source_path), buf_create_from_str(test_case->source)); | 197 | os_write_file(buf_create_from_str(tmp_source_path), buf_create_from_str(test_case->source)); |
| 107 | | 198 | |
| 108 | Buf zig_stderr = BUF_INIT; | 199 | Buf zig_stderr = BUF_INIT; |
| 109 | Buf zig_stdout = BUF_INIT; | 200 | Buf zig_stdout = BUF_INIT; |
| 110 | int return_code; | 201 | int return_code; |
| 111 | static const char *zig_exe = "./zig"; | | |
| 112 | os_exec_process(zig_exe, test_case->compiler_args, &return_code, &zig_stderr, &zig_stdout); | 202 | os_exec_process(zig_exe, test_case->compiler_args, &return_code, &zig_stderr, &zig_stdout); |
| 113 | | 203 | |
| | 204 | if (test_case->compile_errors.length) { |
| | 205 | if (return_code) { |
| | 206 | for (int i = 0; i < test_case->compile_errors.length; i += 1) { |
| | 207 | const char *err_text = test_case->compile_errors.at(i); |
| | 208 | if (!strstr(buf_ptr(&zig_stderr), err_text)) { |
| | 209 | printf("\n"); |
| | 210 | printf("========= Expected this compile error: =========\n"); |
| | 211 | printf("%s\n", err_text); |
| | 212 | printf("================================================\n"); |
| | 213 | print_compiler_invokation(test_case, &zig_stderr); |
| | 214 | exit(1); |
| | 215 | } |
| | 216 | } |
| | 217 | return; // success |
| | 218 | } else { |
| | 219 | printf("\nCompile failed with return code 0 (Expected failure):\n"); |
| | 220 | print_compiler_invokation(test_case, &zig_stderr); |
| | 221 | exit(1); |
| | 222 | } |
| | 223 | } |
| | 224 | |
| 114 | if (return_code != 0) { | 225 | if (return_code != 0) { |
| 115 | printf("\nCompile failed with return code %d:\n", return_code); | 226 | printf("\nCompile failed with return code %d:\n", return_code); |
| 116 | printf("%s", zig_exe); | 227 | print_compiler_invokation(test_case, &zig_stderr); |
| 117 | for (int i = 0; i < test_case->compiler_args.length; i += 1) { | | |
| 118 | printf(" %s", test_case->compiler_args.at(i)); | | |
| 119 | } | | |
| 120 | printf("\n"); | | |
| 121 | printf("%s\n", buf_ptr(&zig_stderr)); | | |
| 122 | exit(1); | 228 | exit(1); |
| 123 | } | 229 | } |
| 124 | | 230 | |
| ... | @@ -164,7 +270,8 @@ static void cleanup(void) { | ... | @@ -164,7 +270,8 @@ static void cleanup(void) { |
| 164 | } | 270 | } |
| 165 | | 271 | |
| 166 | int main(int argc, char **argv) { | 272 | int main(int argc, char **argv) { |
| 167 | add_all_test_cases(); | 273 | add_compiling_test_cases(); |
| | 274 | add_compile_failure_test_cases(); |
| 168 | run_all_tests(); | 275 | run_all_tests(); |
| 169 | cleanup(); | 276 | cleanup(); |
| 170 | } | 277 | } |