Created
October 22, 2020 22:40
-
-
Save nmz787/cf98aa465a4d071a937cf74788687a54 to your computer and use it in GitHub Desktop.
ANTLR Python Lexer unit-testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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