Skip to content

Commit 88bc2b1

Browse files
committed
update tests for sourceIndexToTokenIndex and tokenToIndex
See ziglang/zig#20885
1 parent b903dd0 commit 88bc2b1

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/offsets.zig

+18-20
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,11 @@ pub fn sourceIndexToTokenIndex(tree: Ast, source_index: usize) Ast.TokenIndex {
185185
}
186186

187187
test sourceIndexToTokenIndex {
188-
var tree = try std.zig.Ast.parse(std.testing.allocator, "🠁↉¶a", .zig);
188+
var tree = try std.zig.Ast.parse(std.testing.allocator, "🠁↉¶\na", .zig);
189189
defer tree.deinit(std.testing.allocator);
190190

191191
try std.testing.expectEqualSlices(std.zig.Token.Tag, &.{
192-
.invalid, // 🠁
193-
.invalid, // ↉
194-
.invalid, // ¶
192+
.invalid, // 🠁↉¶
195193
.identifier, // a
196194
.eof,
197195
}, tree.tokens.items(.tag));
@@ -203,18 +201,22 @@ test sourceIndexToTokenIndex {
203201
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 3));
204202

205203
// ↉
206-
try std.testing.expectEqual(1, sourceIndexToTokenIndex(tree, 4));
207-
try std.testing.expectEqual(1, sourceIndexToTokenIndex(tree, 5));
208-
try std.testing.expectEqual(1, sourceIndexToTokenIndex(tree, 6));
204+
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 4));
205+
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 5));
206+
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 6));
209207

210208
// ¶
211-
try std.testing.expectEqual(2, sourceIndexToTokenIndex(tree, 7));
212-
try std.testing.expectEqual(2, sourceIndexToTokenIndex(tree, 8));
209+
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 7));
210+
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 8));
211+
212+
// \n
213+
try std.testing.expectEqual(0, sourceIndexToTokenIndex(tree, 9));
213214

214215
// a
215-
try std.testing.expectEqual(3, sourceIndexToTokenIndex(tree, 9));
216+
try std.testing.expectEqual(1, sourceIndexToTokenIndex(tree, 10));
216217

217-
try std.testing.expectEqual(4, sourceIndexToTokenIndex(tree, 10));
218+
// EOF
219+
try std.testing.expectEqual(2, sourceIndexToTokenIndex(tree, 11));
218220
}
219221

220222
fn identifierIndexToLoc(tree: Ast, source_index: usize) Loc {
@@ -295,22 +297,18 @@ pub fn tokenToIndex(tree: Ast, token_index: Ast.TokenIndex) usize {
295297
}
296298

297299
test tokenToIndex {
298-
var tree = try std.zig.Ast.parse(std.testing.allocator, "🠁↉¶a", .zig);
300+
var tree = try std.zig.Ast.parse(std.testing.allocator, "🠁↉¶\na", .zig);
299301
defer tree.deinit(std.testing.allocator);
300302

301303
try std.testing.expectEqualSlices(std.zig.Token.Tag, &.{
302-
.invalid, // 🠁
303-
.invalid, // ↉
304-
.invalid, // ¶
304+
.invalid, // 🠁↉¶
305305
.identifier, // a
306306
.eof,
307307
}, tree.tokens.items(.tag));
308308

309-
try std.testing.expectEqual(0, tokenToIndex(tree, 0)); // 🠁
310-
try std.testing.expectEqual(4, tokenToIndex(tree, 1)); // ↉
311-
try std.testing.expectEqual(7, tokenToIndex(tree, 2)); // ¶
312-
try std.testing.expectEqual(9, tokenToIndex(tree, 3)); // a
313-
try std.testing.expectEqual(10, tokenToIndex(tree, 4)); // a
309+
try std.testing.expectEqual(0, tokenToIndex(tree, 0)); // 🠁↉¶
310+
try std.testing.expectEqual(10, tokenToIndex(tree, 1)); // a
311+
try std.testing.expectEqual(11, tokenToIndex(tree, 2)); // EOF
314312
}
315313

316314
pub fn tokensToLoc(tree: Ast, first_token: Ast.TokenIndex, last_token: Ast.TokenIndex) Loc {

0 commit comments

Comments
 (0)