Skip to content

Commit

Permalink
Updated Readme.md, add relaxed rule for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
PanAeon committed Apr 29, 2022
1 parent cf38b6b commit b6c7ef0
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 135 deletions.
37 changes: 0 additions & 37 deletions .vscode/launch.json

This file was deleted.

31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Testing

Any contributions are really appreciated! :) Just make sure that all existing tests work, or if they have changed the
change is meaningful.

0. `npm run build`
1. `npm run test`

2. Manually:

```
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/multiple.groups.test.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/misplaced.scopes.test.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/out.of.bounds.test.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/s.parser.test.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/simple.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/successful.test.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/unexpected.scopes.test.dhall
node ./dist/unit.js --grammar ./test/resources/dhall.tmLanguage.json ./test/resources/misplaced.scopes.test.dhall
node ./dist/snapshot.js --scope source.dhall -g ./test/resources/dhall.tmLanguage.json ./test/resources/snaps/simple.dhall
```

## Packaging:
```
npm run build
npm version patch
npm pack
npm publish
```
31 changes: 0 additions & 31 deletions Developing.md

This file was deleted.

118 changes: 72 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

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

* Unit tests:
![Showcase unit](images/showcase.gif?raw=true "unit test in action")
Demo:

* Snapshot tests:

![Showcase snap](images/Snapshots.png "snapshots test in action")
[![asciicast](https://asciinema.org/a/QoGS5fPsxDOHl1T43zzmFxJAU.svg)](https://asciinema.org/a/QoGS5fPsxDOHl1T43zzmFxJAU)

Inspired by [Sublime Text syntax tests](https://www.sublimetext.com/docs/3/syntax.html#testing)

Expand All @@ -19,7 +16,7 @@ Inspired by [Sublime Text syntax tests](https://www.sublimetext.com/docs/3/synta
As a project dependency:

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

Or as a standalone command line tool:
Expand All @@ -35,7 +32,7 @@ vscode-tmgrammar-test --help
```scala
// SYNTAX TEST "source.scala" "sample testcase"

// line which start with a <comment token> but don't have valid assertions are ignored
// line can start with a <comment token> and not have a valid assertion

class Stack[A] {
// <----- keyword.declaration.scala
Expand All @@ -45,17 +42,6 @@ class Stack[A] {
// ^ entity.name.class
// ^ meta.bracket.scala
// ^ punctuation.section.block.begin.scala
private var elements: List[A] = Nil
def push(x: A) { elements = x :: elements }
def peek: A = elements.head
def pop(): A = {
val currentTop = peek
elements = elements.tail
currentTop
}
// <~~- punctuation.section.block.end.scala
}

```

To write a unit test:
Expand Down Expand Up @@ -90,12 +76,18 @@ x=5
^ - comment.line.double-slash.scala punctuation.definition.comment.scala
```

Any lines which start with a `<comment token>` will be ignored by the textmate grammar.
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:
```bash
vscode-tmgrammar-test 'tests/unit/**/*.test.scala'
```


### Snapshot tests
Snapshot tests are like `functional 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 on
Expand All @@ -106,54 +98,88 @@ Then if you change the grammar and run the test again, the program will output t
the `.snap` file and the real output.
If you satisfied with the changes you can `commit` them by running
```bash
vscode-tmgrammar-snap .... --updateSnapshot
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 at [snapshot testing](https://jestjs.io/docs/en/snapshot-testing)

To run snapshot test:
```bash
vscode-tmgrammar-snap 'tests/snap/**/*.scala'
```

### Language configuration via package.json

The configuration follows the format of vscode:

```json
{
"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 the `package.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 and *Injection Grammars* via `injectTo` directive are supported.


### Command Line Options

* Unit tests:
Unit tests:
```
Usage: vscode-tmgrammar-test [options]
Usage: vscode-tmgrammar-test [options] <testcases...>

Run Textmate grammar test cases using vscode-textmate

Arguments:
testcases A glob pattern(s) which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!

Options:
-V, --version output the version number
-s, --scope <scope> Language scope, e.g. source.dhall
-g, --grammar <grammar> Path to a grammar file, either .json or .xml. This option can be specified multiple times if multiple grammar needed. (default: [])
-t, --testcases <glob> A glob pattern which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!
-v, --validate Validate the grammar file for well-formedness and pattern validity instead of running testcases.
-c, --compact Display output in the compact format, which is easier to use with VSCode problem matchers
-h, --help output usage information
-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
-V, --version output the version number
-h, --help display help for command
```

* Snapshot tests:
Snapshot tests:
```
Usage: vscode-tmgrammar-snap [options]
Usage: vscode-tmgrammar-snap [options] <testcases...>

Run VSCode textmate grammar snapshot tests

Options:
-V, --version output the version number
-s, --scope <scope> Language scope, e.g. source.dhall
-g, --grammar <grammar> Path to a grammar file, either .json or .xml. This option can be specified multiple times if multiple grammar needed. (default: [])
-t, --testcases <glob> A glob pattern which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!
-u, --updateSnapshot overwrite all snap files with new changes
--printNotModified include not modified scopes in the output
--expandDiff produce each diff on two lines prefixed with "++" and "--"
-h, --help output usage information
```

Example:
Arguments:
testcases A glob pattern(s) which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!

```bash
> vscode-tmgrammar-test -s source.dhall -g testcase/dhall.tmLanguage.json -t "**/*.dhall"
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
Expand All @@ -164,7 +190,7 @@ You can setup a vscode unit test task for convenience:
{
"label": "Run tests",
"type": "shell",
"command": "vscode-tmgrammar-test -c -s source.dhall -g testcase/dhall.tmLanguage.json -t \"**/*.dhall\"",
"command": "vscode-tmgrammar-test -c -g testcase/dhall.tmLanguage.json \"**/*.dhall\"",
"group": "test",
"presentation": {
"reveal": "always",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"name": "vscode-tmgrammar-test",
"version": "0.1.1-snapshot",
"description": "Test helper for VSCode textmate grammars",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts",
"description": "Test runner for VSCode textmate grammars",
"homepage": "https://github.com/PanAeon/vscode-tmgrammar-test",
"repository": {
"type": "git",
Expand All @@ -20,8 +18,8 @@
"test": "mocha -r ts-node/register 'test/**/*.test.ts'"
},
"bin": {
"vscode-tmgrammar-test": "./dist/src/unit.js",
"vscode-tmgrammar-snap": "./dist/src/snapshot.js"
"vscode-tmgrammar-test": "./dist/unit.js",
"vscode-tmgrammar-snap": "./dist/snapshot.js"
},
"keywords": [
"test",
Expand Down
6 changes: 3 additions & 3 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { getVSCodeTokens, renderSnap, parseSnap, AnnotatedLine } from './snapsho
import * as diff from 'diff'
import * as path from 'path'

let packageJson = require('../../package.json')
let packageJson = require('../package.json')

program
.description('Run VSCode textmate grammar snapshot tests')
.option('-u, --updateSnapshot', 'overwrite all snap files with new changes')
.option('-c, --config <configuration.json>', 'Path to the language configuration, package.json by default')
.option('--config <configuration.json>', 'Path to the language configuration, package.json by default')
.option('--printNotModified', 'include not modified scopes in the output', false)
.option('--expandDiff', 'produce each diff on two lines prefixed with "++" and "--"', false)
.option(
Expand Down Expand Up @@ -65,7 +65,7 @@ const rawTestCases = program.args.map((x) => glob.sync(x)).flat()

const testCases = rawTestCases.filter((x) => !x.endsWith('.snap'))

if (testCases.length == 0) {
if (testCases.length === 0) {
console.log(chalk.red('ERROR') + " No testcases found. Got: '" + chalk.gray(program.args.join(',')) + "'")
process.exit(-1)
}
Expand Down
7 changes: 4 additions & 3 deletions src/unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import { runGrammarTestCase, parseGrammarTestCase, GrammarTestCase, TestFailure

import { createRegistry, loadConfiguration, IGrammarConfig } from './common/index'

let packageJson = require('../../package.json')
let packageJson = require('../package.json')

function collectGrammarOpts(value: String, previous: String[]): String[] {
return previous.concat([value])
}

program
.version(packageJson.version)
.description('Run Textmate grammar test cases using vscode-textmate')
.option(
'-g, --grammar <grammar>',
'Path to a grammar file, either .json or .xml. This option can be specified multiple times if multiple grammar needed.',
"Path to a grammar file. Multiple options supported. 'scopeName' is taken from the grammar",
collectGrammarOpts,
[]
)
.option('--config <configuration.json>', 'Path to the language configuration, package.json by default')
.option('-c, --compact', 'Display output in the compact format, which is easier to use with VSCode problem matchers')
.version(packageJson.version)
.argument(
'<testcases...>',
'A glob pattern(s) which specifies testcases to run, e.g. "./tests/**/test*.dhall". Quotes are important!'
Expand Down
2 changes: 1 addition & 1 deletion src/unit/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function parseGrammarTestCase(str: string): GrammarTestCase {
let rest = lines.slice(headerLength)
let commentTokenLength = commentToken.length

function isLineAssertion(s:string):boolean {
function isLineAssertion(s: string): boolean {
return s.startsWith(commentToken) && /^\s*(\^|<[~]*[-]+)/.test(s.substring(commentTokenLength))
}

Expand Down
Loading

0 comments on commit b6c7ef0

Please sign in to comment.