Writing specs for grammar in atom editor

289 views Asked by At

I have written my own grammar in atom. I would like to write some specs for the same, but I am unable to understand how exactly to write one. I read the Jasmine documentation, but still not very clear. Can someone please explain how to write specs for testing out grammar in atom. Thanks

1

There are 1 answers

0
Sander On
  • Grammars are availabe available under atom.grammars.grammarForScopeName("source.yourlanguage")
  • The grammar object it returns has methods you can feed code snippets (e.g. tokenizeLine, tokenizeLines).
  • These methods return arrays of tokens.
  • Testing is just verifying if these methods return what you expect.

E.g. (CoffeeScript alert):

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!

  • Example specs
  • The array returned by the grammar.tokenizeLine call 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"
        ]
      }
    ]
    

(Kept seeing this question pop up in the search results when I was looking for an answer to the same question - so just as well document it here.)