11const 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
354describe ( "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