Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Test helpers for VSCode textmate grammars

License

NotificationsYou must be signed in to change notification settings

PanAeon/vscode-tmgrammar-test

Repository files navigation

Node.js CINPM Version

VSCode Textmate grammar test

Provides a way to test textmate grammars against a vscode engine using user-friendly plaintext files.

Demo:

asciicast

Inspired bySublime Text syntax tests

Installation

As a project dependency:

npm i --save-dev vscode-tmgrammar-test

Or as a standalone command line tool:

npm i -g vscode-tmgrammar-testvscode-tmgrammar-test --help

Unit tests

// SYNTAX TEST "source.scala" "sample testcase"// line can start with a <comment token> and not have a valid assertionclassStack[A] {// <-----  keyword.declaration.scala//   ^ - keyword.declaration.scala entity.name.class.declaration//    ^^^^^  entity.name.class.declaration//         ^  source.scala meta.bracket.scala//          ^  entity.name.class//           ^  meta.bracket.scala//             ^  punctuation.section.block.begin.scala

To write a unit test:

  • include a header line:
<comment token> SYNTAX TEST "<language scope>" "optional description"
  • Require tokens to have specific scope by using^  :
privatevarelements:List[A]=Nil//          ^^^^^^^^ variable.other.declaration.scala
  • Get into those pesky first few characters by using<-:
varx=3// <--- keyword.declaration.volatile.scala//  the length of '-' determine how many characters are matched from the start of the linex=5//  <~- keyword.operator.comparison.scala//  you specify offset from start by using '~' character, just in case
  • To ensure that tokensdon't have undesired scopes put- symbol before them:
/ ensure comment startwith two double slashes^- comment.line.double slash.scala/ or you can combine both positive and negative scopes^ source.scala- comment.line.double slash.scala

Lines which start with a<comment token> and assertion symbol are ignored by the textmate grammar.

Note, that scope comparison takes into account relative scope's position.So, if required scopes are'scope1 scope2', the test will report an error if a grammar returns them as'scope2 scope1'.

To run a unit test:

vscode-tmgrammar-test'tests/unit/**/*.test.scala'

Snapshot tests

Snapshot tests are likefunctional tests but you don't have to write outputs explicitly.All you have to do is to provide a source files, scopes of which you want to test. Then onthe first runvscode-tmgrammar-snap will generate a set of.snap files which are aninstant snapshot of lines of the source files together with corresponding scopes.

Then if you change the grammar and run the test again, the program will output the changes betweenthe.snap file and the real output.If you satisfied with the changes you cancommit them by running

vscode-tmgrammar-snap --updateSnapshot ....

this will overwrite the existing.snap files with a new ones.After this you should commit them alongside with the source code test cases.

You can read more about them atsnapshot testing

To run snapshot test:

vscode-tmgrammar-snap'tests/snap/**/*.scala'

Language configuration via package.json

The configuration follows the format of vscode:

{"contributes": {"languages": [            {"id":"scala","extensions": [".scala",".sbt",".sc"                ]            }        ],"grammars": [            {"language":"scala","scopeName":"source.scala","path":"./syntaxes/Scala.tmLanguage.json"            }        ]    }}

The idea is that for the average language extension all necessary information for tests are already included in thepackage.json.It is optional, though. If the configuration is missing it is necessary to specify grammars and scopeName of testcases via command line options.

Right now only regular grammars andInjection Grammars viainjectTo directive are supported.

Command Line Options

Unit tests:

Usage: vscode-tmgrammar-test [options] <testcases...>Run Textmate grammar test cases using vscode-textmateArguments:  testcases                      A glob pattern(s) which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!Options:  -g, --grammar <grammar>          Path to a grammar file. Multiple options supported. 'scopeName' is taken from the grammar (default: [])  --config <configuration.json>    Path to the language configuration, package.json by default  -c, --compact                    Display output in the compact format, which is easier to use with VSCode problem matchers  --xunit-report <report.xml>      Path to directory where test reports in the XUnit format will                                   be emitted in addition to console output  --xunit-format <generic|gitlab>  Format of XML reports generated when --xunit-report is used.                                   `gitlab` format is suitable for viewing the results in GitLab  -V, --version                    output the version number  -h, --help                       display help for command

Snapshot tests:

Usage: vscode-tmgrammar-snap [options] <testcases...>Run VSCode textmate grammar snapshot testsArguments:  testcases                      A glob pattern(s) which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!Options:  -u, --updateSnapshot           overwrite all snap files with new changes  --config <configuration.json>  Path to the language configuration, package.json by default  --printNotModified             include not modified scopes in the output (default: false)  --expandDiff                   produce each diff on two lines prefixed with "++" and "--" (default: false)  -g, --grammar <grammar>        Path to a grammar file. Multiple options supported. 'scopeName' is taken from the grammar (default: [])  -s, --scope <scope>            Explicitly specify scope of testcases, e.g. source.dhall  -V, --version                  output the version number  -h, --help                     display help for command

Setup VSCode unit test task

You can setup a vscode unit test task for convenience:

        {"label":"Run tests","type":"shell","command":"vscode-tmgrammar-test -c -g testcase/dhall.tmLanguage.json '**/*.dhall'","group":"test","presentation": {"reveal":"always","panel":"new",            },"problemMatcher": {"owner":"vscode-tmgrammar-test","fileLocation": ["relative","${workspaceFolder}",                ],"pattern": [                    {"regexp":"^(ERROR)\\s([^:]+):(\\d+):(\\d+):(\\d+)\\s(.*)$","severity":1,"file":2,"line":3,"column":4,"endColumn":5,"message":6,                    },                ],            },        },

Notice the-c option that will output messages in a handy format for the problemMatcher.

Result:

Error in the editor

About

Test helpers for VSCode textmate grammars

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp