Writing specs for grammar in the atom editor
1 answer
- Grammar are available in
atom.grammars.grammarForScopeName("source.yourlanguage")
- The grammar object it returns has methods that you can serve in snippets of code (like
tokenizeLine
,tokenizeLines
). - These methods return arrays of tokens.
- Testing is simply a test to see if these methods believe what you expect.
eg. (CoffeeScript Notice):
grammar = atom.grammars.grammarForScopeName("source.yourlanguage")
{tokens} = grammar.tokenizeLine("# this is a comment line of some sort")
expect(tokens[0].value).toEqual "#"
expect(tokens[0].scopes).toEqual [
"source.yourlanguage",
"comment.line.number-sign.yourlanguage",
"punctuation.definition.comment.yourlanguage"
]
Happy testing!
- Examples of specifications
-
The array returned by the call
grammar.tokenizeLine
above looks like this:[ { "value": "#", "scopes": [ "source.yourlanguage", "comment.line.number-sign.yourlanguage", "punctuation.definition.comment.yourlanguage" ] }, { "value": " this is a comment line of some sort", "scopes": [ "source.yourlanguage", "comment.line.number-sign.yourlanguage" ] }, { "value": "", "scopes": [ "source.yourlanguage", "comment.line.number-sign.yourlanguage" ] } ]
(Seeing this question popping up in the search results when I was looking for an answer to the same question - document it well here as well.)
+3
source to share