| ... | @@ -21,9 +21,10 @@ const ErrorMsg = struct { | ... | @@ -21,9 +21,10 @@ const ErrorMsg = struct { |
| 21 | }; | 21 | }; |
| 22 | | 22 | |
| 23 | pub const TestContext = struct { | 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 | /// The input to the current update. We simulate an incremental update | 28 | /// The input to the current update. We simulate an incremental update |
| 28 | /// with the file's contents changed to this value each update. | 29 | /// with the file's contents changed to this value each update. |
| 29 | /// | 30 | /// |
| ... | @@ -33,35 +34,43 @@ pub const TestContext = struct { | ... | @@ -33,35 +34,43 @@ pub const TestContext = struct { |
| 33 | /// effects of the incremental compilation. | 34 | /// effects of the incremental compilation. |
| 34 | src: [:0]const u8, | 35 | src: [:0]const u8, |
| 35 | case: union(enum) { | 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 | /// the expected output ZIR. | 38 | /// the expected output ZIR. |
| 38 | Transformation: [:0]const u8, | 39 | Transformation: [:0]const u8, |
| 39 | /// An error update attempts to compile bad code, and ensures that it | 40 | /// An error update attempts to compile bad code, and ensures that it |
| 40 | /// fails to compile, and for the expected reasons. | 41 | /// fails to compile, and for the expected reasons. |
| 41 | /// A slice containing the expected errors *in sequential order*. | 42 | /// A slice containing the expected errors *in sequential order*. |
| 42 | Error: []const ErrorMsg, | 43 | Error: []const ErrorMsg, |
| 43 | /// An execution update compiles and runs the input ZIR, feeding in | 44 | /// An execution update compiles and runs the input, testing the |
| 44 | /// provided input and ensuring that the stdout match what is expected. | 45 | /// stdout against the expected results |
| | 46 | /// This is a slice containing the expected message. |
| 45 | Execution: []const u8, | 47 | Execution: []const u8, |
| 46 | }, | 48 | }, |
| 47 | }; | 49 | }; |
| 48 | | 50 | |
| 49 | /// A Case consists of a set of *updates*. A update can transform ZIR, | 51 | pub const TestType = enum { |
| 50 | /// compile it, ensure that compilation fails, and more. The same Module is | 52 | Zig, |
| 51 | /// used for each update, so each update's source is treated as a single file | 53 | ZIR, |
| 52 | /// being updated by the test harness and incrementally compiled. | 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 | pub const Case = struct { | 59 | pub const Case = struct { |
| | 60 | /// The name of the test case. This is shown if a test fails, and |
| | 61 | /// otherwise ignored. |
| 54 | name: []const u8, | 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 | /// such as QEMU is required for tests to complete. | 64 | /// such as QEMU is required for tests to complete. |
| 57 | target: std.zig.CrossTarget, | 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 | output_mode: std.builtin.OutputMode, | 68 | output_mode: std.builtin.OutputMode, |
| 60 | /// Either ".zir" or ".zig" | 69 | updates: std.ArrayList(Update), |
| 61 | extension: [4]u8, | 70 | extension: TestType, |
| 62 | | 71 | |
| 63 | /// Adds a subcase in which the module is updated with new ZIR, and the | 72 | /// Adds a subcase in which the module is updated with `src`, and the |
| 64 | /// resulting ZIR is validated. | 73 | /// resulting ZIR is validated against `result`. |
| 65 | pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void { | 74 | pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void { |
| 66 | self.updates.append(.{ | 75 | self.updates.append(.{ |
| 67 | .src = src, | 76 | .src = src, |
| ... | @@ -69,6 +78,8 @@ pub const TestContext = struct { | ... | @@ -69,6 +78,8 @@ pub const TestContext = struct { |
| 69 | }) catch unreachable; | 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 | pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void { | 83 | pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void { |
| 73 | self.updates.append(.{ | 84 | self.updates.append(.{ |
| 74 | .src = src, | 85 | .src = src, |
| ... | @@ -76,31 +87,31 @@ pub const TestContext = struct { | ... | @@ -76,31 +87,31 @@ pub const TestContext = struct { |
| 76 | }) catch unreachable; | 87 | }) catch unreachable; |
| 77 | } | 88 | } |
| 78 | | 89 | |
| 79 | /// Adds a subcase in which the module is updated with invalid ZIR, and | 90 | /// Adds a subcase in which the module is updated with `src`, which |
| 80 | /// ensures that compilation fails for the expected reasons. | 91 | /// should contain invalid input, and ensures that compilation fails |
| 81 | /// | 92 | /// for the expected reasons, given in sequential order in `errors` in |
| 82 | /// Errors must be specified in sequential order. | 93 | /// the form `:line:column: error: message`. |
| 83 | pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void { | 94 | pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void { |
| 84 | var array = self.updates.allocator.alloc(ErrorMsg, errors.len) catch unreachable; | 95 | var array = self.updates.allocator.alloc(ErrorMsg, errors.len) catch unreachable; |
| 85 | for (errors) |e, i| { | 96 | for (errors) |e, i| { |
| 86 | if (e[0] != ':') { | 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 | var cur = e[1..]; | 100 | var cur = e[1..]; |
| 90 | var line_index = std.mem.indexOf(u8, cur, ":"); | 101 | var line_index = std.mem.indexOf(u8, cur, ":"); |
| 91 | if (line_index == null) { | 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 | const line = std.fmt.parseInt(u32, cur[0..line_index.?], 10) catch @panic("Unable to parse line number"); | 105 | const line = std.fmt.parseInt(u32, cur[0..line_index.?], 10) catch @panic("Unable to parse line number"); |
| 95 | cur = cur[line_index.? + 1 ..]; | 106 | cur = cur[line_index.? + 1 ..]; |
| 96 | const column_index = std.mem.indexOf(u8, cur, ":"); | 107 | const column_index = std.mem.indexOf(u8, cur, ":"); |
| 97 | if (column_index == null) { | 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 | const column = std.fmt.parseInt(u32, cur[0..column_index.?], 10) catch @panic("Unable to parse column number"); | 111 | const column = std.fmt.parseInt(u32, cur[0..column_index.?], 10) catch @panic("Unable to parse column number"); |
| 101 | cur = cur[column_index.? + 2 ..]; | 112 | cur = cur[column_index.? + 2 ..]; |
| 102 | if (!std.mem.eql(u8, cur[0..7], "error: ")) { | 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 | const msg = cur[7..]; | 116 | const msg = cur[7..]; |
| 106 | | 117 | |
| ... | @@ -116,123 +127,245 @@ pub const TestContext = struct { | ... | @@ -116,123 +127,245 @@ pub const TestContext = struct { |
| 116 | } | 127 | } |
| 117 | self.updates.append(.{ .src = src, .case = .{ .Error = array } }) catch unreachable; | 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 | ctx: *TestContext, | 139 | ctx: *TestContext, |
| 123 | name: []const u8, | 140 | name: []const u8, |
| 124 | target: std.zig.CrossTarget, | 141 | target: std.zig.CrossTarget, |
| | 142 | T: TestType, |
| 125 | ) *Case { | 143 | ) *Case { |
| 126 | const case = Case{ | 144 | ctx.cases.append(Case{ |
| 127 | .name = name, | 145 | .name = name, |
| 128 | .target = target, | 146 | .target = target, |
| 129 | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), | 147 | .updates = std.ArrayList(Update).init(ctx.cases.allocator), |
| 130 | .output_mode = .Exe, | 148 | .output_mode = .Exe, |
| 131 | .extension = ".zir".*, | 149 | .extension = T, |
| 132 | }; | 150 | }) catch unreachable; |
| 133 | ctx.zir_cases.append(case) catch unreachable; | 151 | return &ctx.cases.items[ctx.cases.items.len - 1]; |
| 134 | return &ctx.zir_cases.items[ctx.zir_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 | ctx: *TestContext, | 165 | ctx: *TestContext, |
| 139 | name: []const u8, | 166 | name: []const u8, |
| 140 | target: std.zig.CrossTarget, | 167 | target: std.zig.CrossTarget, |
| | 168 | T: TestType, |
| 141 | ) *Case { | 169 | ) *Case { |
| 142 | const case = Case{ | 170 | ctx.cases.append(Case{ |
| 143 | .name = name, | 171 | .name = name, |
| 144 | .target = target, | 172 | .target = target, |
| 145 | .updates = std.ArrayList(ZIRUpdate).init(ctx.zir_cases.allocator), | 173 | .updates = std.ArrayList(Update).init(ctx.cases.allocator), |
| 146 | .output_mode = .Obj, | 174 | .output_mode = .Obj, |
| 147 | .extension = ".zir".*, | 175 | .extension = T, |
| 148 | }; | 176 | }) catch unreachable; |
| 149 | ctx.zir_cases.append(case) catch unreachable; | 177 | return &ctx.cases.items[ctx.cases.items.len - 1]; |
| 150 | return &ctx.zir_cases.items[ctx.zir_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 | ctx: *TestContext, | 191 | ctx: *TestContext, |
| 155 | name: []const u8, | 192 | name: []const u8, |
| 156 | target: std.zig.CrossTarget, | 193 | T: TestType, |
| 157 | ) *Case { | 194 | src: [:0]const u8, |
| 158 | const case = Case{ | 195 | expected_stdout: []const u8, |
| 159 | .name = name, | 196 | ) void { |
| 160 | .target = target, | 197 | ctx.addExe(name, .{}, T).addCompareOutput(src, expected_stdout); |
| 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]; | | |
| 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 | ctx: *TestContext, | 203 | ctx: *TestContext, |
| 171 | name: []const u8, | 204 | name: []const u8, |
| 172 | target: std.zig.CrossTarget, | 205 | src: [:0]const u8, |
| 173 | ) *Case { | 206 | expected_stdout: []const u8, |
| 174 | const case = Case{ | 207 | ) void { |
| 175 | .name = name, | 208 | return ctx.addCompareOutput(name, .Zig, src, expected_stdout); |
| 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]; | | |
| 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 | ctx: *TestContext, | 214 | ctx: *TestContext, |
| 187 | name: []const u8, | 215 | name: []const u8, |
| 188 | src: [:0]const u8, | 216 | src: [:0]const u8, |
| 189 | expected_stdout: []const u8, | 217 | expected_stdout: []const u8, |
| 190 | ) void { | 218 | ) void { |
| 191 | var c = ctx.addExeZIR(name, .{}); | 219 | ctx.addCompareOutput(name, .ZIR, src, expected_stdout); |
| 192 | c.addCompareOutput(src, expected_stdout); | | |
| 193 | } | 220 | } |
| 194 | | 221 | |
| 195 | pub fn addCompareOutput( | 222 | pub fn addTransform( |
| 196 | ctx: *TestContext, | 223 | ctx: *TestContext, |
| 197 | name: []const u8, | 224 | name: []const u8, |
| | 225 | target: std.zig.CrossTarget, |
| | 226 | T: TestType, |
| 198 | src: [:0]const u8, | 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 | ) void { | 241 | ) void { |
| 201 | var c = ctx.addExe(name, .{}); | 242 | ctx.addTransform(name, target, .Zig, src, result); |
| 202 | c.addCompareOutput(src, expected_stdout); | | |
| 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 | ctx: *TestContext, | 248 | ctx: *TestContext, |
| 207 | name: []const u8, | 249 | name: []const u8, |
| 208 | target: std.zig.CrossTarget, | 250 | target: std.zig.CrossTarget, |
| 209 | src: [:0]const u8, | 251 | src: [:0]const u8, |
| 210 | result: [:0]const u8, | 252 | result: [:0]const u8, |
| 211 | ) void { | 253 | ) void { |
| 212 | var c = ctx.addObjZIR(name, target); | 254 | ctx.addTransform(name, target, .ZIR, src, result); |
| 213 | c.addTransform(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 | ctx: *TestContext, | 332 | ctx: *TestContext, |
| 218 | name: []const u8, | 333 | name: []const u8, |
| 219 | target: std.zig.CrossTarget, | 334 | target: std.zig.CrossTarget, |
| 220 | src: [:0]const u8, | 335 | src: [:0]const u8, |
| 221 | expected_errors: []const []const u8, | 336 | expected_errors: []const []const u8, |
| | 337 | fixed_src: [:0]const u8, |
| 222 | ) void { | 338 | ) void { |
| 223 | var c = ctx.addObjZIR(name, target); | 339 | var case = ctx.addObj(name, target, .Zig); |
| 224 | c.addError(src, expected_errors); | 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 | fn init() TestContext { | 362 | fn init() TestContext { |
| 228 | const allocator = std.heap.page_allocator; | 363 | const allocator = std.heap.page_allocator; |
| 229 | return .{ | 364 | return .{ .cases = std.ArrayList(Case).init(allocator) }; |
| 230 | .zir_cases = std.ArrayList(Case).init(allocator), | | |
| 231 | }; | | |
| 232 | } | 365 | } |
| 233 | | 366 | |
| 234 | fn deinit(self: *TestContext) void { | 367 | fn deinit(self: *TestContext) void { |
| 235 | for (self.zir_cases.items) |c| { | 368 | for (self.cases.items) |c| { |
| 236 | for (c.updates.items) |u| { | 369 | for (c.updates.items) |u| { |
| 237 | if (u.case == .Error) { | 370 | if (u.case == .Error) { |
| 238 | c.updates.allocator.free(u.case.Error); | 371 | c.updates.allocator.free(u.case.Error); |
| ... | @@ -240,26 +373,28 @@ pub const TestContext = struct { | ... | @@ -240,26 +373,28 @@ pub const TestContext = struct { |
| 240 | } | 373 | } |
| 241 | c.updates.deinit(); | 374 | c.updates.deinit(); |
| 242 | } | 375 | } |
| 243 | self.zir_cases.deinit(); | 376 | self.cases.deinit(); |
| 244 | self.* = undefined; | 377 | self.* = undefined; |
| 245 | } | 378 | } |
| 246 | | 379 | |
| 247 | fn run(self: *TestContext) !void { | 380 | fn run(self: *TestContext) !void { |
| 248 | var progress = std.Progress{}; | 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 | defer root_node.end(); | 383 | defer root_node.end(); |
| 251 | | 384 | |
| 252 | const native_info = try std.zig.system.NativeTargetInfo.detect(std.heap.page_allocator, .{}); | 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 | std.testing.base_allocator_instance.reset(); | 388 | std.testing.base_allocator_instance.reset(); |
| 256 | | 389 | |
| 257 | var prg_node = root_node.start(case.name, case.updates.items.len); | 390 | var prg_node = root_node.start(case.name, case.updates.items.len); |
| 258 | prg_node.activate(); | 391 | prg_node.activate(); |
| 259 | defer prg_node.end(); | 392 | defer prg_node.end(); |
| 260 | | 393 | |
| 261 | // So that we can see which test case failed when the leak checker goes off. | 394 | // So that we can see which test case failed when the leak checker goes off, |
| 262 | progress.refresh(); | 395 | // or there's an internal error |
| | 396 | progress.initial_delay_ns = 0; |
| | 397 | progress.refresh_rate_ns = 0; |
| 263 | | 398 | |
| 264 | const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.target); | 399 | const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.target); |
| 265 | try self.runOneCase(std.testing.allocator, &prg_node, case, info.target); | 400 | try self.runOneCase(std.testing.allocator, &prg_node, case, info.target); |
| ... | @@ -267,17 +402,15 @@ pub const TestContext = struct { | ... | @@ -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 | var tmp = std.testing.tmpDir(.{}); | 406 | var tmp = std.testing.tmpDir(.{}); |
| 272 | defer tmp.cleanup(); | 407 | defer tmp.cleanup(); |
| 273 | | 408 | |
| 274 | const root_name = "test_case"; | 409 | const tmp_src_path = if (case.extension == .Zig) "test_case.zig" else if (case.extension == .ZIR) "test_case.zir" else unreachable; |
| 275 | const tmp_src_path = try std.fmt.allocPrint(allocator, "{}{}", .{ root_name, case.extension }); | | |
| 276 | defer allocator.free(tmp_src_path); | | |
| 277 | const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path); | 410 | const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path); |
| 278 | defer root_pkg.destroy(); | 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 | defer allocator.free(bin_name); | 414 | defer allocator.free(bin_name); |
| 282 | | 415 | |
| 283 | var module = try Module.init(allocator, .{ | 416 | var module = try Module.init(allocator, .{ |
| ... | @@ -299,7 +432,7 @@ pub const TestContext = struct { | ... | @@ -299,7 +432,7 @@ pub const TestContext = struct { |
| 299 | defer module.deinit(); | 432 | defer module.deinit(); |
| 300 | | 433 | |
| 301 | for (case.updates.items) |update, update_index| { | 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 | update_node.activate(); | 436 | update_node.activate(); |
| 304 | defer update_node.end(); | 437 | defer update_node.end(); |
| 305 | | 438 | |
| ... | @@ -316,6 +449,7 @@ pub const TestContext = struct { | ... | @@ -316,6 +449,7 @@ pub const TestContext = struct { |
| 316 | | 449 | |
| 317 | switch (update.case) { | 450 | switch (update.case) { |
| 318 | .Transformation => |expected_output| { | 451 | .Transformation => |expected_output| { |
| | 452 | update_node.estimated_total_items = 5; |
| 319 | var emit_node = update_node.start("emit", null); | 453 | var emit_node = update_node.start("emit", null); |
| 320 | emit_node.activate(); | 454 | emit_node.activate(); |
| 321 | var new_zir_module = try zir.emit(allocator, module); | 455 | var new_zir_module = try zir.emit(allocator, module); |
| ... | @@ -329,9 +463,26 @@ pub const TestContext = struct { | ... | @@ -329,9 +463,26 @@ pub const TestContext = struct { |
| 329 | try new_zir_module.writeToStream(allocator, out_zir.outStream()); | 463 | try new_zir_module.writeToStream(allocator, out_zir.outStream()); |
| 330 | write_node.end(); | 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 | .Error => |e| { | 482 | .Error => |e| { |
| | 483 | var test_node = update_node.start("assert", null); |
| | 484 | test_node.activate(); |
| | 485 | defer test_node.end(); |
| 335 | var handled_errors = try allocator.alloc(bool, e.len); | 486 | var handled_errors = try allocator.alloc(bool, e.len); |
| 336 | defer allocator.free(handled_errors); | 487 | defer allocator.free(handled_errors); |
| 337 | for (handled_errors) |*h| { | 488 | for (handled_errors) |*h| { |
| ... | @@ -360,6 +511,7 @@ pub const TestContext = struct { | ... | @@ -360,6 +511,7 @@ pub const TestContext = struct { |
| 360 | } | 511 | } |
| 361 | }, | 512 | }, |
| 362 | .Execution => |expected_stdout| { | 513 | .Execution => |expected_stdout| { |
| | 514 | update_node.estimated_total_items = 4; |
| 363 | var exec_result = x: { | 515 | var exec_result = x: { |
| 364 | var exec_node = update_node.start("execute", null); | 516 | var exec_node = update_node.start("execute", null); |
| 365 | exec_node.activate(); | 517 | exec_node.activate(); |
| ... | @@ -376,6 +528,10 @@ pub const TestContext = struct { | ... | @@ -376,6 +528,10 @@ pub const TestContext = struct { |
| 376 | .cwd_dir = tmp.dir, | 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 | defer allocator.free(exec_result.stdout); | 535 | defer allocator.free(exec_result.stdout); |
| 380 | defer allocator.free(exec_result.stderr); | 536 | defer allocator.free(exec_result.stderr); |
| 381 | switch (exec_result.term) { | 537 | switch (exec_result.term) { |