| ... | ... | @@ -21,9 +21,10 @@ const ErrorMsg = struct { |
| 21 | 21 | }; |
| 22 | 22 | |
| 23 | 23 | pub const TestContext = struct { |
| 24 | | zir_cases: std.ArrayList(Case), |
| 24 | /// TODO: find a way to treat cases as individual tests (shouldn't show "1 test passed" if there are 200 cases) |
| 25 | cases: std.ArrayList(Case), |
| 25 | 26 | |
| 26 | | pub const ZIRUpdate = struct { |
| 27 | pub const Update = struct { |
| 27 | 28 | /// The input to the current update. We simulate an incremental update |
| 28 | 29 | /// with the file's contents changed to this value each update. |
| 29 | 30 | /// |
| ... | ... | @@ -33,35 +34,43 @@ pub const TestContext = struct { |
| 33 | 34 | /// effects of the incremental compilation. |
| 34 | 35 | src: [:0]const u8, |
| 35 | 36 | case: union(enum) { |
| 36 | | /// A transformation update transforms the input ZIR and tests against |
| 37 | /// A transformation update transforms the input and tests against |
| 37 | 38 | /// the expected output ZIR. |
| 38 | 39 | Transformation: [:0]const u8, |
| 39 | 40 | /// An error update attempts to compile bad code, and ensures that it |
| 40 | 41 | /// fails to compile, and for the expected reasons. |
| 41 | 42 | /// A slice containing the expected errors *in sequential order*. |
| 42 | 43 | Error: []const ErrorMsg, |
| 43 | | /// An execution update compiles and runs the input ZIR, feeding in |
| 44 | | /// provided input and ensuring that the stdout match what is expected. |
| 44 | /// An execution update compiles and runs the input, testing the |
| 45 | /// stdout against the expected results |
| 46 | /// This is a slice containing the expected message. |
| 45 | 47 | Execution: []const u8, |
| 46 | 48 | }, |
| 47 | 49 | }; |
| 48 | 50 | |
| 49 | | /// A Case consists of a set of *updates*. A update can transform ZIR, |
| 50 | | /// compile it, ensure that compilation fails, and more. The same Module is |
| 51 | | /// used for each update, so each update's source is treated as a single file |
| 52 | | /// being updated by the test harness and incrementally compiled. |
| 51 | pub const TestType = enum { |
| 52 | Zig, |
| 53 | ZIR, |
| 54 | }; |
| 55 | |
| 56 | /// A Case consists of a set of *updates*. The same Module is used for each |
| 57 | /// update, so each update's source is treated as a single file being |
| 58 | /// updated by the test harness and incrementally compiled. |
| 53 | 59 | pub const Case = struct { |
| 60 | /// The name of the test case. This is shown if a test fails, and |
| 61 | /// otherwise ignored. |
| 54 | 62 | name: []const u8, |
| 55 | | /// The platform the ZIR targets. For non-native platforms, an emulator |
| 63 | /// The platform the test targets. For non-native platforms, an emulator |
| 56 | 64 | /// such as QEMU is required for tests to complete. |
| 57 | 65 | target: std.zig.CrossTarget, |
| 58 | | updates: std.ArrayList(ZIRUpdate), |
| 66 | /// In order to be able to run e.g. Execution updates, this must be set |
| 67 | /// to Executable. |
| 59 | 68 | output_mode: std.builtin.OutputMode, |
| 60 | | /// Either ".zir" or ".zig" |
| 61 | | extension: [4]u8, |
| 69 | updates: std.ArrayList(Update), |
| 70 | extension: TestType, |
| 62 | 71 | |
| 63 | | /// Adds a subcase in which the module is updated with new ZIR, and the |
| 64 | | /// resulting ZIR is validated. |
| 72 | /// Adds a subcase in which the module is updated with `src`, and the |
| 73 | /// resulting ZIR is validated against `result`. |
| 65 | 74 | pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void { |
| 66 | 75 | self.updates.append(.{ |
| 67 | 76 | .src = src, |
| ... | ... | @@ -69,6 +78,8 @@ pub const TestContext = struct { |
| 69 | 78 | }) catch unreachable; |
| 70 | 79 | } |
| 71 | 80 | |
| 81 | /// Adds a subcase in which the module is updated with `src`, compiled, |
| 82 | /// run, and the output is tested against `result`. |
| 72 | 83 | pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void { |
| 73 | 84 | self.updates.append(.{ |
| 74 | 85 | .src = src, |
| ... | ... | @@ -76,31 +87,31 @@ pub const TestContext = struct { |
| 76 | 87 | }) catch unreachable; |
| 77 | 88 | } |
| 78 | 89 | |
| 79 | | /// Adds a subcase in which the module is updated with invalid ZIR, and |
| 80 | | /// ensures that compilation fails for the expected reasons. |
| 81 | | /// |
| 82 | | /// Errors must be specified in sequential order. |
| 90 | /// Adds a subcase in which the module is updated with `src`, which |
| 91 | /// should contain invalid input, and ensures that compilation fails |
| 92 | /// for the expected reasons, given in sequential order in `errors` in |
| 93 | /// the form `:line:column: error: message`. |
| 83 | 94 | pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void { |
| 84 | 95 | var array = self.updates.allocator.alloc(ErrorMsg, errors.len) catch unreachable; |
| 85 | 96 | for (errors) |e, i| { |
| 86 | 97 | if (e[0] != ':') { |
| 87 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 98 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 88 | 99 | } |
| 89 | 100 | var cur = e[1..]; |
| 90 | 101 | var line_index = std.mem.indexOf(u8, cur, ":"); |
| 91 | 102 | if (line_index == null) { |
| 92 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 103 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 93 | 104 | } |
| 94 | 105 | const line = std.fmt.parseInt(u32, cur[0..line_index.?], 10) catch @panic("Unable to parse line number"); |
| 95 | 106 | cur = cur[line_index.? + 1 ..]; |
| 96 | 107 | const column_index = std.mem.indexOf(u8, cur, ":"); |
| 97 | 108 | if (column_index == null) { |
| 98 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 109 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 99 | 110 | } |
| 100 | 111 | const column = std.fmt.parseInt(u32, cur[0..column_index.?], 10) catch @panic("Unable to parse column number"); |
| 101 | 112 | cur = cur[column_index.? + 2 ..]; |
| 102 | 113 | if (!std.mem.eql(u8, cur[0..7], "error: ")) { |
| 103 | | std.debug.panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n", .{}); |
| 114 | @panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n"); |
| 104 | 115 | } |
| 105 | 116 | const msg = cur[7..]; |
| 106 | 117 | |
| ... | ... | @@ -116,123 +127,245 @@ pub const TestContext = struct { |
| 116 | 127 | } |
| 117 | 128 | self.updates.append(.{ .src = src, .case = .{ .Error = array } }) catch unreachable; |
| 118 | 129 | } |
| 130 | |
| 131 | /// Adds a subcase in which the module is updated with `src`, and |
| 132 | /// asserts that it compiles without issue |
| 133 | pub fn compiles(self: *Case, src: [:0]const u8) void { |
| 134 | self.addError(src, &[_][]const u8{}); |
| 135 | } |
| 119 | 136 | }; |
| 120 | 137 | |
| 121 | | pub fn addExeZIR( |
| 138 | pub fn addExe( |
| 122 | 139 | ctx: *TestContext, |
| 123 | 140 | name: []const u8, |
| 124 | 141 | target: std.zig.CrossTarget, |
| 142 | T: TestType, |
| 125 | 143 | ) *Case { |
| 126 | | const case = Case{ |
| 144 | ctx.cases.append(Case{ |
| 127 | 145 | .name = name, |
| 128 | 146 | .target = target, |
| 129 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 147 | .updates = std.ArrayList(Update).init(ctx.cases.allocator), |
| 130 | 148 | .output_mode = .Exe, |
| 131 | | .extension = ".zir".*, |
| 132 | | }; |
| 133 | | ctx.zir_cases.append(case) catch unreachable; |
| 134 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 149 | .extension = T, |
| 150 | }) catch unreachable; |
| 151 | return &ctx.cases.items[ctx.cases.items.len - 1]; |
| 135 | 152 | } |
| 136 | 153 | |
| 137 | | pub fn addObjZIR( |
| 154 | /// Adds a test case for Zig input, producing an executable |
| 155 | pub fn exe(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { |
| 156 | return ctx.addExe(name, target, .Zig); |
| 157 | } |
| 158 | |
| 159 | /// Adds a test case for ZIR input, producing an executable |
| 160 | pub fn exeZIR(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { |
| 161 | return ctx.addExe(name, target, .ZIR); |
| 162 | } |
| 163 | |
| 164 | pub fn addObj( |
| 138 | 165 | ctx: *TestContext, |
| 139 | 166 | name: []const u8, |
| 140 | 167 | target: std.zig.CrossTarget, |
| 168 | T: TestType, |
| 141 | 169 | ) *Case { |
| 142 | | const case = Case{ |
| 170 | ctx.cases.append(Case{ |
| 143 | 171 | .name = name, |
| 144 | 172 | .target = target, |
| 145 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 173 | .updates = std.ArrayList(Update).init(ctx.cases.allocator), |
| 146 | 174 | .output_mode = .Obj, |
| 147 | | .extension = ".zir".*, |
| 148 | | }; |
| 149 | | ctx.zir_cases.append(case) catch unreachable; |
| 150 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 175 | .extension = T, |
| 176 | }) catch unreachable; |
| 177 | return &ctx.cases.items[ctx.cases.items.len - 1]; |
| 151 | 178 | } |
| 152 | 179 | |
| 153 | | pub fn addExe( |
| 180 | /// Adds a test case for Zig input, producing an object file |
| 181 | pub fn obj(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { |
| 182 | return ctx.addObj(name, target, .Zig); |
| 183 | } |
| 184 | |
| 185 | /// Adds a test case for ZIR input, producing an object file |
| 186 | pub fn objZIR(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { |
| 187 | return ctx.addObj(name, target, .ZIR); |
| 188 | } |
| 189 | |
| 190 | pub fn addCompareOutput( |
| 154 | 191 | ctx: *TestContext, |
| 155 | 192 | name: []const u8, |
| 156 | | target: std.zig.CrossTarget, |
| 157 | | ) *Case { |
| 158 | | const case = Case{ |
| 159 | | .name = name, |
| 160 | | .target = target, |
| 161 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 162 | | .output_mode = .Exe, |
| 163 | | .extension = ".zig".*, |
| 164 | | }; |
| 165 | | ctx.zir_cases.append(case) catch unreachable; |
| 166 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 193 | T: TestType, |
| 194 | src: [:0]const u8, |
| 195 | expected_stdout: []const u8, |
| 196 | ) void { |
| 197 | ctx.addExe(name, .{}, T).addCompareOutput(src, expected_stdout); |
| 167 | 198 | } |
| 168 | 199 | |
| 169 | | pub fn addObj( |
| 200 | /// Adds a test case that compiles the Zig source given in `src`, executes |
| 201 | /// it, runs it, and tests the output against `expected_stdout` |
| 202 | pub fn compareOutput( |
| 170 | 203 | ctx: *TestContext, |
| 171 | 204 | name: []const u8, |
| 172 | | target: std.zig.CrossTarget, |
| 173 | | ) *Case { |
| 174 | | const case = Case{ |
| 175 | | .name = name, |
| 176 | | .target = target, |
| 177 | | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), |
| 178 | | .output_mode = .Obj, |
| 179 | | .extension = ".zig".*, |
| 180 | | }; |
| 181 | | ctx.zir_cases.append(case) catch unreachable; |
| 182 | | return &ctx.zir_cases.items[ctx.zir_cases.items.len - 1]; |
| 205 | src: [:0]const u8, |
| 206 | expected_stdout: []const u8, |
| 207 | ) void { |
| 208 | return ctx.addCompareOutput(name, .Zig, src, expected_stdout); |
| 183 | 209 | } |
| 184 | 210 | |
| 185 | | pub fn addZIRCompareOutput( |
| 211 | /// Adds a test case that compiles the ZIR source given in `src`, executes |
| 212 | /// it, runs it, and tests the output against `expected_stdout` |
| 213 | pub fn compareOutputZIR( |
| 186 | 214 | ctx: *TestContext, |
| 187 | 215 | name: []const u8, |
| 188 | 216 | src: [:0]const u8, |
| 189 | 217 | expected_stdout: []const u8, |
| 190 | 218 | ) void { |
| 191 | | var c = ctx.addExeZIR(name, .{}); |
| 192 | | c.addCompareOutput(src, expected_stdout); |
| 219 | ctx.addCompareOutput(name, .ZIR, src, expected_stdout); |
| 193 | 220 | } |
| 194 | 221 | |
| 195 | | pub fn addCompareOutput( |
| 222 | pub fn addTransform( |
| 196 | 223 | ctx: *TestContext, |
| 197 | 224 | name: []const u8, |
| 225 | target: std.zig.CrossTarget, |
| 226 | T: TestType, |
| 198 | 227 | src: [:0]const u8, |
| 199 | | expected_stdout: []const u8, |
| 228 | result: [:0]const u8, |
| 229 | ) void { |
| 230 | ctx.addObj(name, target, T).addTransform(src, result); |
| 231 | } |
| 232 | |
| 233 | /// Adds a test case that compiles the Zig given in `src` to ZIR and tests |
| 234 | /// the ZIR against `result` |
| 235 | pub fn transform( |
| 236 | ctx: *TestContext, |
| 237 | name: []const u8, |
| 238 | target: std.zig.CrossTarget, |
| 239 | src: [:0]const u8, |
| 240 | result: [:0]const u8, |
| 200 | 241 | ) void { |
| 201 | | var c = ctx.addExe(name, .{}); |
| 202 | | c.addCompareOutput(src, expected_stdout); |
| 242 | ctx.addTransform(name, target, .Zig, src, result); |
| 203 | 243 | } |
| 204 | 244 | |
| 205 | | pub fn addZIRTransform( |
| 245 | /// Adds a test case that cleans up the ZIR source given in `src`, and |
| 246 | /// tests the resulting ZIR against `result` |
| 247 | pub fn transformZIR( |
| 206 | 248 | ctx: *TestContext, |
| 207 | 249 | name: []const u8, |
| 208 | 250 | target: std.zig.CrossTarget, |
| 209 | 251 | src: [:0]const u8, |
| 210 | 252 | result: [:0]const u8, |
| 211 | 253 | ) void { |
| 212 | | var c = ctx.addObjZIR(name, target); |
| 213 | | c.addTransform(src, result); |
| 254 | ctx.addTransform(name, target, .ZIR, src, result); |
| 255 | } |
| 256 | |
| 257 | pub fn addError( |
| 258 | ctx: *TestContext, |
| 259 | name: []const u8, |
| 260 | target: std.zig.CrossTarget, |
| 261 | T: TestType, |
| 262 | src: [:0]const u8, |
| 263 | expected_errors: []const []const u8, |
| 264 | ) void { |
| 265 | ctx.addObj(name, target, T).addError(src, expected_errors); |
| 266 | } |
| 267 | |
| 268 | /// Adds a test case that ensures that the Zig given in `src` fails to |
| 269 | /// compile for the expected reasons, given in sequential order in |
| 270 | /// `expected_errors` in the form `:line:column: error: message`. |
| 271 | pub fn compileError( |
| 272 | ctx: *TestContext, |
| 273 | name: []const u8, |
| 274 | target: std.zig.CrossTarget, |
| 275 | src: [:0]const u8, |
| 276 | expected_errors: []const []const u8, |
| 277 | ) void { |
| 278 | ctx.addError(name, target, .Zig, src, expected_errors); |
| 279 | } |
| 280 | |
| 281 | /// Adds a test case that ensures that the ZIR given in `src` fails to |
| 282 | /// compile for the expected reasons, given in sequential order in |
| 283 | /// `expected_errors` in the form `:line:column: error: message`. |
| 284 | pub fn compileErrorZIR( |
| 285 | ctx: *TestContext, |
| 286 | name: []const u8, |
| 287 | target: std.zig.CrossTarget, |
| 288 | src: [:0]const u8, |
| 289 | expected_errors: []const []const u8, |
| 290 | ) void { |
| 291 | ctx.addError(name, target, .ZIR, src, expected_errors); |
| 292 | } |
| 293 | |
| 294 | pub fn addCompiles( |
| 295 | ctx: *TestContext, |
| 296 | name: []const u8, |
| 297 | target: std.zig.CrossTarget, |
| 298 | T: TestType, |
| 299 | src: [:0]const u8, |
| 300 | ) void { |
| 301 | ctx.addObj(name, target, T).compiles(src); |
| 302 | } |
| 303 | |
| 304 | /// Adds a test case that asserts that the Zig given in `src` compiles |
| 305 | /// without any errors. |
| 306 | pub fn compiles( |
| 307 | ctx: *TestContext, |
| 308 | name: []const u8, |
| 309 | target: std.zig.CrossTarget, |
| 310 | src: [:0]const u8, |
| 311 | ) void { |
| 312 | ctx.addCompiles(name, target, .Zig, src); |
| 313 | } |
| 314 | |
| 315 | /// Adds a test case that asserts that the ZIR given in `src` compiles |
| 316 | /// without any errors. |
| 317 | pub fn compilesZIR( |
| 318 | ctx: *TestContext, |
| 319 | name: []const u8, |
| 320 | target: std.zig.CrossTarget, |
| 321 | src: [:0]const u8, |
| 322 | ) void { |
| 323 | ctx.addCompiles(name, target, .ZIR, src); |
| 214 | 324 | } |
| 215 | 325 | |
| 216 | | pub fn addZIRError( |
| 326 | /// Adds a test case that first ensures that the Zig given in `src` fails |
| 327 | /// to compile for the reasons given in sequential order in |
| 328 | /// `expected_errors` in the form `:line:column: error: message`, then |
| 329 | /// asserts that fixing the source (updating with `fixed_src`) isn't broken |
| 330 | /// by incremental compilation. |
| 331 | pub fn incrementalFailure( |
| 217 | 332 | ctx: *TestContext, |
| 218 | 333 | name: []const u8, |
| 219 | 334 | target: std.zig.CrossTarget, |
| 220 | 335 | src: [:0]const u8, |
| 221 | 336 | expected_errors: []const []const u8, |
| 337 | fixed_src: [:0]const u8, |
| 222 | 338 | ) void { |
| 223 | | var c = ctx.addObjZIR(name, target); |
| 224 | | c.addError(src, expected_errors); |
| 339 | var case = ctx.addObj(name, target, .Zig); |
| 340 | case.addError(src, expected_errors); |
| 341 | case.compiles(fixed_src); |
| 342 | } |
| 343 | |
| 344 | /// Adds a test case that first ensures that the ZIR given in `src` fails |
| 345 | /// to compile for the reasons given in sequential order in |
| 346 | /// `expected_errors` in the form `:line:column: error: message`, then |
| 347 | /// asserts that fixing the source (updating with `fixed_src`) isn't broken |
| 348 | /// by incremental compilation. |
| 349 | pub fn incrementalFailureZIR( |
| 350 | ctx: *TestContext, |
| 351 | name: []const u8, |
| 352 | target: std.zig.CrossTarget, |
| 353 | src: [:0]const u8, |
| 354 | expected_errors: []const []const u8, |
| 355 | fixed_src: [:0]const u8, |
| 356 | ) void { |
| 357 | var case = ctx.addObj(name, target, .ZIR); |
| 358 | case.addError(src, expected_errors); |
| 359 | case.compiles(fixed_src); |
| 225 | 360 | } |
| 226 | 361 | |
| 227 | 362 | fn init() TestContext { |
| 228 | 363 | const allocator = std.heap.page_allocator; |
| 229 | | return .{ |
| 230 | | .zir_cases = std.ArrayList(Case).init(allocator), |
| 231 | | }; |
| 364 | return .{ .cases = std.ArrayList(Case).init(allocator) }; |
| 232 | 365 | } |
| 233 | 366 | |
| 234 | 367 | fn deinit(self: *TestContext) void { |
| 235 | | for (self.zir_cases.items) |c| { |
| 368 | for (self.cases.items) |c| { |
| 236 | 369 | for (c.updates.items) |u| { |
| 237 | 370 | if (u.case == .Error) { |
| 238 | 371 | c.updates.allocator.free(u.case.Error); |
| ... | ... | @@ -240,26 +373,28 @@ pub const TestContext = struct { |
| 240 | 373 | } |
| 241 | 374 | c.updates.deinit(); |
| 242 | 375 | } |
| 243 | | self.zir_cases.deinit(); |
| 376 | self.cases.deinit(); |
| 244 | 377 | self.* = undefined; |
| 245 | 378 | } |
| 246 | 379 | |
| 247 | 380 | fn run(self: *TestContext) !void { |
| 248 | 381 | var progress = std.Progress{}; |
| 249 | | const root_node = try progress.start("zir", self.zir_cases.items.len); |
| 382 | const root_node = try progress.start("tests", self.cases.items.len); |
| 250 | 383 | defer root_node.end(); |
| 251 | 384 | |
| 252 | 385 | const native_info = try std.zig.system.NativeTargetInfo.detect(std.heap.page_allocator, .{}); |
| 253 | 386 | |
| 254 | | for (self.zir_cases.items) |case| { |
| 387 | for (self.cases.items) |case| { |
| 255 | 388 | std.testing.base_allocator_instance.reset(); |
| 256 | 389 | |
| 257 | 390 | var prg_node = root_node.start(case.name, case.updates.items.len); |
| 258 | 391 | prg_node.activate(); |
| 259 | 392 | defer prg_node.end(); |
| 260 | 393 | |
| 261 | | // So that we can see which test case failed when the leak checker goes off. |
| 262 | | progress.refresh(); |
| 394 | // So that we can see which test case failed when the leak checker goes off, |
| 395 | // or there's an internal error |
| 396 | progress.initial_delay_ns = 0; |
| 397 | progress.refresh_rate_ns = 0; |
| 263 | 398 | |
| 264 | 399 | const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.target); |
| 265 | 400 | try self.runOneCase(std.testing.allocator, &prg_node, case, info.target); |
| ... | ... | @@ -267,17 +402,15 @@ pub const TestContext = struct { |
| 267 | 402 | } |
| 268 | 403 | } |
| 269 | 404 | |
| 270 | | fn runOneCase(self: *TestContext, allocator: *Allocator, prg_node: *std.Progress.Node, case: Case, target: std.Target) !void { |
| 405 | fn runOneCase(self: *TestContext, allocator: *Allocator, root_node: *std.Progress.Node, case: Case, target: std.Target) !void { |
| 271 | 406 | var tmp = std.testing.tmpDir(.{}); |
| 272 | 407 | defer tmp.cleanup(); |
| 273 | 408 | |
| 274 | | const root_name = "test_case"; |
| 275 | | const tmp_src_path = try std.fmt.allocPrint(allocator, "{}{}", .{ root_name, case.extension }); |
| 276 | | defer allocator.free(tmp_src_path); |
| 409 | const tmp_src_path = if (case.extension == .Zig) "test_case.zig" else if (case.extension == .ZIR) "test_case.zir" else unreachable; |
| 277 | 410 | const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path); |
| 278 | 411 | defer root_pkg.destroy(); |
| 279 | 412 | |
| 280 | | const bin_name = try std.zig.binNameAlloc(allocator, root_name, target, case.output_mode, null); |
| 413 | const bin_name = try std.zig.binNameAlloc(allocator, "test_case", target, case.output_mode, null); |
| 281 | 414 | defer allocator.free(bin_name); |
| 282 | 415 | |
| 283 | 416 | var module = try Module.init(allocator, .{ |
| ... | ... | @@ -299,7 +432,7 @@ pub const TestContext = struct { |
| 299 | 432 | defer module.deinit(); |
| 300 | 433 | |
| 301 | 434 | for (case.updates.items) |update, update_index| { |
| 302 | | var update_node = prg_node.start("update", 4); |
| 435 | var update_node = root_node.start("update", 3); |
| 303 | 436 | update_node.activate(); |
| 304 | 437 | defer update_node.end(); |
| 305 | 438 | |
| ... | ... | @@ -316,6 +449,7 @@ pub const TestContext = struct { |
| 316 | 449 | |
| 317 | 450 | switch (update.case) { |
| 318 | 451 | .Transformation => |expected_output| { |
| 452 | update_node.estimated_total_items = 5; |
| 319 | 453 | var emit_node = update_node.start("emit", null); |
| 320 | 454 | emit_node.activate(); |
| 321 | 455 | var new_zir_module = try zir.emit(allocator, module); |
| ... | ... | @@ -329,9 +463,26 @@ pub const TestContext = struct { |
| 329 | 463 | try new_zir_module.writeToStream(allocator, out_zir.outStream()); |
| 330 | 464 | write_node.end(); |
| 331 | 465 | |
| 332 | | std.testing.expectEqualSlices(u8, expected_output, out_zir.items); |
| 466 | var test_node = update_node.start("assert", null); |
| 467 | test_node.activate(); |
| 468 | defer test_node.end(); |
| 469 | if (expected_output.len != out_zir.items.len) { |
| 470 | std.debug.warn("{}\nTransformed ZIR length differs:\n================\nExpected:\n================\n{}\n================\nFound: {}\n================\nTest failed.\n", .{ case.name, expected_output, out_zir.items }); |
| 471 | std.process.exit(1); |
| 472 | } |
| 473 | for (expected_output) |e, i| { |
| 474 | if (out_zir.items[i] != e) { |
| 475 | if (expected_output.len != out_zir.items.len) { |
| 476 | std.debug.warn("{}\nTransformed ZIR differs:\n================\nExpected:\n================\n{}\n================\nFound: {}\n================\nTest failed.\n", .{ case.name, expected_output, out_zir.items }); |
| 477 | std.process.exit(1); |
| 478 | } |
| 479 | } |
| 480 | } |
| 333 | 481 | }, |
| 334 | 482 | .Error => |e| { |
| 483 | var test_node = update_node.start("assert", null); |
| 484 | test_node.activate(); |
| 485 | defer test_node.end(); |
| 335 | 486 | var handled_errors = try allocator.alloc(bool, e.len); |
| 336 | 487 | defer allocator.free(handled_errors); |
| 337 | 488 | for (handled_errors) |*h| { |
| ... | ... | @@ -360,6 +511,7 @@ pub const TestContext = struct { |
| 360 | 511 | } |
| 361 | 512 | }, |
| 362 | 513 | .Execution => |expected_stdout| { |
| 514 | update_node.estimated_total_items = 4; |
| 363 | 515 | var exec_result = x: { |
| 364 | 516 | var exec_node = update_node.start("execute", null); |
| 365 | 517 | exec_node.activate(); |
| ... | ... | @@ -376,6 +528,10 @@ pub const TestContext = struct { |
| 376 | 528 | .cwd_dir = tmp.dir, |
| 377 | 529 | }); |
| 378 | 530 | }; |
| 531 | var test_node = update_node.start("test", null); |
| 532 | test_node.activate(); |
| 533 | defer test_node.end(); |
| 534 | |
| 379 | 535 | defer allocator.free(exec_result.stdout); |
| 380 | 536 | defer allocator.free(exec_result.stderr); |
| 381 | 537 | switch (exec_result.term) { |