Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 7f2ca52

Browse files
test clean up
1 parent 5963771 commit 7f2ca52

2 files changed

Lines changed: 104 additions & 90 deletions

File tree

spec/tree-sitter-helpers.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const dedent = require("dedent");
2+
3+
module.exports = {
4+
// https://github.com/atom/atom/blob/b3d3a52d9e4eb41f33df7b91ad1f8a2657a04487/spec/tree-sitter-language-mode-spec.js#L47-L55
5+
expectTokensToEqual(editor, expectedTokenLines, startingRow = 1) {
6+
const lastRow = editor.getLastScreenRow();
7+
8+
for (let row = startingRow; row <= lastRow - startingRow; row++) {
9+
const tokenLine = editor
10+
.tokensForScreenRow(row)
11+
.map(({ text, scopes }) => ({
12+
text,
13+
scopes: scopes.map((scope) =>
14+
scope
15+
.split(" ")
16+
.map((className) => className.replace("syntax--", ""))
17+
.join(".")
18+
),
19+
}));
20+
21+
const expectedTokenLine = expectedTokenLines[row - startingRow];
22+
23+
expect(tokenLine.length).toEqual(expectedTokenLine.length);
24+
for (let i = 0; i < tokenLine.length; i++) {
25+
expect(tokenLine[i].text).toEqual(
26+
expectedTokenLine[i].text,
27+
`Token ${i}, row: ${row}`
28+
);
29+
expect(tokenLine[i].scopes).toEqual(
30+
expectedTokenLine[i].scopes,
31+
`Token ${i}, row: ${row}, token: '${tokenLine[i].text}'`
32+
);
33+
}
34+
}
35+
},
36+
37+
toHaveScopesAtPosition(posn, token, expected, includeEmbeddedScopes = false) {
38+
if (expected === undefined) {
39+
expected = token;
40+
}
41+
if (token === undefined) {
42+
expected = [];
43+
}
44+
45+
// token is not used at this time; it's just a way to keep note where we are
46+
// in the line
47+
48+
let filterEmbeddedScopes = (scope) =>
49+
includeEmbeddedScopes ||
50+
(scope !== "text.html.php" &&
51+
scope !== "meta.embedded.block.php" &&
52+
scope !== "meta.embedded.line.php");
53+
54+
let actual = this.actual
55+
.scopeDescriptorForBufferPosition(posn)
56+
.scopes.filter(filterEmbeddedScopes);
57+
58+
let notExpected = actual.filter((scope) => !expected.includes(scope));
59+
let notReceived = expected.filter((scope) => !actual.includes(scope));
60+
61+
let pass = notExpected.length === 0 && notReceived.length === 0;
62+
63+
if (pass) {
64+
this.message = () => "Scopes matched";
65+
} else {
66+
let line = this.actual.getBuffer().lineForRow(posn[0]);
67+
let caret = " ".repeat(posn[1]) + "^";
68+
69+
this.message = () =>
70+
`Failure:
71+
Scopes did not match at position [${posn.join(", ")}]:
72+
${line}
73+
${caret}
74+
These scopes were expected but not received:
75+
${notReceived.join(", ")}
76+
These scopes were received but not expected:
77+
${notExpected.join(", ")}
78+
`;
79+
}
80+
81+
return pass;
82+
},
83+
84+
setContent(content) {
85+
this.setText(`<?php
86+
${dedent(content)}
87+
`);
88+
},
89+
90+
nextHighlightingUpdate(editor) {
91+
return new Promise((resolve) => {
92+
const subscription = editor
93+
.getBuffer()
94+
.getLanguageMode()
95+
.onDidChangeHighlighting(() => {
96+
subscription.dispose();
97+
resolve();
98+
});
99+
});
100+
},
101+
};

spec/tree-sitter-spec.js

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,5 @@
11
const dedent = require("dedent");
2-
3-
// https://github.com/atom/atom/blob/b3d3a52d9e4eb41f33df7b91ad1f8a2657a04487/spec/tree-sitter-language-mode-spec.js#L47-L55
4-
function expectTokensToEqual (editor, expectedTokenLines, startingRow = 1) {
5-
const lastRow = editor.getLastScreenRow()
6-
7-
for (let row = startingRow; row <= lastRow-startingRow; row++) {
8-
const tokenLine = editor.tokensForScreenRow(row).map(({text, scopes}) => ({
9-
text,
10-
scopes: scopes.map(scope => scope
11-
.split(' ')
12-
.map(className => className.replace('syntax--', ''))
13-
.join('.'))
14-
}))
15-
16-
const expectedTokenLine = expectedTokenLines[row-startingRow]
17-
18-
expect(tokenLine.length).toEqual(expectedTokenLine.length)
19-
for (let i = 0; i < tokenLine.length; i++) {
20-
expect(tokenLine[i].text).toEqual(expectedTokenLine[i].text, `Token ${i}, row: ${row}`)
21-
expect(tokenLine[i].scopes).toEqual(expectedTokenLine[i].scopes, `Token ${i}, row: ${row}, token: '${tokenLine[i].text}'`)
22-
}
23-
}
24-
}
25-
26-
function nextHighlightingUpdate (editor) {
27-
return new Promise(resolve => {
28-
const subscription = editor.getBuffer().getLanguageMode().onDidChangeHighlighting(() => {
29-
subscription.dispose()
30-
resolve()
31-
})
32-
})
33-
}
2+
const {expectTokensToEqual, toHaveScopesAtPosition, setContent, nextHighlightingUpdate} = require('./tree-sitter-helpers')
343

354
describe("Tree-sitter PHP grammar", () => {
365
var editor;
@@ -39,67 +8,11 @@ describe("Tree-sitter PHP grammar", () => {
398
atom.config.set("core.useTreeSitterParsers", true);
409
await atom.packages.activatePackage("language-php");
4110
editor = await atom.workspace.open("foo.php");
42-
editor.setContent = function (content) {
43-
this.setText(`<?php
44-
${dedent(content)}
45-
`);
46-
};
11+
editor.setContent = setContent;
4712
});
4813

4914
beforeEach(function () {
50-
this.addMatchers({
51-
toHaveScopesAtPosition(
52-
posn,
53-
token,
54-
expected,
55-
includeEmbeddedScopes = false
56-
) {
57-
if (expected === undefined) {
58-
expected = token;
59-
}
60-
if (token === undefined) {
61-
expected = [];
62-
}
63-
64-
// token is not used at this time; it's just a way to keep note where we are
65-
// in the line
66-
67-
let filterEmbeddedScopes = scope =>
68-
includeEmbeddedScopes ||
69-
scope !== 'text.html.php' &&
70-
scope !== 'meta.embedded.block.php' &&
71-
scope !== 'meta.embedded.line.php'
72-
73-
let actual = this.actual
74-
.scopeDescriptorForBufferPosition(posn).scopes
75-
.filter(filterEmbeddedScopes)
76-
77-
let notExpected = actual.filter((scope) => !expected.includes(scope));
78-
let notReceived = expected.filter((scope) => !actual.includes(scope));
79-
80-
let pass = notExpected.length === 0 && notReceived.length === 0;
81-
82-
if (pass) {
83-
this.message = () => "Scopes matched";
84-
} else {
85-
let line = this.actual.getBuffer().lineForRow(posn[0]);
86-
let caret = " ".repeat(posn[1]) + "^";
87-
88-
this.message = () =>
89-
`Failure:
90-
Scopes did not match at position [${posn.join(", ")}]:
91-
${line}
92-
${caret}
93-
These scopes were expected but not received:
94-
${notReceived.join(", ")}
95-
These scopes were received but not expected:
96-
${notExpected.join(", ")}
97-
`;
98-
}
99-
100-
return pass;
101-
},
102-
});
15+
this.addMatchers({toHaveScopesAtPosition});
10316
});
10417

10518
describe("loading the grammar", () => {

0 commit comments

Comments
 (0)