tree-sitter/lib/binding_web/test/language-test.js

88 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-02-01 11:10:19 -07:00
const { assert } = require("chai");
let JavaScript;
describe("Language", () => {
before(async () => ({ JavaScript } = await require("./helper")));
describe(".fieldIdForName, .fieldNameForId", () => {
it("converts between the string and integer representations of fields", () => {
const nameId = JavaScript.fieldIdForName("name");
const bodyId = JavaScript.fieldIdForName("body");
2021-02-01 11:10:19 -07:00
assert.isBelow(nameId, JavaScript.fieldCount);
assert.isBelow(bodyId, JavaScript.fieldCount);
assert.equal("name", JavaScript.fieldNameForId(nameId));
assert.equal("body", JavaScript.fieldNameForId(bodyId));
2021-02-01 11:10:19 -07:00
});
it("handles invalid inputs", () => {
assert.equal(null, JavaScript.fieldIdForName("namezzz"));
assert.equal(null, JavaScript.fieldNameForId(-1));
assert.equal(null, JavaScript.fieldNameForId(10000));
2021-02-01 11:10:19 -07:00
});
});
describe(".idForNodeType, .nodeTypeForId, .nodeTypeIsNamed", () => {
it("converts between the string and integer representations of a node type", () => {
const exportStatementId = JavaScript.idForNodeType("export_statement", true);
const starId = JavaScript.idForNodeType("*", false);
2021-02-01 11:10:19 -07:00
assert.isBelow(exportStatementId, JavaScript.nodeTypeCount);
assert.isBelow(starId, JavaScript.nodeTypeCount);
assert.equal(true, JavaScript.nodeTypeIsNamed(exportStatementId))
assert.equal("export_statement", JavaScript.nodeTypeForId(exportStatementId))
assert.equal(false, JavaScript.nodeTypeIsNamed(starId))
assert.equal("*", JavaScript.nodeTypeForId(starId))
2021-02-01 11:10:19 -07:00
});
it("handles invalid inputs", () => {
assert.equal(null, JavaScript.nodeTypeForId(-1));
assert.equal(null, JavaScript.nodeTypeForId(10000));
assert.equal(null, JavaScript.idForNodeType("export_statement", false));
2021-02-01 11:10:19 -07:00
});
});
});
2023-06-16 11:33:00 +03:00
describe("Lookahead iterator", () => {
let lookahead;
let state;
before(async () => {
let Parser;
({ JavaScript, Parser } = await require("./helper"));
const parser = new Parser().setLanguage(JavaScript);
const tree = parser.parse("function fn() {}");
2023-06-16 13:20:20 +03:00
parser.delete();
2023-06-16 11:33:00 +03:00
const cursor = tree.walk();
2023-06-16 13:20:20 +03:00
assert(cursor.gotoFirstChild());
assert(cursor.gotoFirstChild());
2023-06-16 11:33:00 +03:00
state = cursor.currentNode().nextParseState;
lookahead = JavaScript.lookaheadIterator(state);
assert.exists(lookahead);
});
2023-06-16 13:20:20 +03:00
after(() => {
lookahead.delete();
});
2023-06-16 11:33:00 +03:00
const expected = ["identifier", "comment", "(", "*", "formal_parameters"];
it("should iterate over valid symbols in the state", () => {
const symbols = Array.from(lookahead);
assert.includeMembers(symbols, expected);
assert.lengthOf(symbols, expected.length);
});
it("should reset to the initial state", () => {
assert(lookahead.resetState(state));
const symbols = Array.from(lookahead);
assert.includeMembers(symbols, expected);
assert.lengthOf(symbols, expected.length);
});
it("should reset", () => {
assert(lookahead.reset(JavaScript, state));
const symbols = Array.from(lookahead);
assert.includeMembers(symbols, expected);
assert.lengthOf(symbols, expected.length);
});
});