diff options
author | Kenn Knowles <kenn.knowles@gmail.com> | 2013-06-21 07:42:01 -0700 |
---|---|---|
committer | Kenn Knowles <kenn.knowles@gmail.com> | 2013-06-21 07:42:01 -0700 |
commit | 7d5169c363a8b2366a3b08ae102f3246ac8b8a7b (patch) | |
tree | 09098fa781f13596e890d1884d6f9d7bc671d062 | |
parent | 5c41c53877eb26df40c82d58ef33c0901891b81f (diff) | |
parent | 125a3714fa09eda5f8e7cdd99d405e136bdab319 (diff) | |
download | jsonpath-rw-7d5169c363a8b2366a3b08ae102f3246ac8b8a7b.tar.gz |
Merge pull request #5 from iley/master
Add test for lexer errors
-rw-r--r-- | jsonpath_rw/lexer.py | 13 | ||||
-rw-r--r-- | tests/test_lexer.py | 11 |
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.#') |