forked from tree-sitter/node-tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookahead_iterable_test.js
41 lines (32 loc) · 1.4 KB
/
lookahead_iterable_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Parser = require("..");
const Rust = require("tree-sitter-rust");
const { assert } = require("chai");
const { LookaheadIterator } = Parser;
describe("LookaheadIterator", () => {
const parser = new Parser();
parser.setLanguage(Rust);
describe("test lookahead iterator", () => {
it("should iterate correctly", () => {
const tree = parser.parse("struct Stuff {}");
const cursor = tree.walk();
assert(cursor.gotoFirstChild()); // struct
assert(cursor.gotoFirstChild()); // struct keyword
const nextState = cursor.currentNode.nextParseState;
assert.notEqual(nextState, 0);
assert(cursor.gotoNextSibling()); // type_identifier
assert.equal(nextState, cursor.currentNode.parseState);
assert.equal(cursor.currentNode.grammarType, "identifier");
assert.notEqual(cursor.currentNode.grammarId, cursor.currentNode.typeId);
const expectedSymbols = ["//", "/*", "identifier", "line_comment", "block_comment"]
const lookahead = new LookaheadIterator(Rust, nextState);
let symbols = Array.from(lookahead);
assert.deepEqual(symbols, expectedSymbols);
assert(lookahead.resetState(nextState));
symbols = Array.from(lookahead);
assert.deepEqual(symbols, expectedSymbols);
assert(lookahead.reset(Rust, nextState));
symbols = Array.from(lookahead);
assert.deepEqual(symbols, expectedSymbols);
});
});
});