Skip to content

Instantly share code, notes, and snippets.

@nmz787
Created October 22, 2020 22:40
Show Gist options
  • Save nmz787/cf98aa465a4d071a937cf74788687a54 to your computer and use it in GitHub Desktop.
Save nmz787/cf98aa465a4d071a937cf74788687a54 to your computer and use it in GitHub Desktop.
ANTLR Python Lexer unit-testing
from antlr4 import CommonTokenStream, InputStream, Token
# from mytool.parsers.error_listener import MyErrorListener
def getTokensFromText(input_string, MyLexer):
input_stream = InputStream(input_string)
lexer = MyLexer(input_stream)
# lexer.addErrorListener(MyErrorListener)
stream = CommonTokenStream(lexer)
stream.fill()
return stream.tokens
def test_lexer_bad_input(self):
from mytool.parsers.Lexer import Lexer as MyToolsLexer
tokens = getTokensFromText("function ()", MyToolsLexer) # assumes MyLexer won't detect/parse "function ()"
self.assertEqual(1, len(tokens)) #// includes EOF
self.assertEqual(Token.EOF, tokens[0].type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment