Writing specs for grammar in the atom editor

I wrote my own grammar in atom. I would like to write some specs for the same, but I cannot figure out how exactly to write it. I've read Jasmine's documentation but still not very clear. Can someone explain how to write specs for testing grammar in an atom. Thanks to

+3


source to share


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







All Articles