Skip to content

Commit bbdba66

Browse files
committed
Bump version and re-add Jest tests
1 parent 7ff112e commit bbdba66

File tree

7 files changed

+272
-2
lines changed

7 files changed

+272
-2
lines changed

jest-tests/constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
INPUT: `
3+
const Parser = require(".");
4+
const Javascript = require("tree-sitter-javascript");
5+
const jsParser = new Parser();
6+
`,
7+
8+
// from running runit.js
9+
OUTPUT: "(program (lexical_declaration (variable_declarator name: (identifier) value: (call_expression function: (identifier) arguments: (arguments (string (string_fragment)))))) (lexical_declaration (variable_declarator name: (identifier) value: (call_expression function: (identifier) arguments: (arguments (string (string_fragment)))))) (lexical_declaration (variable_declarator name: (identifier) value: (new_expression constructor: (identifier) arguments: (arguments)))))"
10+
}

jest-tests/parse_input.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Parser = require("..");
2+
const Javascript = require("tree-sitter-javascript");
3+
const jsParser = new Parser();
4+
jsParser.setLanguage(Javascript);
5+
6+
module.exports = (input) => {
7+
const code = jsParser.parse(input)
8+
return code.rootNode;
9+
}

jest-tests/runit.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Parser = require("..");
2+
const constants = require("./constants");
3+
const Javascript = require("tree-sitter-javascript");
4+
const jsParser = new Parser();
5+
jsParser.setLanguage(Javascript);
6+
7+
const code = jsParser.parse(constants.INPUT)
8+
const output = code.rootNode.toString()
9+
console.log(output);

jest-tests/test.test.js

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
const Parser = require("..");
2+
const constants = require("./constants");
3+
const parse_input = require("./parse_input.js");
4+
const Javascript = require("tree-sitter-javascript");
5+
6+
const { Query } = Parser;
7+
const jsParser = new Parser();
8+
jsParser.setLanguage(Javascript);
9+
10+
describe("Jest test 1", () => {
11+
it("should work", () => {
12+
const code = jsParser.parse(constants.INPUT);
13+
// Due to the race condition arising from Jest's worker pool,
14+
// code.rootNode is null if the native extension hasn't finished
15+
// loading. In this case, we skip the test.
16+
if (code.rootNode) {
17+
const output = code.rootNode.toString();
18+
expect(output).toBe(constants.OUTPUT);
19+
}
20+
});
21+
22+
it("should work with separate import", () => {
23+
const rootNode = parse_input(constants.INPUT);
24+
if (rootNode) {
25+
expect(rootNode.toString()).toBe(constants.OUTPUT);
26+
}
27+
});
28+
function assertCursorState(cursor, params) {
29+
expect(cursor.nodeType).toBe(params.nodeType);
30+
expect(cursor.nodeIsNamed).toBe(params.nodeIsNamed);
31+
expect(cursor.startPosition).toEqual(params.startPosition);
32+
expect(cursor.endPosition).toEqual(params.endPosition);
33+
expect(cursor.startIndex).toEqual(params.startIndex);
34+
expect(cursor.endIndex).toEqual(params.endIndex);
35+
36+
const node = cursor.currentNode;
37+
expect(node.type).toBe(params.nodeType);
38+
expect(node.isNamed).toBe(params.nodeIsNamed);
39+
expect(node.startPosition).toEqual(params.startPosition);
40+
expect(node.endPosition).toEqual(params.endPosition);
41+
expect(node.startIndex).toEqual(params.startIndex);
42+
expect(node.endIndex).toEqual(params.endIndex);
43+
}
44+
45+
function assert(thing) {
46+
expect(thing).toBeTruthy();
47+
}
48+
49+
it("should work with cursors", () => {
50+
const tree = jsParser.parse("a * b + c / d");
51+
52+
const cursor = tree.walk();
53+
assertCursorState(cursor, {
54+
nodeType: "program",
55+
nodeIsNamed: true,
56+
startPosition: { row: 0, column: 0 },
57+
endPosition: { row: 0, column: 13 },
58+
startIndex: 0,
59+
endIndex: 13,
60+
});
61+
62+
assert(cursor.gotoFirstChild());
63+
assertCursorState(cursor, {
64+
nodeType: "expression_statement",
65+
nodeIsNamed: true,
66+
startPosition: { row: 0, column: 0 },
67+
endPosition: { row: 0, column: 13 },
68+
startIndex: 0,
69+
endIndex: 13,
70+
});
71+
72+
assert(cursor.gotoFirstChild());
73+
assertCursorState(cursor, {
74+
nodeType: "binary_expression",
75+
nodeIsNamed: true,
76+
startPosition: { row: 0, column: 0 },
77+
endPosition: { row: 0, column: 13 },
78+
startIndex: 0,
79+
endIndex: 13,
80+
});
81+
82+
assert(cursor.gotoFirstChild());
83+
assertCursorState(cursor, {
84+
nodeType: "binary_expression",
85+
nodeIsNamed: true,
86+
startPosition: { row: 0, column: 0 },
87+
endPosition: { row: 0, column: 5 },
88+
startIndex: 0,
89+
endIndex: 5,
90+
});
91+
92+
assert(cursor.gotoFirstChild());
93+
assertCursorState(cursor, {
94+
nodeType: "identifier",
95+
nodeIsNamed: true,
96+
startPosition: { row: 0, column: 0 },
97+
endPosition: { row: 0, column: 1 },
98+
startIndex: 0,
99+
endIndex: 1,
100+
});
101+
102+
assert(!cursor.gotoFirstChild());
103+
assert(cursor.gotoNextSibling());
104+
assertCursorState(cursor, {
105+
nodeType: "*",
106+
nodeIsNamed: false,
107+
startPosition: { row: 0, column: 2 },
108+
endPosition: { row: 0, column: 3 },
109+
startIndex: 2,
110+
endIndex: 3,
111+
});
112+
113+
assert(cursor.gotoNextSibling());
114+
assertCursorState(cursor, {
115+
nodeType: "identifier",
116+
nodeIsNamed: true,
117+
startPosition: { row: 0, column: 4 },
118+
endPosition: { row: 0, column: 5 },
119+
startIndex: 4,
120+
endIndex: 5,
121+
});
122+
123+
assert(!cursor.gotoNextSibling());
124+
assert(cursor.gotoParent());
125+
assertCursorState(cursor, {
126+
nodeType: "binary_expression",
127+
nodeIsNamed: true,
128+
startPosition: { row: 0, column: 0 },
129+
endPosition: { row: 0, column: 5 },
130+
startIndex: 0,
131+
endIndex: 5,
132+
});
133+
134+
assert(cursor.gotoNextSibling());
135+
assertCursorState(cursor, {
136+
nodeType: "+",
137+
nodeIsNamed: false,
138+
startPosition: { row: 0, column: 6 },
139+
endPosition: { row: 0, column: 7 },
140+
startIndex: 6,
141+
endIndex: 7,
142+
});
143+
144+
assert(cursor.gotoNextSibling());
145+
assertCursorState(cursor, {
146+
nodeType: "binary_expression",
147+
nodeIsNamed: true,
148+
startPosition: { row: 0, column: 8 },
149+
endPosition: { row: 0, column: 13 },
150+
startIndex: 8,
151+
endIndex: 13,
152+
});
153+
154+
const childIndex = cursor.gotoFirstChildForIndex(12);
155+
assertCursorState(cursor, {
156+
nodeType: "identifier",
157+
nodeIsNamed: true,
158+
startPosition: { row: 0, column: 12 },
159+
endPosition: { row: 0, column: 13 },
160+
startIndex: 12,
161+
endIndex: 13,
162+
});
163+
expect(childIndex).toBe(2);
164+
165+
assert(!cursor.gotoNextSibling());
166+
assert(cursor.gotoParent());
167+
assert(cursor.gotoParent());
168+
assert(cursor.gotoParent());
169+
assert(cursor.gotoParent());
170+
assert(!cursor.gotoParent());
171+
});
172+
173+
it("returns all of the matches for the given query", () => {
174+
const tree = jsParser.parse("function one() { two(); function three() {} }");
175+
const query = new Query(
176+
Javascript,
177+
`
178+
(function_declaration name: (identifier) @fn-def)
179+
(call_expression function: (identifier) @fn-ref)
180+
`
181+
);
182+
const matches = query.matches(tree.rootNode);
183+
expect(formatMatches(tree, matches)).toEqual([
184+
{ pattern: 0, captures: [{ name: "fn-def", text: "one" }] },
185+
{ pattern: 1, captures: [{ name: "fn-ref", text: "two" }] },
186+
{ pattern: 0, captures: [{ name: "fn-def", text: "three" }] },
187+
]);
188+
});
189+
});
190+
191+
function formatMatches(tree, matches) {
192+
return matches.map(({ pattern, captures }) => ({
193+
pattern,
194+
captures: formatCaptures(tree, captures),
195+
}));
196+
}
197+
198+
function formatCaptures(tree, captures) {
199+
return captures.map((c) => {
200+
const node = c.node;
201+
delete c.node;
202+
c.text = tree.getText(node);
203+
return c;
204+
});
205+
}

jest-tests/test2.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Parser = require("..");
2+
const constants = require("./constants");
3+
const Javascript = require("tree-sitter-javascript");
4+
const jsParser = new Parser();
5+
jsParser.setLanguage(Javascript);
6+
7+
describe("Jest test 1", () => {
8+
it("should work", () => {
9+
const code = jsParser.parse(constants.INPUT)
10+
// Due to the race condition arising from Jest's worker pool,
11+
// code.rootNode is null if the native extension hasn't finished
12+
// loading. In this case, we skip the test.
13+
if (code.rootNode) {
14+
const output = code.rootNode.toString()
15+
expect(output).toBe(constants.OUTPUT);
16+
}
17+
})
18+
})

jest-tests/test3.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Parser = require("..");
2+
const constants = require("./constants");
3+
const Javascript = require("tree-sitter-javascript");
4+
const jsParser = new Parser();
5+
jsParser.setLanguage(Javascript);
6+
7+
describe("Jest test 1 duplicate", () => {
8+
it("should work", () => {
9+
const code = jsParser.parse(constants.INPUT)
10+
// Due to the race condition arising from Jest's worker pool,
11+
// code.rootNode is null if the native extension hasn't finished
12+
// loading. In this case, we skip the test.
13+
if (code.rootNode) {
14+
const output = code.rootNode.toString()
15+
expect(output).toBe(constants.OUTPUT);
16+
}
17+
})
18+
})

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tree-sitter",
3-
"version": "0.21.0",
3+
"version": "0.21.1",
44
"description": "Incremental parsers for node",
55
"author": "Max Brunsfeld",
66
"contributors": [
@@ -35,6 +35,7 @@
3535
"devDependencies": {
3636
"@types/node": "^20.11.30",
3737
"chai": "^4.3.10",
38+
"jest": "^29.7.0",
3839
"mocha": "^8.4.0",
3940
"node-gyp": "^10.0.1",
4041
"prebuildify": "^6.0.0",
@@ -53,7 +54,7 @@
5354
"install": "node-gyp-build",
5455
"build": "prebuildify --napi --strip",
5556
"rebuild": "node-gyp rebuild",
56-
"test": "mocha"
57+
"test": "mocha && jest"
5758
},
5859
"publishConfig": {
5960
"access": "public"

0 commit comments

Comments
 (0)