8000 Add test for basic lexer errors · jeremydw/python-jsonpath-rw@125a371 · GitHub
[go: up one dir, main page]

Skip to content

Commit 125a371

Browse files
committed
Add test for basic lexer errors
1 parent 6a3063f commit 125a371

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

jsonpath_rw/lexer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
logger = logging.getLogger(__name__)
88

9+
class JsonPathLexerError(Exception):
10+
pass
11+
912
class JsonPathLexer(object):
1013
'''
1114
A Lexical analyzer for JsonPath.
@@ -14,7 +17,7 @@ class JsonPathLexer(object):
1417
def __init__(self, debug=False):
1518
self.debug = debug
1619
if self.__doc__ == None:
17-
raise Exception('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.')
20+
raise JsonPathLexerError('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.')
1821

1922
def tokenize(self, string):
2023
'''
@@ -78,7 +81,7 @@ def t_singlequote_SINGLEQUOTE(self, t):
7881
return t
7982

8083
def t_singlequote_error(self, t):
81-
raise Exception('Error on line %s, col %s while lexing singlequoted field: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
84+
raise JsonPathLexerError('Error on line %s, col %s while lexing singlequoted field: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
8285

8386

8487
# Double-quoted strings
@@ -96,7 +99,7 @@ def t_doublequote_DOUBLEQUOTE(self, t):
9699
return t
97100

98101
def t_doublequote_error(self, t):
99-
raise Exception('Error on line %s, col %s while lexing doublequoted field: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
102+
raise JsonPathLexerError('Error on line %s, col %s while lexing doublequoted field: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
100103

101104

102105
# Back-quoted "magic" operators
@@ -114,7 +117,7 @@ def t_backquote_BACKQUOTE(self, t):
114117
return t
115118

116119
def t_backquote_error(self, t):
117-
raise Exception('Error on line %s, col %s while lexing backquoted operator: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
120+
raise JsonPathLexerError('Error on line %s, col %s while lexing backquoted operator: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
118121

119122

120123
# Counting lines, handling errors
@@ -124,7 +127,7 @@ def t_newline(self, t):
124127
t.lexer.latest_newline = t.lexpos
125128

126129
def t_error(self, t):
127-
raise Exception('Error on line %s, col %s: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
130+
raise JsonPathLexerError('Error on line %s, col %s: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
128131

129132
if __name__ == '__main__':
130133
logging.basicConfig()

tests/test_lexer.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from ply.lex import LexToken
66

7-
from jsonpath_rw.lexer import JsonPathLexer
7+
from jsonpath_rw.lexer import JsonPathLexer, JsonPathLexerError
88

99
class TestLexer(unittest.TestCase):
1010

@@ -47,3 +47,12 @@ def test_simple_inputs(self):
4747
self.assert_lex_equiv('`this`', [self.token('this', 'NAMED_OPERATOR')])
4848
self.assert_lex_equiv('|', [self.token('|', '|')])
4949
self.assert_lex_equiv('where', [self.token('where', 'WHERE')])
50+
51+
def test_basic_errors(self):
52+
def tokenize(s):
53+
l = JsonPathLexer(debug=True)
54+
return list(l.tokenize(s))
55+
self.assertRaises(JsonPathLexerError, tokenize, "'\"")
56+
self.assertRaises(JsonPathLexerError, tokenize, '"\'')
57+
self.assertRaises(JsonPathLexerError, tokenize, '?')
58+
self.assertRaises(JsonPathLexerError, tokenize, '$.foo.bar.#')

0 commit comments

Comments
 (0)
0