summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Strukov <iley@iley.ru>2013-06-21 16:12:22 +0400
committerIlya Strukov <iley@iley.ru>2013-06-21 16:12:22 +0400
commit125a3714fa09eda5f8e7cdd99d405e136bdab319 (patch)
tree09098fa781f13596e890d1884d6f9d7bc671d062
parent6a3063f3c183b623ef8bf4c7ba3d8b781d7ceca5 (diff)
downloadjsonpath-rw-125a3714fa09eda5f8e7cdd99d405e136bdab319.tar.gz
Add test for basic lexer errors
-rw-r--r--jsonpath_rw/lexer.py13
-rw-r--r--tests/test_lexer.py11
2 files changed, 18 insertions, 6 deletions
diff --git a/jsonpath_rw/lexer.py b/jsonpath_rw/lexer.py
index e88baae..d9efa50 100644
--- a/jsonpath_rw/lexer.py
+++ b/jsonpath_rw/lexer.py
@@ -6,6 +6,9 @@ import ply.lex
logger = logging.getLogger(__name__)
+class JsonPathLexerError(Exception):
+ pass
+
class JsonPathLexer(object):
'''
A Lexical analyzer for JsonPath.
@@ -14,7 +17,7 @@ class JsonPathLexer(object):
def __init__(self, debug=False):
self.debug = debug
if self.__doc__ == None:
- raise Exception('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.')
+ raise JsonPathLexerError('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.')
def tokenize(self, string):
'''
@@ -78,7 +81,7 @@ class JsonPathLexer(object):
return t
def t_singlequote_error(self, t):
- 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]))
+ 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]))
# Double-quoted strings
@@ -96,7 +99,7 @@ class JsonPathLexer(object):
return t
def t_doublequote_error(self, t):
- 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]))
+ 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]))
# Back-quoted "magic" operators
@@ -114,7 +117,7 @@ class JsonPathLexer(object):
return t
def t_backquote_error(self, t):
- 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]))
+ 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]))
# Counting lines, handling errors
@@ -124,7 +127,7 @@ class JsonPathLexer(object):
t.lexer.latest_newline = t.lexpos
def t_error(self, t):
- raise Exception('Error on line %s, col %s: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
+ raise JsonPathLexerError('Error on line %s, col %s: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0]))
if __name__ == '__main__':
logging.basicConfig()
diff --git a/tests/test_lexer.py b/tests/test_lexer.py
index c1ddc0f..115874f 100644
--- a/tests/test_lexer.py
+++ b/tests/test_lexer.py
@@ -4,7 +4,7 @@ import unittest
from ply.lex import LexToken
-from jsonpath_rw.lexer import JsonPathLexer
+from jsonpath_rw.lexer import JsonPathLexer, JsonPathLexerError
class TestLexer(unittest.TestCase):
@@ -47,3 +47,12 @@ class TestLexer(unittest.TestCase):
self.assert_lex_equiv('`this`', [self.token('this', 'NAMED_OPERATOR')])
self.assert_lex_equiv('|', [self.token('|', '|')])
self.assert_lex_equiv('where', [self.token('where', 'WHERE')])
+
+ def test_basic_errors(self):
+ def tokenize(s):
+ l = JsonPathLexer(debug=True)
+ return list(l.tokenize(s))
+ self.assertRaises(JsonPathLexerError, tokenize, "'\"")
+ self.assertRaises(JsonPathLexerError, tokenize, '"\'')
+ self.assertRaises(JsonPathLexerError, tokenize, '?')
+ self.assertRaises(JsonPathLexerError, tokenize, '$.foo.bar.#')